Advertisement
misamisk

bot

Mar 1st, 2021
1,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. # config.py
  2.  
  3. # ID of message with reaction roles
  4. reaction_message_id=816005108306935839
  5.  
  6. # bot token
  7. token='hidden_token'
  8.  
  9. roles={
  10.     ":ice_cube:": "816004965688541224",
  11.     ":loudspeaker:": "816004985720012801",
  12.     ":bell:": "816005011896533017"
  13. }
  14.  
  15.  
  16.  
  17. # bot.py
  18.  
  19. import discord
  20. from discord import utils
  21. import config
  22. import sys
  23.  
  24. print("Python version: {0}".format(sys.version))
  25.  
  26.  
  27. class MyClient(discord.Client):
  28.     async def on_ready(self):
  29.         print('Logged in as {0}'.format(self.user))
  30.         pass
  31.  
  32.     async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent):
  33.         if payload.message_id == config.reaction_message_id:
  34.             channel = self.get_channel(payload.channel_id)
  35.             message = await channel.fetch_message(payload.message_id)
  36.             user = self.get_user(payload.user_id)
  37.  
  38.             print(user)
  39.  
  40.             try:
  41.                 emoji = config[payload.emoji]
  42.                 role = utils.get(message.guild.roles, id=config.roles[emoji])
  43.                 await user.add_roles(role)
  44.                 print('[SUCCESS] User {0.display_name} has been granted with role {1.name}'.format(user, role))
  45.  
  46.             except KeyError as e:
  47.                 print('[ERROR] KeyError, no role found for ' + emoji)
  48.             except Exception as e:
  49.                 print(repr(e))
  50.  
  51.     async def on_raw_reaction_remove(self, payload):
  52.         channel = self.get_channel(payload.channel_id)
  53.         message = await channel.fetch_message(payload.message_id)
  54.         user = utils.get(message.guild.members, id=payload.user_id)
  55.  
  56.         print(user)
  57.  
  58.         try:
  59.             emoji = str(payload.emoji)
  60.             role = utils.get(message.guild.roles, id=config.roles[emoji])
  61.  
  62.             await user.remove_roles(role)
  63.             print('[SUCCESS] Role {1.name} has been removed for user {0.display_name}'.format(user, role))
  64.  
  65.         except KeyError as e:
  66.             print('[ERROR] KeyError, no role found for ' + emoji)
  67.         except Exception as e:
  68.             print(repr(e))
  69.  
  70.  
  71.  
  72. client = MyClient()
  73. client.run(config.token)
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement