Skip to content
Advertisement

Where to find discord.py bot in self attributes in order to assign roles

I am the owner of a Discord server that doesn’t have too many people so I want them to introduce themselves so I can keep track of who’s who. Upon joining, members only have access to a #introductions channel. When they type in their name, they are given the Member role.

There isn’t any discord.py function that does this, so I have to split this into two parts: first, get the author of the most recent message, and, second, assign them the Member role. The first part is not difficult at all, but the second part throws me an error.

Code

class UtilBot(commands.Bot):
    def __init__(self, *, command_prefix, name):
        commands.Bot.__init__(
            self, command_prefix=command_prefix, self_bot=False)
        with open(r'.docschannels.csv') as channels:
            channel_ids = list(csv.reader(channels, delimiter='t'))
            for i, item in enumerate(channel_ids):
                channel_ids[i] = [int(item[0]), item[1]]
            self.channels = dict(channel_ids)
        self.channel_commands = {
            '#introductions': self.introduction,
            '#dev-build': self.introduction} #Allow for feature testing
        self.name = name

    async def on_ready(self):
        print(f"Utils is running")

    async def on_message(self, message):
        channel = self.channels[message.channel.id]
        print(channel)
        try:
            self.channel_commands[channel](message)
        except KeyError:
            pass

    def introduction(self, message):
        member = message.author
        role = discord.utils.get(member.guild.roles, name="Member")
        self.add_roles(member, role)

    def dev_build(self, message):
        print(f'{message.author.name} ({self.channels[message.channel.id]})')
        print(message.content)

Error

AttributeError: 'UtilBot' object has no attribute 'add_roles'

I am asking both how to fix this error while maintaining an OOP framework and if there is an alternative method that completes the same task. If I know where to find the self attribute that corresponds to the bot I can easily fix this, but when looking at self.__dict__, I cannot find it.

Advertisement

Answer

Instead of self.add_roles(member, role), try member.add_roles(role)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement