#role
class MyClient(discord.Client):
async def on_ready(self):
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
self.role_message_id = 920038146673082439 # ID of the message that can be reacted to to add/remove a role.
self.emoji_to_role = {
discord.PartialEmoji(name=''): 811174119403421717,
discord.PartialEmoji(name=''): 863320935264026644,
discord.PartialEmoji(name=''): 863321497090261042
}
'''@client.event
async def on_raw_reaction_add(self, payload : discord.RawReactionActionEvent):
message_id = payload.message_id
if message_id == 918952147511545856-920038146673082439:
guild_id = payload.guild_id
guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)
if payload.emoji.name == '':
role = discord.utils.get(guild.roles, id='811174119403421717')
if role is not None:
member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
if member is not None:
await member.add_roles(role)
print("done")
else:
print("Member not found.")
else:
print("Role not found")
return
@client.event
async def on_raw_reaction_remove(self, payload : discord.RawReactionActionEvent):
message_id = payload.message_id
if message_id == 918952147511545856-920038146673082439:
guild_id = payload.guild_id
guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)
if payload.emoji.name == '':
role = discord.utils.get(guild.roles, id='811174119403421717')
if role is not None:
member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
if member is not None:
await member.add_removes(role)
print("done")
else:
print("Member not found.")
else:
print("Role not found") '''
async def on_raw_reaction_add(self, payload : discord.RawReactionActionEvent):
if payload.message_id == self.role_message_id:
channel = self.get_channel(payload.channel_id) # получаем объект канала
message = await channel.fetch_message(self.role_message_id) # получаем объект сообщения
member = utils.get(message.guild.members, id=payload.user_id) # получаем объект пользователя который поставил реакцию
return
guild = self.get_guild(payload.guild_id)
if guild is None:
print('666')
return
try:
role_id = self.emoji_to_role[payload.emoji] # объект выбранной роли (если есть)
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 payload.member.add_roles(role)
else:
await message.remove_reaction(payload.emoji, member)
print('[ERROR] Too many roles for user {0.display_name}'.format(member))
except KeyError:
print('111')
role = guild.get_role(role_id)
if role is None:
print('777')
return
try:
await payload.member.add_roles(role)
except discord.HTTPException:
print('999')
pass
async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent):
if payload.message_id != self.role_message_id:
print('888')
return
channel = self.get_channel(payload.channel_id) # получаем объект канала
message = await channel.fetch_message(self.role_message_id) # получаем объект сообщения
member = utils.get(message.guild.members, id=payload.user_id) # получаем объект пользователя который поставил реакцию
guild = self.get_guild(payload.guild_id)
if guild is None:
print('222')
return
try:
emoji = str(payload.emoji) # эмоджик который выбрал юзер
role_id = self.emoji_to_role[payload.emoji] # объект выбранной роли (если есть)
except KeyError:
print('333')
role = guild.get_role(role_id)
if role is None:
print('444')
return
member = guild.get_member(payload.user_id)
if member is None:
print('555')
return
try:
await member.remove_roles(role)
print('[SUCCESS]')
except KeyError:
print('[ERROR] KeyError, no role found for ' + emoji)
except Exception as e:
print(repr(e))
# RUN
client = MyClient(intents=intents)
keep_alive()
client.run(os.getenv("BOT_TOKEN"))
intents = discord.Intents(messages=True, guilds=True)
intents.reactions = True
discord.member = True
#role
@client.event
async def on_raw_reaction_add(payload : discord.RawReactionActionEvent):
if payload.message_id == config.POST_ID:
channel = client.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(payload: discord.RawReactionActionEvent):
channel = client.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))
keep_alive()
client.run(os.getenv("BOT_TOKEN"))