diff --git a/bot.py b/bot.py index 8e51089..2191346 100644 --- a/bot.py +++ b/bot.py @@ -60,6 +60,9 @@ def __init__(self): # First declaration to be able to add commands to the guild self.hypixel_guild = discord.Object(int(self.config["HYPIXEL_GUILD_ID"])) + # First declaration to be able to add commands to the guild + self.my_guild = discord.Object(int(self.config["GUILD_ID"])) + async def setup_hook(self): # Call commands and import tasks await commands(self) @@ -76,6 +79,9 @@ async def on_ready(self): # Hypixel guild with all information self.hypixel_guild = self.get_guild(int(self.config["HYPIXEL_GUILD_ID"])) + # My guild with all information + self.my_guild = self.get_guild(int(self.config["GUILD_ID"])) + # Log that the bot is ready and the number of guilds the bot is in self.bot_logger.info(f"{self.user} is now online and ready!") self.bot_logger.info(f"Number of servers I'm in : {len(self.guilds)}") diff --git a/commands/birthdays.py b/commands/birthdays.py index dd9f7b9..2e5e950 100644 --- a/commands/birthdays.py +++ b/commands/birthdays.py @@ -1,30 +1,82 @@ from __future__ import annotations -from datetime import datetime, timezone -from typing import TYPE_CHECKING +from datetime import date +from typing import TYPE_CHECKING, Optional import discord from discord import app_commands +from tomlkit import table from utils.birthdays import load_birthdays, save_birthdays if TYPE_CHECKING: from bot import ChouetteBot +# Define command group based on the Group class +class Birthday(app_commands.Group): + # Set command group name and description + def __init__(self): + super().__init__(name="birthday", description="Birthday management related commands") -# Commande pour ajouter un anniversaire -@app_commands.command( - name="add_birthday", - description="Permit the user to register his birthday", -) -async def add_birthday(interaction: discord.Interaction[ChouetteBot]): - date = datetime.now(tz=timezone.utc) - user_id = str(interaction.user.id) - birthdays = load_birthdays() - birthdays.table[user_id] - birthdays[user_id][interaction.user.name] = date - save_birthdays(birthdays) - await interaction.response.send_message("Anniversaire enregistré !", ephemeral=True) + # Commande pour ajouter un anniversaire + @app_commands.command( + name="add", + description="Permit the user to register his birthday", + ) + async def add(self, interaction: discord.Interaction[ChouetteBot], day: int, month: int, year: Optional[int]): + if not year: + year = 1 + try: + birth_date = date(year, month, day) + except ValueError: + pass + user_name = str(interaction.user.name) + user_id = str(interaction.user.id) + birthdays = load_birthdays() + if user_id not in birthdays: + user_info = table() + user_info["name"] = user_name + user_info["birthday"] = birth_date + birthdays.update({user_id:user_info}) + save_birthdays(birthdays) + await interaction.response.send_message("Anniversaire enregistré !", ephemeral=True) + else: + await interaction.response.send_message( + "Vous avez déjà un anniversaire enregistré.\n" + "Vous pouvez le supprimer avec la commande `/birthday remove`\n" + "Vous pouvez aussi le modifier avec la commande `/birthday modify`", + ephemeral=True, + ) + + + # Commande pour modifier un anniversaire + @app_commands.command( + name="modify", + description="Permit the user to modify his birthday", + ) + async def modify(self, interaction: discord.Interaction[ChouetteBot], day: int, month: int, year: Optional[int]): + if not year: + year = 1 + try: + birth_date = date(year, month, day) + except ValueError: + pass + user_name = str(interaction.user.name) + user_id = str(interaction.user.id) + birthdays = load_birthdays() + if user_id in birthdays: + user_info = table() + user_info["name"] = user_name + user_info["birthday"] = birth_date + birthdays.update({user_id:user_info}) + save_birthdays(birthdays) + await interaction.response.send_message("Anniversaire modifié !", ephemeral=True) + else: + await interaction.response.send_message( + "Vous n'avez pas d'anniversaire enregistré.\n" + "Vous pouvez l'ajouter avec la commande `/birthday add`", + ephemeral=True, + ) # Commande pour supprimer un anniversaire @@ -40,6 +92,4 @@ async def remove_birthday(interaction: discord.Interaction[ChouetteBot]): save_birthdays(birthdays) await interaction.response.send_message("Anniversaire supprimé !") else: - await interaction.response.send_message( - "Vous n'avez pas d'anniversaire enregistré.", ephemeral=True - ) + await interaction.response.send_message("Vous n'avez pas d'anniversaire enregistré.", ephemeral=True) diff --git a/commands/misc.py b/commands/misc.py index e0fc3f2..7031335 100644 --- a/commands/misc.py +++ b/commands/misc.py @@ -76,7 +76,7 @@ def is_msg(msg: discord.Message) -> bool: return (message.id >> 22) <= (msg.id >> 22) <= (last_id >> 22) del_msg = await message.channel.purge(bulk=True, reason="Admin used bulk delete", check=is_msg) - await interaction.followup.send(f"{len(del_msg)} messages supprimés !", delete_after=2) + await interaction.followup.send(f"{len(del_msg)} messages supprimés !") # Make a bot information command diff --git a/commands_list.py b/commands_list.py index 17d0628..90ea5df 100644 --- a/commands_list.py +++ b/commands_list.py @@ -5,7 +5,7 @@ import discord from commands.admin import whisper -from commands.birthdays import add_birthday, remove_birthday +from commands.birthdays import Birthday from commands.misc import cheh, delete, die_roll, info, latex, pin, ping from commands.skyblock import Skyblock @@ -14,7 +14,6 @@ # List the commands COMMANDS_LIST: tuple = ( - add_birthday, cheh, delete, die_roll, @@ -22,7 +21,6 @@ latex, pin, ping, - remove_birthday, whisper, ) @@ -38,6 +36,9 @@ async def commands(client: ChouetteBot): # Add the Skyblock command group to my Hypixel guild client.tree.add_command(Skyblock(), guild=client.hypixel_guild) + # Add the Birthday command group to my guild + client.tree.add_command(Birthday(), guild=client.my_guild) + # Create a global commands error handler @client.tree.error async def on_command_error( diff --git a/tasks.py b/tasks.py index 76c1c32..28b3be1 100644 --- a/tasks.py +++ b/tasks.py @@ -22,6 +22,7 @@ async def poke_ping(): # Loop to check if it's 8:00 and send a message if it's someone's birthday @tasks.loop(hours=24) async def check_birthdays(): +<<<<<<< HEAD now = time.now() if now.hour == 8 and now.minute == 0: today = now.strftime("%d/%m") @@ -32,6 +33,11 @@ async def check_birthdays(): await client.get_channel(int(client.config["BIRTHDAY_CHANNEL"])).send( msg_birthday ) +======= + user = "todo" + msg_birthday = f"\N{PARTY POPPER} {user.mention} is a year older now! Wish them a happy birthday! \N{PARTY POPPER}" + await client.get_channel(int(client.config["BIRTHDAY_CHANNEL"])).send(msg_birthday) +>>>>>>> 3435db9 (fix birthday commands) # Start loop poke_ping.start() diff --git a/utils/birthdays.py b/utils/birthdays.py index 9f79295..343db96 100644 --- a/utils/birthdays.py +++ b/utils/birthdays.py @@ -1,6 +1,9 @@ +from pathlib import Path + import tomlkit -birthday_file_path = "data/birthdays.toml" +Path("data").mkdir(exist_ok=True) +birthday_file_path = Path("data", "birthdays.toml") # Charge les anniversaires depuis un fichier TOML