perf: compute study groups from config only once
This commit is contained in:
parent
ff5993997a
commit
cdd68052ca
1 changed files with 36 additions and 27 deletions
55
main.py
55
main.py
|
@ -10,9 +10,6 @@ DEFAULT_CONFIG_FILE = 'config.ini' # Configuration file with server IDs and stud
|
||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
intents.members = True
|
intents.members = True
|
||||||
|
|
||||||
config = ConfigParser()
|
|
||||||
config.read(DEFAULT_CONFIG_FILE)
|
|
||||||
|
|
||||||
bot = commands.Bot(command_prefix='!', intents=intents)
|
bot = commands.Bot(command_prefix='!', intents=intents)
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,28 +21,31 @@ async def reply(ctx: commands.Context, message: str):
|
||||||
await ctx.send(message)
|
await ctx.send(message)
|
||||||
|
|
||||||
|
|
||||||
def get_server_IDs():
|
def init(config):
|
||||||
config = ConfigParser()
|
"""
|
||||||
config.read(DEFAULT_CONFIG_FILE)
|
Initialize the server IDs and study groups from the configuration file.
|
||||||
server_IDs = []
|
:param config: The configuration file.
|
||||||
for section in config.sections():
|
:return: A tuple with server IDs and study groups.
|
||||||
server_IDs.append(int(config[section]['server_ID']))
|
"""
|
||||||
return server_IDs
|
def get_section_server_id(section):
|
||||||
|
return int(config[section]["server_ID"])
|
||||||
|
|
||||||
# Study group roles and their corresponding IDs (you can customize this)
|
server_ids = {get_section_server_id(section) for section in config.sections() if "server_ID" in config[section]}
|
||||||
def get_study_groups(server_id):
|
study_groups = {server_id: dict() for server_id in server_ids}
|
||||||
config = ConfigParser()
|
|
||||||
config.read(DEFAULT_CONFIG_FILE)
|
|
||||||
|
|
||||||
study_groups = {}
|
|
||||||
for section in config.sections():
|
for section in config.sections():
|
||||||
if int(config[section]['server_ID']) == server_id:
|
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
|
# Study group names are in style "kruh-XX", where XX is a two-digit number
|
||||||
# Iterate over all from top to bottom
|
# Iterate over all from top to bottom
|
||||||
for (key, value) in config.items(section):
|
for (key, value) in config.items(section):
|
||||||
if key.startswith('kruh-'):
|
if key.startswith('kruh-'):
|
||||||
study_groups[key] = int(value)
|
study_groups[server_id][key] = int(value)
|
||||||
return study_groups
|
|
||||||
|
return server_ids, study_groups
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
|
@ -54,25 +54,28 @@ async def on_ready():
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def grant_role(ctx, study_group_name: str):
|
async def grant_role(ctx, study_group_name: str):
|
||||||
server_IDs = get_server_IDs()
|
|
||||||
author_id = ctx.author.id
|
author_id = ctx.author.id
|
||||||
|
|
||||||
for guild_id in server_IDs:
|
for guild_id in server_ids:
|
||||||
guild = bot.get_guild(guild_id)
|
guild = bot.get_guild(guild_id)
|
||||||
|
|
||||||
if guild is None:
|
if guild is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
author = guild.get_member(author_id)
|
author = guild.get_member(author_id)
|
||||||
|
|
||||||
if author is None:
|
if author is None:
|
||||||
continue
|
continue
|
||||||
study_groups = get_study_groups(guild_id)
|
|
||||||
if study_group_name in study_groups:
|
if study_group_name in study_groups[guild_id]:
|
||||||
role_name = study_groups[study_group_name]
|
role_name = study_groups[guild_id][study_group_name]
|
||||||
role = discord.utils.get(guild.roles, id=role_name)
|
role = discord.utils.get(guild.roles, id=role_name)
|
||||||
|
|
||||||
if role is not None:
|
if role is not None:
|
||||||
await author.add_roles(role)
|
await author.add_roles(role)
|
||||||
await reply(ctx, f'{author.mention} has been granted the {study_group_name} role on the [{guild.name}] server.')
|
await reply(ctx, f'{author.mention} has been granted the {study_group_name} role on the [{guild.name}] server.')
|
||||||
return
|
return
|
||||||
|
|
||||||
await reply_error(ctx, 'Something went wrong. Please check your command and try again.')
|
await reply_error(ctx, 'Something went wrong. Please check your command and try again.')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -89,5 +92,11 @@ async def on_message(message):
|
||||||
# Process the command
|
# Process the command
|
||||||
await bot.process_commands(message)
|
await bot.process_commands(message)
|
||||||
|
|
||||||
|
|
||||||
|
config = ConfigParser()
|
||||||
|
config.read(DEFAULT_CONFIG_FILE)
|
||||||
|
|
||||||
|
server_ids, study_groups = init(config)
|
||||||
|
|
||||||
# Run the bot
|
# Run the bot
|
||||||
bot.run(TOKEN)
|
bot.run(TOKEN)
|
||||||
|
|
Loading…
Reference in a new issue