首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何读不和谐的一行

如何读不和谐的一行
EN

Stack Overflow用户
提问于 2020-10-04 21:17:30
回答 1查看 40关注 0票数 0

我正在为不一致编写一个机器人,有一个命令($ bone),机器人在其中写下了以下内容:“从1-9中选择一个数字”,然后用户应该回答,这是我如何读取他写数字的行?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-05 01:06:03

正如评论中提到的,我们将使用一个名为wait_for的协程,它等待像messagehttps://discordpy.readthedocs.io/en/latest/api.html#discord.Client.wait_for这样的事件

因为这些事件与我们可能使用bot.event连接到的事件相同,所以我们将能够处理返回的消息。

代码语言:javascript
复制
@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!")

真实的交互示例来证明它是有效的:

代码语言:javascript
复制
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!
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64195093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档