Discord.py wait_for()
Discord.py wait_for()
我有一个 on_message()
事件,但我想转入命令,问题是当我更改它时,wait_for()
功能不起作用。
@client.command()
中有 wait_for()
的等价物吗?
我的代码:
@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)```
首先,该视频显示了您的机器人的登录令牌。这是一个只有您知道的密钥,我建议 删除该视频并 here 立即 更改密钥!有权访问该密钥的人可以控制您的机器人。
在您为回复 Patrick Haugh 的评论而发送的视频中,您使用了 discord.ext.commands 框架,所以这就是我将在此处使用的框架。
order
命令中的 ctx
参数是 Context,而不是像在视频中那样在频道上调用 send()
,您应该在上下文中调用它,例如这个:ctx.send('foo')
。
此外,在第 18 行和第 21 行,您使用
await client.wait_for('ctx', check=check)
如 here, the events you can use as the first argument of Client.wait_for()
are given in the event reference 所述,只需删除 on_
前缀。在你的情况下,我想你想要 on_message
,所以它看起来像这样:
@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.content
获取这些消息的内容分别。
我有一个 on_message()
事件,但我想转入命令,问题是当我更改它时,wait_for()
功能不起作用。
@client.command()
中有 wait_for()
的等价物吗?
我的代码:
@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)```
首先,该视频显示了您的机器人的登录令牌。这是一个只有您知道的密钥,我建议 删除该视频并 here 立即 更改密钥!有权访问该密钥的人可以控制您的机器人。
在您为回复 Patrick Haugh 的评论而发送的视频中,您使用了 discord.ext.commands 框架,所以这就是我将在此处使用的框架。
order
命令中的 ctx
参数是 Context,而不是像在视频中那样在频道上调用 send()
,您应该在上下文中调用它,例如这个:ctx.send('foo')
。
此外,在第 18 行和第 21 行,您使用
await client.wait_for('ctx', check=check)
如 here, the events you can use as the first argument of Client.wait_for()
are given in the event reference 所述,只需删除 on_
前缀。在你的情况下,我想你想要 on_message
,所以它看起来像这样:
@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.content
获取这些消息的内容分别。