简体   繁体   中英

Why is my list not updating despite appending to the list?

I have a bit of a problem with my code below. After calling the command, the list does not update (it doesn't append the discord id of the user I mentioned to the list.) Even though in the print statement that I added after the code executes SHOWS the user's id in the list, but it's not.

cool_people = []
allowed_people = [1232332423472348]
@client.command()
async def makeCool(ctx, member : discord.Member):
    for allowed in allowed_people:
        if int(ctx.author.id) == allowed:
            print("hey u are allowed to use this command! good job")
            for coolUser in cool_people:
                if coolUser == member.id:
                    await ctx.send(f'{member.mention} you are already cool!')
                elif coolUser != member.id:
                    print(member.id)
                    await ctx.send(f"{member.mention} has been added")
                    cool_people.append(member.id)
                    print(cool_people)
  

 

You should consider using in instead of looping the entire list. In addition make the list global one method is using client.XXXX

# Made it global
client.cool_people = []
client.allowed_people = [1232332423472348]

@client.command()
async def makeCool(ctx, member : discord.Member):
    # skip users not in allowed_people
    if ctx.author.id not in client.allowed_people:
        return await ctx.send("You are not allowed to use this")
    
    # add user to list if not in cool_pepole
    if member.id not in client.cool_people:
        client.cool_people.append(member.id)

    # code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM