Skip to content

Commit

Permalink
refactor: Make skullboard function without data file
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixpereira committed Jul 8, 2024
1 parent e3a7b9a commit 8e7ff02
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 56 deletions.
3 changes: 1 addition & 2 deletions .example.env
Original file line number Diff line number Diff line change
@@ -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
REQUIRED_REACTIONS=5
86 changes: 35 additions & 51 deletions src/commands/skullboard.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -98,19 +86,15 @@ 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
embed.set_author(name=user_nickname, icon_url=user_avatar_url)

# 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()
5 changes: 2 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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):
Expand All @@ -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
)


Expand Down

0 comments on commit 8e7ff02

Please sign in to comment.