保持循环直到输入(discord.py) [英] Keeeping the loop going until input (discord.py)

查看:37
本文介绍了保持循环直到输入(discord.py)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行discord.py僵尸程序,并且希望能够通过IDLE控制台发送消息.如何在不停止漫游器其他动作的情况下执行此操作?我检查了asyncio,发现没有办法.我正在寻找这样的东西:

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

我已经尝试过pygame,但没有结果,但是我也可以尝试使用pygame的任何其他建议.

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

推荐答案

您可以使用 aioconsole .然后,您可以创建一个后台任务,以异步方式等待来自控制台的输入.

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

异步版本的示例:

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')

这篇关于保持循环直到输入(discord.py)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆