N
N
Nikolai Kalinkin2022-02-13 21:49:12
Bots
Nikolai Kalinkin, 2022-02-13 21:49:12

Discord Bot issuing a role on the required message?

import discord
from discord import utils
import const
from discord.ext import commands
class MyClient(discord.Client):
    async def on_ready(self):
        print('Загрузился {0}!'.format(self.user))
    async def on_message(self, message):
        if message.content.startswith('1'):
            channel = message.channel
            user=message.author.id
            await bot.add_roles(user,898612321755230260)
bot = MyClient()
bot.run(const.TOKEN)

AttributeError: 'MyClient' object has no attribute 'add_roles' error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2022-02-14
@koko_goose

add_roles is a method of the discord.Member class, not discord.Client. Accordingly, it must be called from the correct object. In this case, the easiest way to get the Member instance of the sender of this message is to refer to message.author:
await message.author.add_roles(role)
In this case, role should not be a role id, but a discord.Role object. Which can be obtained by the get_role method of the discord.Guild class. And for Member it can be obtained from the guild field.
As a result, it turns out that we need:

author = message.author
guild = author.guild
role = guild.get_role(898612321755230260)
await author.add_roles(role)

Or if put together in one line:
await message.author.add_roles(message.author.guild.get_role(898612321755230260))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question