Discord.py wait_for() [英] Discord.py wait_for()
问题描述
我有一个 on_message()
事件,但是我想上交一个命令,问题是当我更改它时, wait_for()
函数不会不行在 @ client.command()
中是否有等效的 wait_for()
?
I have an on_message()
event, but I want to turn in into a command, the problem is that when I change it, the wait_for()
function doesn't work.
Is there any equivalent of wait_for()
in @client.command()
?
我的代码:
@client.event
async def on_message(message):
channel = message.author
def check(m):
return m.channel == message.channel and m.author != client.user
if message.content.startswith("!order"):
await channel.send("in game name")
in_game_name = await client.wait_for('message', check=check)
await channel.send("in game ID")
in_game_ID = await client.wait_for('message', check=check)
else:
await client.process_commands(message)```
推荐答案
首先,该视频显示了您的漫游器的登录令牌.这应该是只有您自己知道的秘密密钥,我建议删除该视频并更改密钥
First things first, that video shows your bot's login token. This is meant to be a secret key known only to you, and I would recommend taking down that video and changing the key here immediately! People with access to that key can take control your bot.
在您回复Patrick Haugh的评论的视频中,您使用了discord.ext.commands框架,因此我将在这里使用它.您的 order
命令中的 ctx
参数是
In the video that you sent in reply to a comment from Patrick Haugh, you used the discord.ext.commands framework, so that is what I will be using here.
The ctx
parameter in your order
command is a Context, and instead of calling send()
on the channel like you do in your video, you should call it on the context like this: ctx.send('foo')
.
此外,在第18和21行上,您使用
Also, on lines 18 and 21, you use
await client.wait_for('ctx', check=check)
此处中所述,您可以在 on _
前缀即可.在您的情况下,我认为您需要 on_message
,所以它看起来像这样:
As explained here, the events you can use as the first argument of Client.wait_for()
are given in the event reference, just remove the on_
prefix. In your case, I think you want on_message
, so it would look something like this:
@client.command()
async def order(self, ctx: commands.Context):
def check(message: discord.Message):
return message.channel == ctx.channel and message.author != ctx.me
await ctx.send('foo')
foo = await client.wait_for('message', check=check)
await ctx.send('bar')
bar = await client.wait_for('message', check=check)
在这种情况下, foo
和 bar
是 discord.Message
,然后您可以使用 foo.content
和 bar获取这些消息的内容.内容
.
In this case, foo
and bar
are instances of discord.Message
, and you can get the content of those messages using foo.content
and bar.content
respectively.
这篇关于Discord.py wait_for()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!