【问题标题】:I'm trying to write a discord bot in python but my code is not working我正在尝试在 python 中编写一个不和谐的机器人,但我的代码不起作用
【发布时间】:2018-08-09 18:00:35
【问题描述】:

我正在尝试用 python 编写我的第一个 discord 机器人,我需要一些帮助来处理这段代码,请记住我是 python 新手,大约 2 周前我开始学习。

    @bot.event
async def on_message(message):
    content = message.content
    author = message.author
    if content == "example yes":
        bot.say("example @%s" % (author))

如果用户说“example yes”,我希望机器人写“example2 @user”

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您不是 awaiting bot.say,这不是您提及用户的方式(您使用 User.mention 属性代替)

    @bot.event
    async def on_message(message):
        content = message.content
        author = message.author
        if content == "example yes":
            await bot.say("example {}".format(author.mention))
    

    【讨论】:

    • 你能告诉我代码第六行的 await 是做什么的吗?
    • await 表示您的代码将等待协程完成,但如果其他代码同时运行也没关系。它是在PEP 492 中介绍的。如果这有点过于技术文档,我建议搜索涵盖协程、异步和等待的 Python 教程。另请参阅文档中的Tasks and Coroutines
    猜你喜欢
    • 2022-11-15
    • 2019-08-07
    • 2021-04-30
    • 2020-07-25
    • 2022-11-03
    • 2021-05-03
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多