M
M
Michalitch2021-12-12 22:42:53
Python
Michalitch, 2021-12-12 22:42:53

I can't add a role through reaction, how to fix it?

import discord
import os
from discord import utils
from discord.ext import commands
from worker import keep_alive
intents = discord.Intents.all()
discord.member = True
import config

PREFIX = '!'
client = commands.Bot(command_prefix = PREFIX, intents = intents)
client.remove_command('help')

@client.event
async def on_raw_reaction_add(self, payload : discord.RawReactionActionEvent):
    if payload.message_id == config.POST_ID:
      channel = self.get_channel(payload.channel_id) # получаем объект канала
      message = await channel.fetch_message(payload.message_id) # получаем объект сообщения
      member = utils.get(message.guild.members, id=payload.user_id) # получаем объект пользователя который поставил реакцию
      
      try:
        emoji = str(payload.emoji) # эмоджик который выбрал юзер
        role = utils.get(message.guild.roles, id=config.ROLES[emoji]) # объект выбранной роли (если есть)
      
        if(len([i for i in member.roles if i.id not in config.EXCROLES]) <= config.MAX_ROLES_PER_USER):
          await member.add_roles(role)
          print('[SUCCESS] User {0.display_name} has been granted with role {1.name}'.format(member, role))
          
        else:
          await message.remove_reaction(payload.emoji, member)
          print('[ERROR] Too many roles for user {0.display_name}'.format(member))
      
      except KeyError:
        print('[ERROR] KeyError, no role found for ' + emoji)
      except Exception as e:
        print(repr(e))

async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent):
    channel = self.get_channel(payload.channel_id) # получаем объект канала
    message = await channel.fetch_message(payload.message_id) # получаем объект сообщения
    member = utils.get(message.guild.members, id=payload.user_id) # получаем объект пользователя который поставил реакцию

    try:
      emoji = str(payload.emoji) # эмоджик который выбрал юзер
      role = utils.get(message.guild.roles, id=config.ROLES[emoji]) # объект выбранной роли (если есть)

      await member.remove_roles(role)
      print('[SUCCESS] Role {1.name} has been remove for user {0.display_name}'.format(member, role))

    except KeyError:
      print('[ERROR] KeyError, no role found for ' + emoji)
    except Exception as e:
      print(repr(e))

intents = discord.Intents.default()
intents.members = True
keep_alive()
client.run(os.getenv("BOT_TOKEN"))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-12-13
@Mikhalitch

one.

async def on_raw_reaction_add(self, payload : discord.RawReactionActionEvent):

Where does self come from, the function is not in the class?
2.
intents = discord.Intents.default()
intents.members = True
keep_alive()
client.run(os.getenv("BOT_TOKEN"))

Created an intents object, but don't pass it to the bot in client.run(), and don't use it at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question