为什么 discord.py 一直在发送消息? [英] why does discord.py keep sending messages?

查看:26
本文介绍了为什么 discord.py 一直在发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个事件,如果用户发送特定消息,则该事件会发回消息.前.如果用户说你好",机器人会说你好".我发现在下面的代码中,第一个 elif 有一个 or..so 如果用户说 ddos​​ 或 hack,它会进入无限循环并继续发送 你不能这么说!.如果用户输入你好,机器人发送一次你好,然后进入无限循环并发送你不能这么说!.任何想法为什么?

i made an event that sends a message back if a user sends a specific message. ex. if user says 'hello' the bot says 'hello there'. I found that in the code below the first elif has an or..so if the user says ddos or hack it goes in an infinite loop and keep sending you can't say that!.If the user enters hello the bot sends hello there once then goes in an infinite loop and sends you can't say that!. Any ideas why?

@client.event
async def on_message(message):
    msg = message.content.lower()
    if str(msg) == "hello":
        await message.channel.send('hello there')
    elif str(msg) == "hack" or "ddos":
        await message.channel.send("you can't say that!")
    else:
        return

推荐答案

你的代码有一些错误.第一个,你必须检查消息作者是否是机器人,所以 on_message 事件不会检查机器人的消息.您可以通过 discord.Member.bot 进行查看.如果成员是机器人,这将返回 True.

Your code has a few mistakes. First one, you have to check if message author is bot, so on_message event won't check bot's messages. You can check it by discord.Member.bot. This will return True if member is bot.

@client.event
async def on_message(message):
    if message.author.bot:
        return

第二个是,elif str(msg) == "hack";或ddos": 不等于 elif str(msg) == hack";或 str(msg) == "ddos":.

Second one is, elif str(msg) == "hack" or "ddos": is not equal to elif str(msg) == "hack" or str(msg) == "ddos":.

elif str(msg) == "hack";或ddos": 表示如果 msg 等于 hackddos 存在.此外,您不必执行 str(msg).message.content 返回 str 类型的对象.

elif str(msg) == "hack" or "ddos": means if msg equals to hack or ddos exists. Also, you don't have to do str(msg). message.content returns str type object.

@client.event
async def on_message(message):
    if message.author.bot:
        return
    msg = message.content.lower()
    if msg == "hello":
        await message.channel.send('hello there')
    elif msg == "hack" or msg == "ddos":
        await message.channel.send("you can't say that!")

编辑:

您可以使用 if msg in list: 来检查消息内容是否在单词列表中.这是一个例子:

You can use if msg in list: to check if message content is in a list of words. Here's an example:

if msg in ['hack', 'ddos']:
    await message.channel.send("you can't say that!")

这将执行相同的操作:

if msg == 'hack' or msg == 'ddos':
    await message.channel.send("you can't say that!")

这篇关于为什么 discord.py 一直在发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆