【问题标题】:Discord py get inputDiscord py 获取输入
【发布时间】:2021-05-28 00:55:11
【问题描述】:

嘿,有没有办法进入机器人输入并发送一条消息?

@client.event
async def on_ready:
    while True:
        i = input("$")
        print(i)

但如果我运行它,机器人不会再对正常命令做出反应...

这就是为什么我尝试使用线程,但出现无法与 discord py api 交互的问题,因为这些函数都是异步的。

【问题讨论】:

  • 你是想从公会成员那里获得意见,还是从运行程序的人那里获得意见?

标签: python discord discord.py bots


【解决方案1】:

input是一个阻塞函数,有两种非阻塞方式运行它

  1. 使用loop.run_in_executor
@client.event
async def on_ready:
    while True:
        i = await client.loop.run_in_executor(None, input, "$")
        print(i)

使用此方法无法传递 kwargs,如果您需要它们请发表评论,我将编辑我的答案

  1. 使用aioconsole.ainput,使用以下命令安装
pip install aioconsole

使用它

from aioconsole import ainput

@client.event
async def on_ready:
    while True:
        i = await ainput("$")
        print(i)

aioconsole pypi

这也会从 CONSOLE 获得输入,而不是从 discord 获得。如果你愿意,你可以使用wait_for

【讨论】:

  • 非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 2022-12-10
  • 2021-03-24
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
相关资源
最近更新 更多