【问题标题】:Is there a way to make a chat filter bot detect capital letters through code? (discord.py)有没有办法让聊天过滤器机器人通过代码检测大写字母? (discord.py)
【发布时间】:2021-04-15 23:50:42
【问题描述】:

我正在制作一个聊天过滤器机器人,但我不想进行所有可能的绕过。如果它有空格/符号,有没有办法让它查看消息并阻止它?任何帮助表示赞赏。

【问题讨论】:

    标签: discord discord.py


    【解决方案1】:

    这应该有效。我已尝试尽可能接近您的原始代码。

    import discord
    import random
    import string
    from discord.ext import commands
    
    client = commands.Bot(command_prefix = 'as-')
    
    with open("badwords2.txt") as file:
        bad_words = file.read().splitlines()
        
    def check_if_allowed(text):
    
        allowed_characters = string.ascii_uppercase + string.ascii_lowercase #creates a string containing all upper- and lower- case letters.
    
        allowed_characters += "1234567890 " # adds digits to the string
    
        for character in text: #iterates through every character in the input
            if character not in allowed_characters: #checks if the character is in the set of allowed characters, and returns false if not
                return False
    
        return True #returns true if every character in the input is allowed
        
    @client.event
    async def on_message(message):
        if not check_if_allowed(message.content):
            t = discord.Embed(color=0x039e00, title="Message Removed", description=":x:  Please only use alpha-numeric characters and spaces.")
            t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
            await message.channel.send(embed=t)
            await message.delete()
            return
        
        for bad_word in bad_words:
            if bad_word in message.content.lower().split(" "):
                t = discord.Embed(color=0x039e00, title="Message Removed", description=":x:   Please do not swear. Swearing can result in a mute or ban.")
                t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
                await message.channel.send(embed=t)
                await message.delete()
                return
    
    
    client.run('TOKEN')
    
    

    【讨论】:

    • 谢谢!我会检查一下现在是否有效:)
    • 我应该把它放在我的代码中的什么地方?我试图添加我的代码,但我的评论太长了,哈哈。
    • 把import语句和函数声明放在程序的最前面,像this这样调用函数
    • 应该是 on_message(message),而不是 on_recieve。
    • 好的。你有任何机会有不和谐吗?如果你这样做,我的标签是 TheSuperRobert2498#2498。和我联系
    猜你喜欢
    • 2022-01-16
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 2023-02-06
    • 2021-12-02
    • 2021-01-23
    • 1970-01-01
    相关资源
    最近更新 更多