【问题标题】:Discord.py Delete message if the no no word is in itDiscord.py 如果消息中包含 no no 单词,则删除消息
【发布时间】:2022-06-18 23:41:24
【问题描述】:

如果消息中没有单词,我希望机器人删除该消息,但仅在仅发送该“单词”时才删除

示例:no no word 是“Hate”,但如果我发送“I Hate you”,它不会删除消息... 只有当我说“讨厌”时它才会删除 这是我的代码..请帮助:)


nono = ['Hate']

@client.event
@commands.has_permissions(manage_messages = False)
async def on_message(message):
    if message.author.id == client.user.id:
      return
    
    if message.guild is not None:
        for word in nono:
            if word in message.content:
                await message.delete()
                await message.author.send('I hate you too')

                await client.process_commands(message)
            else : 
                return

【问题讨论】:

  • 不起作用:)
  • 您为什么要使用@commands.has_permissions(manage_messages = False) 进行活动?这一切对我来说都很好。机器人删除了I Hate you,但没有删除I hate you,也许这是你的错误。
  • 奇怪....我使用@commands.has_permissions(manage_messages = False) 来处理一个事件是因为我希望只有拥有管理消息权限的人不会删除该消息

标签: discord.py


【解决方案1】:

您确定您在留言中写的是“我恨你”而不是“我恨你”吗?

我根据您的代码构建了一个最小的示例,它工作正常。

nono = ['Hate']
message = "I hate you"

for word in nono:
    m = message.upper()
    if word.upper() in m:
        print("delete")
    else:
        print("Nothing")

我还建议您将字符串大写,以便您的检测不区分大小写。

【讨论】:

  • 是的...我写的是“我恨你”而不是“我恨你”,而且我也尝试过使用上下的东西,但它也没有用
【解决方案2】:

尝试使用 json 文件,我个人制作了一些 automod 机器人,它们都使用 json。

这是一个例子:

 @commands.Cog.listener()
    async def on_message(self, message):
        user = message.author
        guild_id = message.guild.id

        em = discord.Embed(title="Profanity filter", description="Please refrain from using blacklisted words!", color=magenta)
        em.set_footer(text=user.name, icon_url=user.avatar_url)
        em.set_author(name=message.guild.name, icon_url=message.guild.icon_url)
        
        with open(f"json/{guild_id}/blacklist.json", 'r') as f:
            badwords = json.load(f)

        if any(word in message.content.lower() for word in badwords):
            await message.delete()
            return await message.channel.send(embed=em)

它会读取文件夹中与 discord 公会 ID 对应的名称的 json 文件。如果该单词在该文件中,则会将其删除。

这是 json 文件的样子:

{
"test": {
    "test": "test"
},
"test": {
    "test2": "test2"
}
}

【讨论】:

  • aite,让我试试看,谢谢你的建议:)
【解决方案3】:

更新:
我的朋友已经想通了。基本上,这是他的代码:

nono ['Hate']

for word in nono:
    if message.content.lower().find(word)!=-1:
        await message.delete()
        await message.author.send("I hate you too dummy!")

向他致敬:D

【讨论】:

  • 你确定任何消息方法都应该在message.delete()之后起作用吗?
  • 不知道。我是 python 新手,message.delete() 对我有用,所以我没有抱怨
【解决方案4】:

您很可能正在寻找the == operator

nono = ['Hate']

@client.event
@commands.has_permissions(manage_messages = False)
async def on_message(message):
    if message.author.id == client.user.id:
        return
    
    if message.guild is not None:
        for word in nono:
            if word == message.content: #If the message's content is EXACTLY and ONLY the word
                author = message.author 
#Storing the message's 'author' in this variable as 'message.author.send()' probably won't work 
#as mentioned in https://stackoverflow.com/questions/70290260/discord-py-delete-message-if-the-no-no-word-is-in-it/70290501#comment124313432_70307898
                await message.delete()
                await author.send('I hate you too')

                await client.process_commands(message)
            else : 
                return

您使用了in 而不是==== 检查 LHS(左侧)是否与 RHS(右侧)相同。 in in if word in message.content: 检查 LHS (word) 是否在 RHS (message.content) 中的 somewhere 并且 如果有除/之后/之前以外的其他东西,则不会打扰/与 LHS == 不同。

【讨论】:

    猜你喜欢
    • 2018-06-29
    • 2021-09-08
    • 2022-01-07
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多