diff --git a/.example.env b/.example.env index 24118c0..a39bee7 100644 --- a/.example.env +++ b/.example.env @@ -1,5 +1,4 @@ GUILD_ID=GUILD_ID BOT_TOKEN="BOT_TOKEN" SKULLBOARD_CHANNEL_ID=SKULLBOARD_CHANNEL_ID -REQUIRED_REACTIONS=3 -DATA_FILE=skullboard_data.json \ No newline at end of file +REQUIRED_REACTIONS=5 \ No newline at end of file diff --git a/src/commands/skullboard.py b/src/commands/skullboard.py index 4366dcf..726580e 100644 --- a/src/commands/skullboard.py +++ b/src/commands/skullboard.py @@ -1,79 +1,67 @@ import os -import json from discord import Embed, Client from dotenv import load_dotenv # Load environment variables from .env file load_dotenv() -DATA_FILE = os.getenv("DATA_FILE") REQUIRED_REACTIONS = int(os.getenv("REQUIRED_REACTIONS")) class SkullboardManager: def __init__(self, client: Client): self.client = client - self.data_file = DATA_FILE self.required_reactions = REQUIRED_REACTIONS - self.message_map = {} - self.load_data() - - # Load existing data from JSON file if available - def load_data(self): - if os.path.exists(self.data_file): - with open(self.data_file, "r", encoding="utf-8") as f: - self.message_map = json.load(f) - - # Save data to JSON file - def save_data(self): - with open(self.data_file, "w", encoding="utf-8") as f: - json.dump(self.message_map, f) # Function to handle reactions and update/delete skullboard messages - async def handle_skullboard(self, message, skullboard_channel_id, value): + async def handle_skullboard(self, message, skullboard_channel_id): skullboard_channel = self.client.get_channel(skullboard_channel_id) if not skullboard_channel: return emoji = "💀" - message_id_str = str(message.id) - - if message_id_str in self.message_map: - skullboard_message_id, current_count = self.message_map[message_id_str] - else: - skullboard_message_id = None - current_count = 0 - - if value == "ADD": - current_count += 1 - elif value == "REMOVE": - current_count = max(0, current_count - 1) + current_count = next( + ( + reaction.count + for reaction in message.reactions + if reaction.emoji == emoji + ), + 0, + ) - self.message_map[message_id_str] = (skullboard_message_id, current_count) await self.update_or_send_skullboard_message( skullboard_channel, message, current_count, emoji ) - self.save_data() # Function to update or send skullboard message async def update_or_send_skullboard_message( self, channel, message, current_count, emoji ): - skullboard_message_id, _ = self.message_map.get(str(message.id), (None, 0)) + skullboard_message_id = None + message_jump_url = message.jump_url - if skullboard_message_id: - await self.edit_or_send_skullboard_message( - channel, - message, - current_count, - emoji, - send=False, - skullboard_message_id=skullboard_message_id, - ) - else: - await self.edit_or_send_skullboard_message( - channel, message, current_count, emoji, send=True - ) + async for skullboard_message in channel.history(limit=100): + if message_jump_url in skullboard_message.content: + skullboard_message_id = skullboard_message.id + break + + if current_count >= self.required_reactions: + if skullboard_message_id: + await self.edit_or_send_skullboard_message( + channel, + message, + current_count, + emoji, + send=False, + skullboard_message_id=skullboard_message_id, + ) + else: + await self.edit_or_send_skullboard_message( + channel, message, current_count, emoji, send=True + ) + elif skullboard_message_id: + skullboard_message = await channel.fetch_message(skullboard_message_id) + await skullboard_message.delete() # Function to edit or send skullboard message async def edit_or_send_skullboard_message( @@ -98,7 +86,7 @@ async def edit_or_send_skullboard_message( # Constructing the embed embed = Embed( - description=(f"{message.content}\n\n"), + description=f"{message.content}\n\n", timestamp=message.created_at, ) # Set user nickname and thumbnail @@ -106,11 +94,7 @@ async def edit_or_send_skullboard_message( # Determine if sending or editing the message if send: - skullboard_message = await channel.send(message_content, embed=embed) - self.message_map[str(message.id)] = (skullboard_message.id, current_count) + await channel.send(message_content, embed=embed) else: skullboard_message = await channel.fetch_message(skullboard_message_id) await skullboard_message.edit(content=message_content, embed=embed) - - # Save the updated message_map to the JSON file after each modification - self.save_data() diff --git a/src/main.py b/src/main.py index 4e40b5e..c9b8738 100644 --- a/src/main.py +++ b/src/main.py @@ -17,7 +17,6 @@ # Load environment variables from .env file load_dotenv() -# Retrieve environment variables GUILD_ID = int(os.environ["GUILD_ID"]) BOT_TOKEN = os.environ["BOT_TOKEN"] SKULLBOARD_CHANNEL_ID = int(os.environ["SKULLBOARD_CHANNEL_ID"]) @@ -67,7 +66,7 @@ async def on_raw_reaction_add(self, payload: RawReactionActionEvent): # Ignore reactions to own messages if message.author.id != self.user.id: await self.skullboard_manager.handle_skullboard( - message, SKULLBOARD_CHANNEL_ID, "ADD" + message, SKULLBOARD_CHANNEL_ID ) async def on_raw_reaction_remove(self, payload: RawReactionActionEvent): @@ -77,7 +76,7 @@ async def on_raw_reaction_remove(self, payload: RawReactionActionEvent): # Ignore reactions to own messages if message.author.id != self.user.id: await self.skullboard_manager.handle_skullboard( - message, SKULLBOARD_CHANNEL_ID, "REMOVE" + message, SKULLBOARD_CHANNEL_ID )