我运行一个discord.py机器人,我希望能够通过中间控制台发送消息。我怎样才能做到这一点不停止机器人的其他行动?我已经签出ASYNCIO,发现没办法通过。我在寻找这样的事情:
async def some_command():
#actions
if input is given to the console:
#another action
我已经尝试过pygame的,没有结果,但我也可以尝试与pygame的任何其他建议。
您可以使用aioconsole
。然后,您可以创建一个异步等待输入从控制台后台任务。
举例async
版本:
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')
举例rewrite
版本:
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')