Skip to content

Commit

Permalink
fix birthday commands
Browse files Browse the repository at this point in the history
  • Loading branch information
gylfirst committed Apr 5, 2024
1 parent 38d36cb commit ed7e092
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 23 deletions.
6 changes: 6 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)}")
Expand Down
86 changes: 68 additions & 18 deletions commands/birthdays.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
2 changes: 1 addition & 1 deletion commands/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions commands_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -14,15 +14,13 @@

# List the commands
COMMANDS_LIST: tuple = (
add_birthday,
cheh,
delete,
die_roll,
info,
latex,
pin,
ping,
remove_birthday,
whisper,
)

Expand All @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion utils/birthdays.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit ed7e092

Please sign in to comment.