【问题标题】:Keeeping the loop going until input (discord.py)保持循环直到输入(discord.py)
【发布时间】:2019-02-02 16:02:51
【问题描述】:
我正在运行一个 discord.py 机器人,我希望能够通过 IDLE 控制台发送消息。如何在不停止机器人的其他操作的情况下做到这一点?我已经检查了 asyncio 并发现没有办法通过。
我正在寻找这样的东西:
async def some_command():
#actions
if input is given to the console:
#another action
我已经尝试过 pygame,但没有任何结果,但我也可以尝试使用 pygame 的任何其他建议。
【问题讨论】:
标签:
python-3.x
discord.py
【解决方案1】:
您可以使用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')