ToujoursTitou
ToujoursTitou

Reputation: 31

Creating a black-listed words list with discord.py

I have created a list of black-listed words in an external text file which can be modified with a simple command. The problem is that I want my bot to be able to be used by all servers, and when a server modifies the file, the modification is affected for all servers that added the bot. What can I do?

The auto-moderation event:

@client.event
async def on_message(message):
    username = message.author.display_name
    msg = message.content
    with open('blacklist.txt', 'r') as f:
        blacklist = f.read()
        if message.author.bot:
            return
        elif msg.lower() in blacklist.split():
            await message.delete()
        else:
            await client.process_commands(message)

The command I use to add more words to the list:

@client.command()
async def banword(ctx, arg):
    if ctx.message.author.guild_permissions.administrator:
        text = arg
        file = open('blacklist.txt', 'a+')
        file.write("\n " + text)
        em = discord.Embed(title="banword <arg>",
                           description=f"{ctx.author.mention}, '{text}' has been added to the word's blacklist.",
                           color=discord.Colour.green())
        await ctx.send(embed=em)
    else:
        permission = "administrator"
        em = discord.Embed(title="Permissions Required!",
                           description=f"{ctx.author.mention}, You need the permission '{permission}' to use this command.",
                           color=discord.Colour.green())
        await ctx.send(embed=em)

The command I use to clear the list:

@client.command()
async def clearword(ctx):
    if ctx.message.author.guild_permissions.administrator:
        file = open('blacklist.txt', 'w')
        em = discord.Embed(title="clearword",
                           description=f"{ctx.author.mention}, Word's blacklist has been cleared.",
                           color=discord.Colour.green())
        await ctx.send(embed=em)
    else:
        permission = "administrator"
        em = discord.Embed(title="Permissions Required!",
                           description=f"{ctx.author.mention}, You need the permission '{permission}' to use this command.",
                           color=discord.Colour.green())
        await ctx.send(embed=em)

(My code has no error, I just need an alternative to achieve this)

Upvotes: 0

Views: 846

Answers (1)

Toastlover
Toastlover

Reputation: 81

This is just a bad Solution. Using a Database for this is way better. you will have better performance etc. You should take a look at MongoDB it's really easy to post values in the database and read from them later on. so you have a command similar to !addword which then post the word to the database. later you check if the word is in the database and if so, you'll delete it from the discord chat and give some blacklisted word as response.

Upvotes: 0

Related Questions