Discord.py: I get an AttributeError when I try to make my bot give roles on command

How do I make my bot give roles through a command? Nobody else asked this question and nobody else had an answer, I searched for more than an hour. What I want is the user to say "give me member" and then the bot gives the member role. This is the code:

import discord
import os
import random

client = discord.Client()

@client.event
async def on_ready():
    print ('logged in')

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content == 'give me member':
        role = discord.utils.get(message.author.server.roles, id="863082485851750431")
        await bot.add_roles(message.author, role)
client.run('EnterToken')

I get this error:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\USER\Desktop\Dbot2.py", line 19, in on_message
    role = discord.utils.get(message.author.server.roles, id="863082485851750431")
AttributeError: 'Member' object has no attribute 'server'

Once you have an answer please give me code I can copy and paste (don't include the token)

Upvotes: 0

Views: 40

Answers (1)

Abdulaziz
Abdulaziz

Reputation: 3426

server is an old method and no longer supported, and bot.add_roles does not right since bot is not even defined and it is old It should be like this.

You should always check the date of the code you are taking

role = discord.utils.get(message.guild.roles, id=863082485851750431)
await message.author.add_roles(role)

Docs:

Upvotes: 1

Related Questions