import os import uuid import discord from discord.ext import commands from data import react_roles as db_react_roles from react_roles import react_roles as rr from util.console import console, panel, track_iterable as track GUILD_IDS = [int(os.getenv("DEV_GUILD_ID"))] # Description of the reaction message cache reaction_message_descriptions_cache = {} async def check_server_instantiated(ctx: discord.ApplicationContext): """ Check if the server has been instantiated. """ if not db_react_roles.guild_exists(ctx.guild.id): await ctx.send("Server not instantiated. Please run the `/instantiate_server` command first.") return False return True class ReactRoles(commands.Cog): def __init__(self, bot): self.bot = bot ### COMMANDS ### # Command to create a new reaction role message @commands.slash_command(name="new_reaction_message", description="Create a new reaction message", guild_ids=GUILD_IDS) @commands.has_permissions(manage_roles=True) async def new_reaction_message(self, ctx: discord.ApplicationContext, description: str, thumbnail_url: str = None): # Check if server has been instantiated await check_server_instantiated(ctx) # Since we can't get the message ID until we send the message, but need to display in the message itself # some kind of unique identifier, we will create a UUID to display in the message, send the message, and then # add it all to the database. # Send Message to Channel - Must happen first so that we can get the message ID for the database unique_id = str(uuid.uuid4()) embed = rr.generate_reaction_message_embed(description, thumbnail_url, unique_id) message = await ctx.channel.send(embed=embed) # Add the reaction_message to the database db_react_roles.check_and_add_reaction_message(unique_id, ctx.guild.id, ctx.channel.id, message.id, description, thumbnail_url) # Ensure the message was put in the database correctly, otherwise delete the message if not db_react_roles.reaction_message_exists(ctx.guild.id, ctx.channel.id, message.id): await message.delete() await ctx.respond("Failed to create the reaction message. Please try again.", ephemeral=True) return await ctx.respond(f"Reaction message created successfully!", ephemeral=True) @commands.slash_command(name="delete_reaction_message", description="Delete a reaction message", guild_ids=GUILD_IDS) @commands.has_permissions(manage_roles=True) async def delete_reaction_message(self, ctx: discord.ApplicationContext, reaction_role_id: str): # Check if server has been instantiated await check_server_instantiated(ctx) # Check if the message ID is in the database message = db_react_roles.get_reaction_message_by_uuid(reaction_role_id) if not message: await ctx.respond("That Reaction Role ID does not exist", ephemeral=True) return if message[2] != ctx.guild.id: await ctx.respond("That Reaction Role ID does not exist in this server", ephemeral=True) return # Delete the message from the channel channel = message[3] message = await self.bot.get_channel(channel).fetch_message(int(message_id)) await message.delete() # Delete the message from the database db_react_roles.delete_reaction_message_from_db(int(message_id)) # Send confirmation message await ctx.respond(f"Reaction message deleted successfully!", ephemeral=True) def setup(bot): bot.add_cog(ReactRoles(bot))