0
votes

I want to create a discord bot that, after receiving a message from the admin, changes the role of all server members (excluding the admin) from @everyone to @newrole.

I tried this code but there is an error:

role = discord.utils.get(server.roles, name="newrole")
AttributeError: 'NoneType' object has no attribute 'roles'

I use the latest version of discord and Python 3.6.

server = client.get_guild(int(server-id))

@client.event
async def on_message(message):
if message.content.startswith('change role'):
    await message.author.send("Change the role from @everyone to @newrole")
    role = discord.utils.get(server.roles, name="newrole")
    for member in server.members:
        await member.add_roles(role)
1

1 Answers

0
votes

All members have the everyone role at all times. Members can have multiple roles, however, so you can also give them your new role. There is no coroutine for bulk assigning roles, so you have to do it member by member:

@client.event
async def on_message(message):
    if message.content.startswith('change role'):
        await message.author.send("Change the role from @everyone to @newrole")
        role = discord.utils.get(message.guild.roles, name="newrole")
        for member in message.guild.members:
            await member.add_roles(role)