【问题标题】:Command Glitch Discord Py命令故障 Discord Py
【发布时间】:2022-01-18 04:13:03
【问题描述】:

每当成员发送服务器内禁止的单词或短语时,我的机器人都会发送一条消息。尽管在发送消息后,它会导致机器人出现故障并开始发送相同的消息,但它不会删除一次,而是发送多个重复的消息并提及自己并发送消息“我的前缀是 *”

代码如下:

@bot.listen('on_message') 
async def on_message(message):
    messageAuthor = message.author

    if bannedWords != None and (isinstance(message.channel, discord.channel.DMChannel) == False):
        for bannedWord in bannedWords:
            if msg_contains_word(message.content.lower(), bannedWord):
                await message.delete()
                await message.channel.send(f"{messageAuthor.mention} avoid saying ||{bannedWord}|| within the server :)")

@bot.listen('on_message') 
async def on_message(message):
    if bot.user.mentioned_in(message):
        await message.channel.send(f"My prefix is {prefix}")

【问题讨论】:

  • 因为机器人在警告中发送禁用词并自行触发。
  • 我该如何解决?
  • 添加检查以查看作者是否为机器人。如果是则忽略它。

标签: python discord discord.py


【解决方案1】:

检查机器人是否对自己的消息做出反应,如果是,则 return None 从而终止函数而不是进一步处理。

你这样做:

if message.author == bot.user:
    return

这就是您在代码中实现它的方式:

@bot.listen("on_message")
async def on_message(message):

    if message.author == bot.user:
        return

    messageAuthor = message.author

    if bannedWords != None and (
        isinstance(message.channel, discord.channel.DMChannel) == False
    ):
        for bannedWord in bannedWords:
            if msg_contains_word(message.content.lower(), bannedWord):
                await message.delete()
                await message.channel.send(
                    f"{messageAuthor.mention} avoid saying ||{bannedWord}|| within the server :)"
                )


@bot.listen("on_message")
async def on_message(message):

    if message.author == bot.user:
        return

    if bot.user.mentioned_in(message):
        await message.channel.send(f"My prefix is {prefix}")

【讨论】:

    猜你喜欢
    • 2021-05-31
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 2021-09-06
    • 2022-01-12
    • 2021-06-28
    • 2021-09-04
    相关资源
    最近更新 更多