diff --git a/modules/cogs/birthday.py b/modules/cogs/birthday.py new file mode 100644 index 0000000..b7194a7 --- /dev/null +++ b/modules/cogs/birthday.py @@ -0,0 +1,96 @@ +import discord, re +from discord import option +from discord.ext import commands +from ..setup.logger import LOGGER +from ..setup.data import get_guild_data, save_guild_data + +class BirthdayCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.slash_command(description='Sets birthday date. Must be in DAY/MONTH format.') + @option(name='date',description='Birthday date.',required=True) + @option(name='for_user',description='The user to set the birthday date for.',required=False,type=discord.Member) + async def birthday_set(self,ctx:discord.ApplicationContext, date:str, for_user:discord.Member): + """Sets birthday date. Must be in DAY/MONTH format.""" + command_name = 'birthday_set' + guild_id = ctx.guild.id + guild_data = get_guild_data(guild_id) + author = ctx.author + if re.match(r'\d{2}\/\d{2}',date): # Check if date is in DAY/MONTH format + if for_user: + if author.guild_permissions.administrator: # Check if author is an administrator in case they want to set the birthday for another user + LOGGER.debug(f'{author.name} used /{command_name} {date} for {for_user.name}.') + if 'birthday' not in guild_data.keys(): + guild_data['birthday'] = dict() + if for_user.id not in guild_data['birthday'].keys(): + guild_data['birthday'][str(for_user.id)] = dict() + guild_data['birthday'][str(for_user.id)]['date'] = date + if 'announcements' not in guild_data['birthday'][str(for_user.id)].keys(): + guild_data['birthday'][str(for_user.id)]['announcements'] = True + + save_guild_data(guild_id, guild_data) + await ctx.send_response(f'Birthday for {for_user.name} has been set to {date}.',ephemeral=True) + else: + LOGGER.debug(f'{author.name} tried to use /{command_name} {date} for {for_user.name} but is not an administrator.') + await ctx.send_response('You must be an administrator to run the command with "for_user".',ephemeral=True) + else: + # Set birthday for author + LOGGER.debug(f'{author.name} used /{command_name} {date}.') + if 'birthday' not in guild_data.keys(): + guild_data['birthday'] = dict() + if author.id not in guild_data['birthday'].keys(): + guild_data['birthday'][str(author.id)] = dict() + guild_data['birthday'][str(author.id)]['date'] = date + if 'announcements' not in guild_data['birthday'][str(author.id)].keys(): + guild_data['birthday'][str(author.id)]['announcements'] = True + save_guild_data(guild_id, guild_data) + + # The status defines the human readable status of the announcements to be displayed in the response. + announcements_status = 'are enabled' if guild_data['birthday'][str(author.id)]['announcements'] else 'aren\'t enabled' + await ctx.send_response(f'Your birthday has been set to {date} and announcements {announcements_status}.',ephemeral=True) + else: + LOGGER.debug(f'{author.name} used /set_birthday {date} but the format is not correct.') + await ctx.send_response('The date must is DAY/MONTH format.',ephemeral=True) + + @commands.slash_command(description='Enable or disable birthday announcements.') + @option(name='enable',description='Enable birthday announcements.',required=True) + @option(name='for_user',description='The user to enable or disable birthday announcements for.',required=False,type=discord.Member) + async def birthday_announcements(self,ctx:discord.ApplicationContext, enable:bool, for_user:discord.Member): + """Enable or disable birthday announcements.""" + command_name = 'birthday_announcements' + guild_id = ctx.guild.id + guild_data = get_guild_data(guild_id) or dict() + author = ctx.author + if for_user: + if author.guild_permissions.administrator: # Check if author is an administrator in case they want to set the birthday for another user + LOGGER.debug(f'{author.name} used /{command_name} {enable} for {for_user.name}.') + guild_data['birthday'][str(for_user.id)]['announcements'] = enable + save_guild_data(guild_id, guild_data) + await ctx.send_response(f'Announcements for {for_user.name} are set to {enable}',ephemeral=True) + else: + LOGGER.debug(f'{author.name} tried to use /{command_name} {enable} for {for_user.name} but is not an administrator.') + await ctx.send_response('You must be an administrator to run the command with "for_user".',ephemeral=True) + else: + LOGGER.debug(f'{author.name} used /{command_name} {enable}.') + guild_data['birthday'][str(author.id)]['announcements'] = enable + save_guild_data(guild_id, guild_data) + await ctx.send_response(f'Your birthday announcements have been set to {enable}',ephemeral=True) + + @commands.slash_command(description='Sets the channel to send birthday announcements to.') + @option(name='channel',description='The channel to send birthday announcements to.',required=True,type=discord.TextChannel) + async def birthday_announcements_channel(self,ctx:discord.ApplicationContext, channel:discord.TextChannel): + """Sets the channel to send birthday announcements to.""" + command_name = 'birthday_announcements_channel' + guild_id = ctx.guild.id + guild_data = get_guild_data(guild_id) or dict() + author = ctx.author + if author.guild_permissions.administrator: + LOGGER.debug(f'{author.name} used /{command_name} {channel.name}.') + guild_data['birthday_announcements_channel'] = channel.id + save_guild_data(guild_id, guild_data) + await ctx.send_response(f'Birthday announcements will be sent to {channel.name}.',ephemeral=True) + else: + LOGGER.debug(f'{author.name} tried to use /{command_name} {channel.name} but is not an administrator.') + await ctx.send_response('You must be an administrator to run this command.',ephemeral=True) + diff --git a/modules/setup/bot.py b/modules/setup/bot.py new file mode 100644 index 0000000..50ea39c --- /dev/null +++ b/modules/setup/bot.py @@ -0,0 +1,13 @@ +import discord +from ..cogs.birthday import BirthdayCog + +bot_intents = discord.Intents.default() +bot_intents.message_content = True +bot_intents.members = True +bot_intents.presences = True +bot_intents.guilds = True +bot_intents.reactions = True + +bot = discord.Bot(command_prefix='$', intents=bot_intents) + +bot.add_cog(BirthdayCog(bot)) \ No newline at end of file diff --git a/modules/setup/data.py b/modules/setup/data.py new file mode 100644 index 0000000..797c9f1 --- /dev/null +++ b/modules/setup/data.py @@ -0,0 +1,15 @@ +import os, json + +DATA_DIR = 'data/' + +def get_guild_data(guild_id:int): + """Reads and returns the dictionary of guild data from the guild's JSON data file.""" + path = os.path.join(DATA_DIR, f'{guild_id}.json') + with open(path, 'r') as f: + return json.load(f) + +def save_guild_data(guild_id:int, data): + """Saves the dictionary of guild data to the guild's JSON data file.""" + path = os.path.join(DATA_DIR, f'{guild_id}.json') + with open(path, 'w') as f: + json.dump(data, f, indent=2) \ No newline at end of file diff --git a/modules/setup/logger.py b/modules/setup/logger.py new file mode 100644 index 0000000..4055e58 --- /dev/null +++ b/modules/setup/logger.py @@ -0,0 +1,8 @@ +import logging + +logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(asctime)s: %(name)s: %(message)s') +LOGGER = logging.getLogger('botmafieux') +LOGGER.setLevel(logging.DEBUG) +handler = logging.FileHandler(filename='botmafieux.log', encoding='utf-8', mode='w') +handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) +LOGGER.addHandler(handler) \ No newline at end of file