diff --git a/main.py b/main.py index 9b9268e..6e69ae8 100644 --- a/main.py +++ b/main.py @@ -24,11 +24,13 @@ bot = discord.Bot(command_prefix='$', intents=bot_intents) 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) @@ -50,13 +52,14 @@ async def test_loop(): @option(name='for_user',description='The user to set the birthday date for.',required=False,type=discord.Member) async def birthday_set(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): + 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: - LOGGER.debug(f'{author.name} used /set_birthday {date} for {for_user.name}.') + 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(): @@ -64,13 +67,15 @@ async def birthday_set(ctx:discord.ApplicationContext, date:str, for_user:discor 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'{author.name} used /set_birthday {date} for {for_user.name}.',ephemeral=True) + await ctx.send_response(f'Birthday for {for_user.name} has been set to {date}.',ephemeral=True) else: - LOGGER.debug(f'{author.name} used /set_birthday {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".',ephemeral=True) + 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: - LOGGER.debug(f'{author.name} used /set_birthday {date}.') + # 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(): @@ -79,38 +84,50 @@ async def birthday_set(ctx:discord.ApplicationContext, date:str, for_user:discor 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(f'The date must is DAY/MONTH format.',ephemeral=True) + await ctx.send_response('The date must is DAY/MONTH format.',ephemeral=True) @bot.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) async def birthday_announcements(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: - LOGGER.debug(f'{author.name} used /enable_birthday {enable} for {for_user.name}.') + 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} used /enable_birthday {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".',ephemeral=True) + 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 /enable_birthday {enable}.') + 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) +@bot.event +async def on_guild_join(guild:discord.Guild): + LOGGER.info(f'Bot joined guild {guild.name} ({guild.id}).') + path = os.path.join(DATA_DIR, f'{guild.id}.json') + if not os.path.exists(path): + LOGGER.info(f'Creating data file for guild {guild.id}') + with open(path, 'x') as f: + json.dump({}, f, indent=2) + @bot.event async def on_guild_remove(guild:discord.Guild): - LOGGER.info(f'Bot left guild {guild.id}.') + LOGGER.info(f'Bot left guild {guild.name} ({guild.id}).') path = os.path.join(DATA_DIR, f'{guild.id}.json') if os.path.exists(path): LOGGER.info(f'Deleting data file for guild {guild.id}.')