2

I'm running a discord.py bot and I want to be able to send messages through the IDLE console. How can I do this without stopping the bot's other actions? I've checked out asyncio and found no way through. I'm looking for something like this:

async def some_command():
    #actions

if input is given to the console:
     #another action

I've already tried pygame with no results but I can also try any other suggestions with pygame.

1 Answer 1

3

You can use aioconsole. You can then create a background task that asynchronously waits for input from console.

Example for async version:

from discord.ext import commands
import aioconsole

client = commands.Bot(command_prefix='!')


@client.command()
async def ping():
    await client.say('Pong')


async def background_task():
    await client.wait_until_ready()
    channel = client.get_channel('123456') # channel ID to send goes here
    while not client.is_closed:
        console_input = await aioconsole.ainput("Input to send to channel: ")
        await client.send_message(channel, console_input)

client.loop.create_task(background_task())
client.run('token')

Example for rewrite version:

from discord.ext import commands
import aioconsole

client = commands.Bot(command_prefix='!')


@client.command()
async def ping(ctx):
    await ctx.send('Pong')


async def background_task():
    await client.wait_until_ready()
    channel = client.get_channel(123456) # channel ID to send goes here
    while not client.is_closed():
        console_input = await aioconsole.ainput("Input to send to channel: ")
        await channel.send(console_input)

client.loop.create_task(background_task())
client.run('token')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.