2024-08-19 20:27:46 +02:00
|
|
|
import discord
|
|
|
|
from discord.ext import commands
|
|
|
|
from configparser import ConfigParser
|
|
|
|
|
2024-09-24 16:08:30 +02:00
|
|
|
TOKEN = 'MTE1NDc0ODQ3MzE4MzMwNTc4OQ.Gz_Q7u.02sb2YNV_QQy7Bs19roXlB62mjoMKA6y8aubHU' # Bot token (replace with your own)
|
|
|
|
DEFAULT_CONFIG_FILE = 'config.ini' # Configuration file with server IDs and study group roles
|
|
|
|
|
|
|
|
|
|
|
|
# Create an instance of the bot
|
|
|
|
intents = discord.Intents.default()
|
|
|
|
intents.members = True
|
|
|
|
|
|
|
|
bot = commands.Bot(command_prefix='!', intents=intents)
|
2024-09-24 15:13:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def reply_error(ctx: commands.Context, message: str):
|
|
|
|
await reply(ctx, f'**Error:** {message}', True)
|
|
|
|
|
|
|
|
|
|
|
|
async def reply(ctx: commands.Context, message: str):
|
|
|
|
await ctx.send(message)
|
2024-08-19 20:27:46 +02:00
|
|
|
|
|
|
|
|
2024-09-24 16:24:54 +02:00
|
|
|
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"])
|
2024-08-19 20:27:46 +02:00
|
|
|
|
2024-09-24 16:24:54 +02:00
|
|
|
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}
|
2024-08-19 20:27:46 +02:00
|
|
|
|
|
|
|
for section in config.sections():
|
2024-09-24 16:24:54 +02:00
|
|
|
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
|
|
|
|
|
2024-08-19 20:27:46 +02:00
|
|
|
|
|
|
|
@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):
|
|
|
|
author_id = ctx.author.id
|
|
|
|
|
2024-09-24 16:24:54 +02:00
|
|
|
for guild_id in server_ids:
|
2024-08-19 20:27:46 +02:00
|
|
|
guild = bot.get_guild(guild_id)
|
2024-09-24 16:24:54 +02:00
|
|
|
|
2024-08-19 20:27:46 +02:00
|
|
|
if guild is None:
|
|
|
|
continue
|
2024-09-24 16:24:54 +02:00
|
|
|
|
2024-08-19 20:27:46 +02:00
|
|
|
author = guild.get_member(author_id)
|
2024-09-24 16:24:54 +02:00
|
|
|
|
2024-08-19 20:27:46 +02:00
|
|
|
if author is None:
|
|
|
|
continue
|
2024-09-24 16:24:54 +02:00
|
|
|
|
|
|
|
if study_group_name in study_groups[guild_id]:
|
|
|
|
role_name = study_groups[guild_id][study_group_name]
|
2024-08-19 20:27:46 +02:00
|
|
|
role = discord.utils.get(guild.roles, id=role_name)
|
|
|
|
|
|
|
|
if role is not None:
|
|
|
|
await author.add_roles(role)
|
2024-09-24 15:13:52 +02:00
|
|
|
await reply(ctx, f'{author.mention} has been granted the {study_group_name} role on the [{guild.name}] server.')
|
2024-08-19 20:27:46 +02:00
|
|
|
return
|
2024-09-24 16:24:54 +02:00
|
|
|
|
2024-09-24 15:13:52 +02:00
|
|
|
await reply_error(ctx, 'Something went wrong. Please check your command and try again.')
|
2024-08-19 20:27:46 +02:00
|
|
|
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)
|
|
|
|
|
2024-09-24 16:24:54 +02:00
|
|
|
|
|
|
|
config = ConfigParser()
|
|
|
|
config.read(DEFAULT_CONFIG_FILE)
|
|
|
|
|
|
|
|
server_ids, study_groups = init(config)
|
|
|
|
|
2024-08-19 20:27:46 +02:00
|
|
|
# Run the bot
|
|
|
|
bot.run(TOKEN)
|