首页 > 解决方案 > 在 on_message 事件中,discord.py 机器人正在发送垃圾邮件

问题描述

discord.py 有问题。

@client.event
async def on_message(message):
    if message.author != message.author:
        return
    else:
        if message == "Buck" or "buck":
           await message.channel.send("Yes, he's the Chairman of Dallas!")

它基本上无限重复“是的,他是达拉斯的主席”,这可能会使我因滥用 api 而被禁止,并可能使我无法获得佣金。

标签: discorddiscord.py

解决方案


像这样的表达:

if x == "foo" or "bar" or "baz":

Python 是这样解释的:

if (x == "foo") or ("bar") or ("baz"):

如果第一个表达式(x == "foo")不为真,则第二个表达式为真("bar"),所以这个复合条件总是通过。

试试这个:

if x == "foo" or x == "bar" or x == "baz":

或者,甚至更好:

if x in ("foo", "bar", "baz"):

此外,您正在比较整个discord.Message实例,而不仅仅是内容。

修复您的代码:

if message.content in ("Buck", "buck"):

推荐阅读