ColdnessAwaits
ColdnessAwaits

Reputation: 31

discord.py :: How would I make a command to add a word to a filter?

I currently have this bit of code to have a filter system.

This is at the top of my code;

with open('filter.txt', 'r') as f:
    global filter
    words = f.read()
    filter = words.split()

While this is in my on_message part of my code;

@client.event
async def on_message (message):
msg = message.content
    for word in filter:
        if word in msg:
            await message.delete()

I'm currently going into the .txt file to add words to the filter, and I'm trying to figure out how to make a command that my moderators can use to add a word to the filter if I'm not around to access the .txt file itself.

How would I do this?

Upvotes: 1

Views: 258

Answers (1)

flyingbyte
flyingbyte

Reputation: 79

You can create a command that allows moderators to add/append to the list in your filter file.

It could look something like this:

# Instantiates command
@client.command(name="filter", help="Adds word to filter")
# Only users with role id can use command
@commands.has_any_role(id for moderators)
async def appendFilterList(ctx, word):
    # Opens file to Append
    filter = open("filter.txt", "a")
    # Appends word to list
    filter.write(word+"\n")
    filter.close()

Upvotes: 1

Related Questions