From cdd68052caf8f53fde6e911cd02b5bd3bc05b12e Mon Sep 17 00:00:00 2001 From: Lukas Nedbalek Date: Tue, 24 Sep 2024 16:24:54 +0200 Subject: [PATCH] perf: compute study groups from config only once --- main.py | 63 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/main.py b/main.py index 0e68a89..87a07ab 100644 --- a/main.py +++ b/main.py @@ -10,9 +10,6 @@ DEFAULT_CONFIG_FILE = 'config.ini' # Configuration file with server IDs and stud intents = discord.Intents.default() intents.members = True -config = ConfigParser() -config.read(DEFAULT_CONFIG_FILE) - bot = commands.Bot(command_prefix='!', intents=intents) @@ -24,28 +21,31 @@ async def reply(ctx: commands.Context, message: str): await ctx.send(message) -def get_server_IDs(): - config = ConfigParser() - config.read(DEFAULT_CONFIG_FILE) - server_IDs = [] - for section in config.sections(): - server_IDs.append(int(config[section]['server_ID'])) - return server_IDs +def init(config): + """ + Initialize the server IDs and study groups from the configuration file. + :param config: The configuration file. + :return: A tuple with server IDs and study groups. + """ + def get_section_server_id(section): + return int(config[section]["server_ID"]) -# Study group roles and their corresponding IDs (you can customize this) -def get_study_groups(server_id): - config = ConfigParser() - config.read(DEFAULT_CONFIG_FILE) + server_ids = {get_section_server_id(section) for section in config.sections() if "server_ID" in config[section]} + study_groups = {server_id: dict() for server_id in server_ids} - study_groups = {} for section in config.sections(): - if int(config[section]['server_ID']) == server_id: - # Study group names are in style "kruh-XX", where XX is a two-digit number - # Iterate over all from top to bottom - for (key, value) in config.items(section): - if key.startswith('kruh-'): - study_groups[key] = int(value) - return study_groups + server_id = get_section_server_id(section) + if server_id is None: + continue + + # Study group names are in style "kruh-XX", where XX is a two-digit number + # Iterate over all from top to bottom + for (key, value) in config.items(section): + if key.startswith('kruh-'): + study_groups[server_id][key] = int(value) + + return server_ids, study_groups + @bot.event async def on_ready(): @@ -54,25 +54,28 @@ async def on_ready(): @bot.command() async def grant_role(ctx, study_group_name: str): - server_IDs = get_server_IDs() author_id = ctx.author.id - for guild_id in server_IDs: + for guild_id in server_ids: guild = bot.get_guild(guild_id) + if guild is None: continue + author = guild.get_member(author_id) + if author is None: continue - study_groups = get_study_groups(guild_id) - if study_group_name in study_groups: - role_name = study_groups[study_group_name] + + if study_group_name in study_groups[guild_id]: + role_name = study_groups[guild_id][study_group_name] role = discord.utils.get(guild.roles, id=role_name) if role is not None: await author.add_roles(role) await reply(ctx, f'{author.mention} has been granted the {study_group_name} role on the [{guild.name}] server.') return + await reply_error(ctx, 'Something went wrong. Please check your command and try again.') return @@ -89,5 +92,11 @@ async def on_message(message): # Process the command await bot.process_commands(message) + +config = ConfigParser() +config.read(DEFAULT_CONFIG_FILE) + +server_ids, study_groups = init(config) + # Run the bot bot.run(TOKEN)