Skip to content

Commit

Permalink
Merge pull request #46 from MinaFoundation/ui-add-adminchannel
Browse files Browse the repository at this point in the history
UI add adminchannel
  • Loading branch information
johnmarcou authored Feb 20, 2025
2 parents bcac671 + bcc28b4 commit 43c400f
Show file tree
Hide file tree
Showing 8 changed files with 1,188 additions and 41 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ LOG_LEVEL=DEBUG
GUILD_ID=XXXXXXXX
GMAIL_ADDRESS=xyz@gmail.com
LEADERBOARD_FORUM_CHANNEL_ID=XXXXXXXXXXXXXXXXX
LEADERBOARD_ADMIN_CHANNEL_ID=XXXXXXXXXXXXXXXXX
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
GUILD_ID = os.getenv("GUILD_ID")
GMAIL_ADDRESS = os.getenv("GMAIL_ADDRESS")
LEADERBOARD_FORUM_CHANNEL_ID = os.getenv("LEADERBOARD_FORUM_CHANNEL_ID")
LEADERBOARD_ADMIN_CHANNEL_ID = int(os.getenv("LEADERBOARD_ADMIN_CHANNEL_ID", "0"))

GOOGLE_CREDENTIALS = os.getenv("GOOGLE_SHEETS_CREDENTIALS")

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ services:
- GTP_ENDPOINT=gtp_endpoint
- GUILD_ID=guild_id
- LEADERBOARD_FORUM_CHANNEL_ID=leaderboard_forum_channel_id
- LEADERBOARD_ADMIN_CHANNEL_ID=leaderboard_admin_channel_id
- LOG_LEVEL=DEBUG
- MONGO_COLLECTION=example_collection
- MONGO_DB=example_db
Expand Down
56 changes: 16 additions & 40 deletions leader_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import config
from log_config import get_logger
from leader_bot.shared_state import task_details, auto_post_tasks, auto_post_leaderboard
from sheet_functions import (
create_new_spreadsheet,
share_spreadsheet,
Expand All @@ -39,6 +40,8 @@
from modals import UserModal, UserDeletionModal
from helpers import csv_to_structured_string
import utils
from ui_manager import MainView
from leader_bot.utils import convert_to_iso8601

logger = get_logger(__name__)

Expand All @@ -53,16 +56,19 @@

spread_sheet_id = None
auto_post_task = None
auto_post_tasks = {}
task_details = {}

AUTH_TOKEN = config.SHARED_SECRET


@client.event
async def on_ready():
try:
await tree.sync(guild=discord.Object(id=config.GUILD_ID))
admin_channel = client.get_channel(config.LEADERBOARD_ADMIN_CHANNEL_ID)
await admin_channel.purge() # Clear previous messages

main_view = MainView()
embed = main_view.create_main_menu_embed()

await admin_channel.send(embed=embed, view=main_view)
logger.info(f"We have logged in as {client.user}")
except Exception as e:
logger.error(f"Error during on_ready: {e}")
Expand All @@ -73,6 +79,12 @@ async def on_message(message):
try:
if message.author == client.user:
return

if message.content.lower() == "!leaderbot":
main_view = MainView()
embed = main_view.create_main_menu_embed()
await message.channel.send(embed=embed, view=main_view)

except Exception as e:
logger.error(f"Error processing message: {e}")

Expand Down Expand Up @@ -300,35 +312,6 @@ async def leaderboard_stop_auto_post(interaction: discord.Interaction, date: str
await interaction.followup.send(f"An error occurred: {e}", ephemeral=True)


def auto_post_leaderboard(task_id):
async def inner():
try:
now = datetime.now()
details = task_details[task_id]
if now.hour == details["hour"] and now.minute == details["minute"]:
leaderboard = create_leaderboard_by_month(
details["year"], details["month"]
)
create_leaderboard_sheet(
details["spreadsheet_id"],
leaderboard,
details["year"],
details["month"],
)
messages = format_leaderboard_for_discord(leaderboard)
channel = details["channel"]
bot_user_id = client.user.id
async for message in channel.history(limit=None):
if message.author.id == bot_user_id:
await message.delete()
for msg in messages:
await channel.send(msg)
except Exception as e:
logger.error(f"Error in auto_post_leaderboard task {task_id}: {e}")

return inner


@tree.command(
name="leaderboard-closure-month",
description="It will create forum thread for leaderboard in the discord forum channel",
Expand Down Expand Up @@ -445,13 +428,6 @@ async def on_command(interaction: discord.Interaction):
await interaction.followup.send(f"An error occured: {e}", ephemeral=True)


def convert_to_iso8601(date_str):
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
iso8601_str = date_obj.strftime("%Y-%m-%dT%H:%M:%SZ")

return iso8601_str


async def fetch(session, url, method="GET", data=None, params=None):
async with session.request(method, url, json=data, params=params) as response:
return await response.json()
Expand Down
Loading

0 comments on commit 43c400f

Please sign in to comment.