python - 如何在'on message'事件discord.py rewrite中使用wait_for命令
问题描述
我正在尝试使 discord 机器人具有与命令相同的功能
input()
,但由于 discord.py rewrite 没有该命令,我搜索了 API 并找到了wait_for
. 但是,当然,它带来了一大堆问题。我在互联网上搜索了这个,但大多数答案都在 a@command.command
而不是async def on_message(message)
,其他的并没有真正的帮助。我得到的最远的是:
def check(m):
if m.author.name == message.author.name and m.channel.name == message.channel.name:
return True
else:
return False
msg = "404 file not found"
try:
msg = await client.wait_for('message', check=check, timeout=60)
await message.channel.send(msg)
except TimeoutError:
await message.channel.send("timed out. try again.")
pass
except Exception as e:
print(e)
pass
```
解决方案
首先,您msg
对多个事物使用相同的变量。这是我可以根据您提供的信息制作的工作示例。
msg = "404 file not found"
await message.channel.send(msg)
def check(m):
return m.author == message.author and m.channel == message.channel
try:
mesg = await client.wait_for("message", check=check, timeout=60)
except TimeoutError: # The only error this can raise is an asyncio.TimeoutError
return await message.channel.send("Timed out, try again.")
await message.channel.send(mesg.content) # mesg.content is the response, do whatever you want with this
mesg 返回一个消息对象。
希望这可以帮助!
推荐阅读
- flutter - TextFormField:使用控制器时操作初始值
- xamarin - 如何停止 (c) 在 Xamarin.Forms 中成为版权 ©?
- azure - KEDA 缩放器无法在 AKS 上使用 pod 身份触发身份验证
- typescript - 如何在 NestJS 中实现 OnApplicationShutdown 接口的类中使用 Service
- python - 将列表添加到列表列表
- html - 在 GrooveFunnels 中创建时,如何将网站的导出代码居中?
- javascript - Android 蓝牙打印速度很慢。用于原始打印的 Java 脚本(直接调用打印机)
- java - 如何获取方法调用返回的字段
- swift - SpriteView与普通View的通信
- python - 如何为每个元素创建一个总和乘积的矩阵