From 6d35fc562e32ee19e491d4837878f0e51a161e79 Mon Sep 17 00:00:00 2001 From: Gylfirst Date: Fri, 5 Apr 2024 20:08:34 +0200 Subject: [PATCH] bazar --- .env.example | 2 ++ commands/birthdays.py | 43 +++++++++++++++++++++++++++++++++++++ commands/misc.py | 2 +- commands_list.py | 13 ++++++----- data/birthdays.toml | 0 docker-compose.yaml.example | 1 + requirements.txt | 1 + tasks.py | 13 +++++++++++ utils/birthdays.py | 18 ++++++++++++++++ 9 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 commands/birthdays.py create mode 100644 data/birthdays.toml create mode 100644 utils/birthdays.py diff --git a/.env.example b/.env.example index db1cd4c..5822380 100644 --- a/.env.example +++ b/.env.example @@ -16,6 +16,8 @@ GUILD_ID= POKE_CHANNEL= POKE_ROLE= POKEBALL_EMOJI= +# Birthdays notif +BIRTHDAY_CHANNEL= # Hypixel API key HYPIXEL_KEY= diff --git a/commands/birthdays.py b/commands/birthdays.py new file mode 100644 index 0000000..a4f494f --- /dev/null +++ b/commands/birthdays.py @@ -0,0 +1,43 @@ +from __future__ import annotations + +from datetime import datetime, timezone +from typing import TYPE_CHECKING + +import discord +from discord import app_commands + +from utils.birthdays import load_birthdays, save_birthdays + +if TYPE_CHECKING: + from bot import ChouetteBot + + +# 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 supprimer un anniversaire +@app_commands.command( + name="remove_birthday", + description="Permit the user to delte his birthday", +) +async def remove_birthday(interaction: discord.Interaction[ChouetteBot]): + user_id = str(interaction.user.id) + birthdays = load_birthdays() + if user_id in birthdays: + del birthdays[user_id] + 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) diff --git a/commands/misc.py b/commands/misc.py index 7031335..e0fc3f2 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 !") + await interaction.followup.send(f"{len(del_msg)} messages supprimés !", delete_after=2) # Make a bot information command diff --git a/commands_list.py b/commands_list.py index 96defe3..17d0628 100644 --- a/commands_list.py +++ b/commands_list.py @@ -5,6 +5,7 @@ import discord from commands.admin import whisper +from commands.birthdays import add_birthday, remove_birthday from commands.misc import cheh, delete, die_roll, info, latex, pin, ping from commands.skyblock import Skyblock @@ -13,14 +14,16 @@ # List the commands COMMANDS_LIST: tuple = ( - latex, - die_roll, - ping, + add_birthday, cheh, - pin, delete, - whisper, + die_roll, info, + latex, + pin, + ping, + remove_birthday, + whisper, ) SPACES = " " * 38 diff --git a/data/birthdays.toml b/data/birthdays.toml new file mode 100644 index 0000000..e69de29 diff --git a/docker-compose.yaml.example b/docker-compose.yaml.example index ff9b2da..566ff2d 100644 --- a/docker-compose.yaml.example +++ b/docker-compose.yaml.example @@ -16,6 +16,7 @@ services: - "8080:8080" # Exposed port:Inner port volumes: - /path/to/logs:/usr/src/chouettebot/logs # Path to logs + - /path/to/data:/usr/src/chouettebot/data # Path to data storage restart: unless-stopped # Restart policy (here it will always restart unless you stopped it) pull_policy: always # Pull the image before the application start diff --git a/requirements.txt b/requirements.txt index 5504db5..06638d3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ aiohttp ~= 3.9.3 discord.py[speed] ~= 2.3.2 python-dotenv ~= 1.0.1 +tomlkit ~= 0.12.4 \ No newline at end of file diff --git a/tasks.py b/tasks.py index 75135bc..c3ffa87 100644 --- a/tasks.py +++ b/tasks.py @@ -18,6 +18,19 @@ async def poke_ping(): pokeball = client.get_emoji(int(client.config["POKEBALL_EMOJI"])) msg_poke = f"{dresseurs.mention} C'est l'heure d'attraper des pokémons {pokeball}" await client.get_channel(int(client.config["POKE_CHANNEL"])).send(msg_poke) + + # 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(): + now = time.now() + if now.hour == 8 and now.minute == 0: + today = now.strftime('%d/%m') + for user_id, birthday in birthdays.items(): + if birthday == today: + user = await bot.fetch_user(int(user_id)) + msg_birthday = f":tada: {user.mention} is a year older now! Wish them a happy birthday! :tada:" + await client.get_channel(int(client.config["BIRTHDAY_CHANNEL"])).send(msg_birthday) # Start loop poke_ping.start() + check_birthdays.start() diff --git a/utils/birthdays.py b/utils/birthdays.py new file mode 100644 index 0000000..9f79295 --- /dev/null +++ b/utils/birthdays.py @@ -0,0 +1,18 @@ +import tomlkit + +birthday_file_path = "data/birthdays.toml" + + +# Charge les anniversaires depuis un fichier TOML +def load_birthdays(): + try: + with open(birthday_file_path) as f: + return tomlkit.load(f) + except FileNotFoundError: + return {} + + +# Enregistre les anniversaires dans le fichier TOML +def save_birthdays(birthdays): + with open(birthday_file_path, "w") as f: + tomlkit.dump(birthdays, f)