commit a70a5a39126de5895bf2ac7887a3100d2e4dfeb2 Author: Ondřej Chwiedziuk Date: Mon Aug 19 20:27:46 2024 +0200 New diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2fa7ce7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.ini diff --git a/config-example.ini b/config-example.ini new file mode 100644 index 0000000..01b0d4f --- /dev/null +++ b/config-example.ini @@ -0,0 +1,3 @@ +[ROK] +server_ID = 111 +kruh-XX = 6 diff --git a/main.py b/main.py new file mode 100644 index 0000000..4aae035 --- /dev/null +++ b/main.py @@ -0,0 +1,82 @@ +import discord +from discord.ext import commands +from configparser import ConfigParser + +# Bot token (replace with your own) +TOKEN = 'MTE1NDc0ODQ3MzE4MzMwNTc4OQ.Gz_Q7u.02sb2YNV_QQy7Bs19roXlB62mjoMKA6y8aubHU' +# Iterate over the sections in the config file + + +def get_server_IDs(): + config = ConfigParser() + config.read("config.ini") + server_IDs = [] + for section in config.sections(): + server_IDs.append(int(config[section]['server_ID'])) + return server_IDs + + +# Create an instance of the bot +intents = discord.Intents.default() +intents.members = True +bot = commands.Bot(command_prefix='!', intents=intents) + +# Study group roles and their corresponding IDs (you can customize this) +def get_study_groups(server_id): + config = ConfigParser() + config.read("config.ini") + + 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 + +@bot.event +async def on_ready(): + print(f'Logged in as {bot.user.name}') + + +@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: + 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] + role = discord.utils.get(guild.roles, id=role_name) + + if role is not None: + await author.add_roles(role) + await ctx.send(f'{author.mention} has been granted the {study_group_name} role on server {guild.name}.') + return + await ctx.send('Something gone wrong. Please check your command and try again.') + return + + +@bot.event +async def on_message(message): + # Check if the message is a private message and not from the bot itself + if isinstance(message.channel, discord.DMChannel) and message.author != bot.user: + # Split the message content into words + words = message.content.split() + + # Check if the message starts with the bot's command prefix and has enough arguments + if len(words) >= 3 and words[0] == '!grant_role': + # Process the command + await bot.process_commands(message) + +# Run the bot +bot.run(TOKEN)