简体   繁体   中英

Discord Py / How to remove a specific role from a server user

I plan to manage information of server personnel in conjunction with Google Spread. And I want to remove the role from the server personnel by entering the name of the role. Check the typos.

The part of the spreadsheet is complete, but it has not removed its role from the discord, has tried several searches, but it does not help, leaving questions.

I need help.

This is my code.

@bot.command()
async def removerole(ctx, team_name):
    role = get(ctx.guild.roles, name=team_name)
    team_mem = []
    # type error check
    role_list = ["TEAM_A", "TEAM_B", "TEAM_C", "TEAM_D"]
    if role.name in role_list:
        type_error = 1
    else:
        type_error = 0
    cell_max = worksheet_list.acell('A1').value
    range_list = worksheet_list.range('H2:H' + cell_max)

    if type_error == 1:
        for i, cell in enumerate(range_list):
            #print(str(i) + " / " + cell.value)
            if str(cell.value) == team_name:
                temp = i + 2
                data = worksheet_list.acell('H' + str(temp)).value
                worksheet_list.update_acell('H' + str(temp), 'Any')
                #print(temp, data)
                if data == role:
                    team_mem.append(worksheet_list.acell('C' + str(temp)).value)
        #await ctx.message.delete()
        await ctx.send(content=f"{team_name} role has been removed")
    else:
        await ctx.send("Check the typos.")

    empty = True
    for member in ctx.guild.members:
        if role in member.roles:
            await ctx.guild.member.remove_roles(role)
            empty = False
    if empty == False:
        await ctx.send("Anyone has this role")

You are not removing it right, you should reference to member not ctx.guild.member since you already got the member from the for loop.

for member in ctx.guild.members:
    if role in member.roles:
        await member.remove_roles(role)

Note: Make sure to enable intents.

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