Created
July 14, 2022 13:17
-
-
Save Blake-McCullough/320ba2f45a55581e9970a1cee54ac45a to your computer and use it in GitHub Desktop.
An updated version of https://gist.github.com/makupi/c508c9d33bb01dcc04e57d1a93c23ae1#file-reactionroles-discord-py , any questions just ask or join the discord! https://discord.gg/X25F6Sfpq7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Edited by Blake McCullough, original by makupi | |
| #Discord - Spoiled_Kitten#4911 | |
| #Discord Server - https://discord.gg/X25F6Sfpq7 | |
| #Github - https://github.com/Blake-McCullough/ | |
| #Website - https://blakemccullough.com/ | |
| #Email - [email protected] | |
| #Twitch - https://www.twitch.tv/spoiled_kitten_ | |
| import discord | |
| from discord.ext import commands | |
| import json | |
| import atexit | |
| import uuid | |
| reaction_roles_data = {} | |
| try: | |
| with open("reaction_roles.json") as file: | |
| reaction_roles_data = json.load(file) | |
| except (FileNotFoundError, json.JSONDecodeError) as ex: | |
| with open("reaction_roles.json", "w") as file: | |
| json.dump({}, file) | |
| @atexit.register | |
| def store_reaction_roles(): | |
| with open("reaction_roles.json", "w") as file: | |
| json.dump(reaction_roles_data, file) | |
| class ReactionRoles(commands.Cog): | |
| reaction_roles= discord.SlashCommandGroup("reactionroles", "For using reaction roles!") | |
| def __init__(self, bot): | |
| self.bot = bot | |
| @commands.Cog.listener() | |
| async def on_ready(self): | |
| print(f"ReactionRoles ready.") | |
| @commands.Cog.listener() | |
| async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent): | |
| role, user = self.parse_reaction_payload(payload) | |
| if role is not None and user is not None: | |
| await user.add_roles(role, reason="ReactionRole") | |
| @commands.Cog.listener() | |
| async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent): | |
| role, user = self.parse_reaction_payload(payload) | |
| if role is not None and user is not None: | |
| await user.remove_roles(role, reason="ReactionRole") | |
| @commands.has_permissions(manage_channels=True) | |
| @reaction_roles.command(name="create",description="To create a new reaction role.") | |
| async def reaction( | |
| self, | |
| ctx, | |
| emote, | |
| role: discord.Role, | |
| channel: discord.TextChannel, | |
| title, | |
| message, | |
| ): | |
| embed = discord.Embed(title=title, description=message) | |
| msg = await channel.send(embed=embed) | |
| await msg.add_reaction(emote) | |
| self.add_reaction(ctx.guild.id, emote, role.id, channel.id, msg.id) | |
| await ctx.respond(f"Added Reaction {msg.id}") | |
| @commands.has_permissions(manage_channels=True) | |
| @reaction_roles.command(name="list",description="To list currently active reactions in your server.") | |
| async def reactions(self, ctx): | |
| guild_id = ctx.guild.id | |
| data = reaction_roles_data.get(str(guild_id), None) | |
| embed = discord.Embed(title="Reactions") | |
| if data is None: | |
| embed.description = "There are no reaction roles set up right now." | |
| else: | |
| for index, rr in enumerate(data): | |
| emote = rr.get("emote") | |
| role_id = rr.get("roleID") | |
| role = ctx.guild.get_role(role_id) | |
| channel_id = rr.get("channelID") | |
| message_id = rr.get("messageID") | |
| embed.add_field( | |
| name=index, | |
| value=f"{emote} - @{role} - [message](https://www.discordapp.com/channels/{guild_id}/{channel_id}/{message_id})", | |
| inline=False, | |
| ) | |
| await ctx.respond(embed=embed) | |
| @commands.has_permissions(manage_channels=True) | |
| @reaction_roles.command(name = "remove",description = "To remove a reaction role.") | |
| async def reaction_remove(self, ctx, index: int): | |
| guild_id = ctx.guild.id | |
| data = reaction_roles_data.get(str(guild_id), None) | |
| embed = discord.Embed(title=f"Remove Reaction Role {index}") | |
| rr = None | |
| if data is None: | |
| embed.description = "Given Reaction Role was not found." | |
| else: | |
| embed.description = ( | |
| "Do you wish to remove the reaction role below? Please react with ๐๏ธ." | |
| ) | |
| rr = data[index] | |
| emote = rr.get("emote") | |
| role_id = rr.get("roleID") | |
| role = ctx.guild.get_role(role_id) | |
| channel_id = rr.get("channelID") | |
| message_id = rr.get("messageID") | |
| _id = rr.get("id") | |
| embed.set_footer(text=_id) | |
| embed.add_field( | |
| name=index, | |
| value=f"{emote} - @{role} - [message](https://www.discordapp.com/channels/{guild_id}/{channel_id}/{message_id})", | |
| inline=False, | |
| ) | |
| msg = await ctx.send(embed=embed) | |
| if rr is not None: | |
| await msg.add_reaction("๐๏ธ") | |
| def check(reaction, user): | |
| return ( | |
| reaction.message.id == msg.id | |
| and user == ctx.author | |
| and str(reaction.emoji) == "๐๏ธ" | |
| ) | |
| reaction, user = await self.bot.wait_for("reaction_add", check=check) | |
| data.remove(rr) | |
| reaction_roles_data[str(guild_id)] = data | |
| store_reaction_roles() | |
| def add_reaction(self, guild_id, emote, role_id, channel_id, message_id): | |
| if not str(guild_id) in reaction_roles_data: | |
| reaction_roles_data[str(guild_id)] = [] | |
| reaction_roles_data[str(guild_id)].append( | |
| { | |
| "id": str(uuid.uuid4()), | |
| "emote": emote, | |
| "roleID": role_id, | |
| "channelID": channel_id, | |
| "messageID": message_id, | |
| } | |
| ) | |
| store_reaction_roles() | |
| def parse_reaction_payload(self, payload: discord.RawReactionActionEvent): | |
| guild_id = payload.guild_id | |
| data = reaction_roles_data.get(str(guild_id), None) | |
| if data is not None: | |
| for rr in data: | |
| emote = rr.get("emote") | |
| if payload.message_id == rr.get("messageID"): | |
| if payload.channel_id == rr.get("channelID"): | |
| if str(payload.emoji) == emote: | |
| guild = self.bot.get_guild(guild_id) | |
| role = guild.get_role(rr.get("roleID")) | |
| user = guild.get_member(payload.user_id) | |
| return role, user | |
| return None, None | |
| def setup(bot): | |
| bot.add_cog(ReactionRoles(bot)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment