我正在为不一致编写一个机器人,有一个命令($ bone),机器人在其中写下了以下内容:“从1-9中选择一个数字”,然后用户应该回答,这是我如何读取他写数字的行?
发布于 2020-10-05 01:06:03
正如评论中提到的,我们将使用一个名为wait_for的协程,它等待像message:https://discordpy.readthedocs.io/en/latest/api.html#discord.Client.wait_for这样的事件
因为这些事件与我们可能使用bot.event连接到的事件相同,所以我们将能够处理返回的消息。
@bot.command()
async def bone(ctx):
await ctx.send("Select a number 1-9")
def my_check_func(message): #A checking function: When we return True, consider our message "passed".
if message.author == ctx.author: #Make sure we're only responding to the original invoker
return True
else:
return False
response = await bot.wait_for("message", check=my_check_func) #This waits for the first message event that passes the check function.
#This execution process now freezes until a message that passes that check is sent,
#but other bot interactions can still happen.
c = response.content #equal to message.content, because response IS a message
if c.isdigit() and 1 <= int(c) <= 9: #if our response is a number equal to 1, 9, or between those...
await ctx.send("You sent: {}".format(response.content))
else:
await ctx.send("That's not a number between 1 and 9!")真实的交互示例来证明它是有效的:
Cook1
$bone
Tutbot
Select a number 1-9
Cook1
4
Tutbot
You sent: 4
Cook1
$bone
Tutbot
Select a number 1-9
Cook1
no
Tutbot
That's not a number between 1 and 9!https://stackoverflow.com/questions/64195093
复制相似问题