Search code examples
pythonpython-3.xdiscorddiscord.py

Check if user is in txt list (discordpy)


as you can see below, I'm trying to check whether the ctx.author is = to a line in user.txt, I just want to check the txt file if it has the user and executes

fusers = open('users.txt', 'r')
users = fusers.readlines()

@user.event
async def on_message(ctx):
    if ctx.author == user:
        print("dm from user")
    else:
        print("no dm from user")

sorry for my grammer


Solution

  • users = [] #empty list to store all the users in 'users.txt'
    
    with open('users.txt') as userfile: #opens the users.txt file
        users = userfile.read().split('\n')
    
    @client.event
    async def on_message(ctx):
        if ctx.author in users: #if ctx.author is in users list
            print("dm from user")
        else:
            print("no dm from user")
    

    This code should achieve what you are trying to do. Comments are included too.