Changed data structure

This commit is contained in:
2023-11-11 09:16:54 +01:00
parent de90b874fc
commit d9411d94bd
2 changed files with 38 additions and 32 deletions

24
main.py
View File

@@ -11,14 +11,14 @@ async def birthday_anouncements_task():
LOGGER.debug('birthday_anouncements_task started.') LOGGER.debug('birthday_anouncements_task started.')
for guild in bot.guilds: for guild in bot.guilds:
guild_data = get_guild_data(guild.id) guild_data = get_guild_data(guild.id)
if 'birthday_announcements_channel' in guild_data.keys(): if 'birthday_announcements_channel' in guild_data['features']['birthday'].keys():
channel = guild.get_channel(guild_data['birthday_announcements_channel']) channel = guild.get_channel(guild_data['features']['birthday']['birthday_announcements_channel'])
if 'birthday' in guild_data.keys(): if 'birthdays' in guild_data.keys():
for user_id in guild_data['birthday'].keys(): for user_id in guild_data['features']['birthday']['birthdays'].keys():
user = bot.get_user(int(user_id)) user = guild.get_member(int(user_id))
if user: if user:
if guild_data['birthday'][user_id]['announcements']: if guild_data['features']['birthday']['birthdays'][user_id]['announcements']:
date = guild_data['birthday'][user_id]['date'] date = guild_data['features']['birthday']['birthdays'][user_id]['date']
today = datetime.today().strftime('%d/%m') today = datetime.today().strftime('%d/%m')
if date == today: if date == today:
await channel.send(f'Joyeux anniversaire {user.mention}!') await channel.send(f'Joyeux anniversaire {user.mention}!')
@@ -26,12 +26,16 @@ async def birthday_anouncements_task():
LOGGER.debug(f'No birthday_announcements_channel set for guild {guild.name} ({guild.id}).') LOGGER.debug(f'No birthday_announcements_channel set for guild {guild.name} ({guild.id}).')
LOGGER.debug('birthday_anouncements_task ended.') LOGGER.debug('birthday_anouncements_task ended.')
@bot.slash_command(name='help',description='Displays this message.') @bot.slash_command(name='help',description='Displays the help message.')
async def help(ctx:discord.ApplicationContext): async def help(ctx:discord.ApplicationContext):
"""Displays this message.""" """Displays this message."""
command_name = 'help' command_name = 'help'
LOGGER.debug(f'{ctx.author.name} used /{command_name}.') LOGGER.debug(f'{ctx.author.name} used /{command_name}.')
await ctx.send_response('test',ephemeral=True) embed = discord.Embed(title='Help',description='List of commands and their descriptions.',color=discord.Color.from_rgb(171,0,219))
embed.set_author(name=bot.user.name,icon_url=bot.user.avatar.url)
for command in bot.commands:
embed.add_field(name=command.name,value=command.description,inline=False)
await ctx.send_response(embed=embed,ephemeral=True)
@bot.event @bot.event
async def on_guild_join(guild:discord.Guild): async def on_guild_join(guild:discord.Guild):
@@ -61,7 +65,7 @@ async def on_ready():
if not os.path.exists(path): if not os.path.exists(path):
LOGGER.info(f'Creating data file for guild {guild_id}') LOGGER.info(f'Creating data file for guild {guild_id}')
with open(path, 'x') as f: with open(path, 'x') as f:
json.dump({}, f, indent=2) json.dump({'features': {}}, f, indent=2)
birthday_anouncements_task.start() birthday_anouncements_task.start()
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -21,14 +21,15 @@ class BirthdayCog(commands.Cog):
if for_user: 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 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}.') LOGGER.debug(f'{author.name} used /{command_name} {date} for {for_user.name}.')
if 'birthday' not in guild_data.keys(): if 'birthday' not in guild_data['features'].keys():
guild_data['birthday'] = dict() guild_data['features']['birthday'] = dict()
if for_user.id not in guild_data['birthday'].keys(): if 'birthdays' not in guild_data['features']['birthday'].keys():
guild_data['birthday'][str(for_user.id)] = dict() guild_data['features']['birthday']['birthdays'] = dict()
guild_data['birthday'][str(for_user.id)]['date'] = date if str(for_user.id) not in guild_data['features']['birthday']['birthdays'].keys():
if 'announcements' not in guild_data['birthday'][str(for_user.id)].keys(): guild_data['features']['birthday'][str(for_user.id)] = dict()
guild_data['birthday'][str(for_user.id)]['announcements'] = True guild_data['features']['birthday']['birthdays'][str(for_user.id)]['date'] = date
guild_data['features']['birthday']['birthdays'][str(for_user.id)]['announcements'] = True
save_guild_data(guild_id, guild_data) save_guild_data(guild_id, guild_data)
await ctx.send_response(f'Birthday for {for_user.name} has been set to {date}.',ephemeral=True) await ctx.send_response(f'Birthday for {for_user.name} has been set to {date}.',ephemeral=True)
else: else:
@@ -37,18 +38,19 @@ class BirthdayCog(commands.Cog):
else: else:
# Set birthday for author # Set birthday for author
LOGGER.debug(f'{author.name} used /{command_name} {date}.') LOGGER.debug(f'{author.name} used /{command_name} {date}.')
if 'birthday' not in guild_data.keys(): if 'birthday' not in guild_data['features'].keys():
guild_data['birthday'] = dict() guild_data['features']['birthday'] = dict()
if author.id not in guild_data['birthday'].keys(): if 'birthdays' not in guild_data['features']['birthday'].keys():
guild_data['birthday'][str(author.id)] = dict() guild_data['features']['birthday']['birthdays'] = dict()
guild_data['birthday'][str(author.id)]['date'] = date if author.id not in guild_data['features']['birthday']['birthdays'].keys():
if 'announcements' not in guild_data['birthday'][str(author.id)].keys(): guild_data['features']['birthday']['birthdays'][str(author.id)] = dict()
guild_data['birthday'][str(author.id)]['announcements'] = True guild_data['features']['birthday']['birthdays'][str(author.id)]['date'] = date
guild_data['features']['birthday']['birthdays'][str(author.id)]['announcements'] = True
save_guild_data(guild_id, guild_data) save_guild_data(guild_id, guild_data)
# The status defines the human readable status of the announcements to be displayed in the response. # 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' announcements_status = 'will be' if guild_data['features']['birthday']['birthdays'][str(author.id)]['announcements'] else 'will not be'
await ctx.send_response(f'Your birthday has been set to {date} and announcements {announcements_status}.',ephemeral=True) await ctx.send_response(f'{author.mention} has set their birthday to {date} and {announcements_status} announced.',ephemeral=True)
else: else:
LOGGER.debug(f'{author.name} used /set_birthday {date} but the format is not correct.') 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) await ctx.send_response('The date must is DAY/MONTH format.',ephemeral=True)
@@ -65,7 +67,7 @@ class BirthdayCog(commands.Cog):
if for_user: 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 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}.') LOGGER.debug(f'{author.name} used /{command_name} {enable} for {for_user.name}.')
guild_data['birthday'][str(for_user.id)]['announcements'] = enable guild_data['features']['birthday']['birthdays'][str(for_user.id)]['announcements'] = enable
save_guild_data(guild_id, guild_data) save_guild_data(guild_id, guild_data)
await ctx.send_response(f'Announcements for {for_user.name} are set to {enable}',ephemeral=True) await ctx.send_response(f'Announcements for {for_user.name} are set to {enable}',ephemeral=True)
else: else:
@@ -73,7 +75,7 @@ class BirthdayCog(commands.Cog):
await ctx.send_response('You must be an administrator to run the command with "for_user".',ephemeral=True) await ctx.send_response('You must be an administrator to run the command with "for_user".',ephemeral=True)
else: else:
LOGGER.debug(f'{author.name} used /{command_name} {enable}.') LOGGER.debug(f'{author.name} used /{command_name} {enable}.')
guild_data['birthday'][str(author.id)]['announcements'] = enable guild_data['features']['birthday']['birthdays'][str(author.id)]['announcements'] = enable
save_guild_data(guild_id, guild_data) save_guild_data(guild_id, guild_data)
await ctx.send_response(f'Your birthday announcements have been set to {enable}',ephemeral=True) await ctx.send_response(f'Your birthday announcements have been set to {enable}',ephemeral=True)
@@ -87,10 +89,10 @@ class BirthdayCog(commands.Cog):
author = ctx.author author = ctx.author
if author.guild_permissions.administrator: if author.guild_permissions.administrator:
LOGGER.debug(f'{author.name} used /{command_name} {channel.name}.') LOGGER.debug(f'{author.name} used /{command_name} {channel.name}.')
guild_data['birthday_announcements_channel'] = channel.id guild_data['features']['birthday']['birthday_announcements_channel'] = channel.id
save_guild_data(guild_id, guild_data) save_guild_data(guild_id, guild_data)
await ctx.send_response(f'Birthday announcements will be sent to {channel.name}.',ephemeral=True) await ctx.send_response(f'Birthday announcements will be sent to {channel.mention}.',ephemeral=True)
else: else:
LOGGER.debug(f'{author.name} tried to use /{command_name} {channel.name} but is not an administrator.') LOGGER.debug(f'{author.name} tried to use /{command_name} {channel.mention} but is not an administrator.')
await ctx.send_response('You must be an administrator to run this command.',ephemeral=True) await ctx.send_response('You must be an administrator to run this command.',ephemeral=True)