From 82a21ec8c323296148c75f431bfeb9ab7235c0f4 Mon Sep 17 00:00:00 2001 From: SpagettiFisch Date: Fri, 14 Jun 2024 14:46:05 +0200 Subject: [PATCH] changed permission management for high-level commands added a feature to manually whitelist people (and to not whitelist everyone by default) Signed-off-by: SpagettiFisch --- main.py | 27 +++++++++++++-------- modules/functions.py | 58 ++++++++++++++++++++++++++++++++++++++++++-- modules/init.py | 12 ++++----- 3 files changed, 79 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index f186ded..ea5d9d0 100644 --- a/main.py +++ b/main.py @@ -11,34 +11,41 @@ from discord.ext import slash token = init.config().get_token() domain = init.config().get_pterodactyl_domain() apikey = init.config().get_pterodactyl_apikey() -guild_id = init.config().get_guild_id() bot = slash.SlashBot(command_prefix='!', help_command=None) -# msg_opt = slash.Option(description="Dein Minecraft Name", , required=True) @bot.event async def on_ready(): - print('Bot wurde gestartet') + print('Bot started succesfully') return @bot.slash_cmd(aliases=["hilfe"]) async def help(ctx:slash.Context): - "Hilfe für alle verwendbaren Befehle" + "Hilfe für alle verwendbaren Befehle" #Help for all usable commands await functions.cmdhelp(ctx) -@bot.slash_cmd(aliases=["minecraft"], guild_id=1210285934248198244) -async def mc(ctx:slash.Context, name:slash.Option(description="Dein Minecraftname", required=True)): - "Registriere deinen Minecraft Namen" +@bot.slash_cmd(aliases=["minecraft"]) +async def mc(ctx:slash.Context, name:slash.Option(description="Dein Minecraftname", required=True)): #Your Minecraft name + "Registriere deinen Minecraft Namen" #Register your Minecraft name await functions.cmdmc(ctx, name.strip(), bot) @bot.slash_cmd() async def mcname(ctx:slash.Context): - "Gibt deinen aktuellen Minecraft Namen an" + "Gibt deinen aktuellen Minecraft Namen an" #Outputs your linked Minecraft name await functions.cmdmcname(ctx) -@bot.slash_cmd(guild_id=guild_id, hidden=True) +@bot.slash_cmd() async def shutdown(ctx:slash.Context): "Will shutdown the bot if you are mighty enough." - await functions.cmdshutdown(ctx, bot) + if await functions.isAdmin(ctx, bot): + await functions.cmdshutdown(ctx, bot) + +@bot.slash_cmd(guild_id=1210285934248198244) +async def allow(ctx:slash.Context, user:slash.Option(description="der zu whitelistene Nutzuer (@)", required=True)): + "Fügt Spieler der Whitelist hinzu." #Add Players to whitelist + if await functions.isMod(ctx, bot): + await functions.cmdallow(ctx, user.strip(), bot) + + bot.run(token) \ No newline at end of file diff --git a/modules/functions.py b/modules/functions.py index 8c5b1cd..a2de05b 100644 --- a/modules/functions.py +++ b/modules/functions.py @@ -11,6 +11,8 @@ from discord.ext import slash from modules import init con, cur = init.getdb() +mod_roles = init.config().get_mod_roles() +admin_roles = init.config().get_admin_roles() async def cmdhelp(ctx:slash.Context): embed = discord.Embed(title="Hilfe", @@ -37,7 +39,8 @@ async def cmdmc(ctx:slash.Context, name:str, client): cur.execute(f"UPDATE user SET mcname = '{mcinfo['name']}', uuid = '{uuid}' WHERE id = {ctx.author.id}") await ctx.respond(f'Dein Minecraftname **{name}** wurde erfolgreich aktualisiert.') else: - cur.execute(f"INSERT INTO user VALUES ({ctx.author.id}, '{ctx.author.nick}', '{ctx.author.avatar_url}', '{mcinfo['name']}', '{uuid}', {True})") + cur.execute(f"INSERT INTO user VALUES ({ctx.author.id}, '{ctx.author.nick}', '{ctx.author.avatar_url}', '{mcinfo['name']}', '{uuid}', {False + })") await ctx.respond(f'Dein Minecraftname **{name}** wurde erfolgreich hinzugefügt.') con.commit() await syncWhitelist() @@ -69,6 +72,25 @@ async def cmdshutdown(ctx:slash.Context, bot): else: await ctx.respond('You are not as mighty as you may think you are.') +async def cmdallow(ctx:slash.Context, user:str, bot:discord.ext.commands.Bot): + user_id = user.removeprefix('<@').removesuffix('>') + try: + user = str(await bot.fetch_user(user_id)).removesuffix('#0') + except: + await ctx.respond(f"Eingabe ungültig. \nErhaltene Eingabe: **{user}**. Es wurde **@** oder **** erwartet. \nBitte versuche es erneut.", ephemeral=True) + return + + result = cur.execute(f"SELECT * FROM user WHERE id = '{user_id}'") + result = cur.fetchone() + + if result: + cur.execute(f"UPDATE user SET iswhitelisted = {True} WHERE id = '{user_id}'") + con.commit() + await ctx.respond(f'Der Nutzer **{user}** wurde erfolgreich gewhitelisted.') + await syncWhitelist() + else: + await ctx.respond(f'Der Nutzer **{user}** existiert nicht im System. Damit er vom System registriert wird, muss er zunächst seinen Minecraftnamen hinzufügen.', ephemeral=True) + async def syncWhitelist(): results = cur.execute("SELECT mcname, uuid, iswhitelisted FROM user") results = cur.fetchall() @@ -104,4 +126,36 @@ async def syncWhitelistPterodactyl(whitelist): async def pterodactylWriteFile(serverid, path, data, apikey): url = f'{init.config().get_pterodactyl_domain()}api/client/servers/{serverid}/files/write?file={urllib.parse.quote(path)}' - requests.post(url, data=data, headers={"Accept": "application/json", "Authorization": f"Bearer {apikey}"}) \ No newline at end of file + requests.post(url, data=data, headers={"Accept": "application/json", "Authorization": f"Bearer {apikey}"}) + +async def isMod(ctx:slash.Context, bot): + allowed = False + for id in mod_roles: + guild = bot.get_guild(ctx.guild.id) + role = guild.get_role(id) + member = await guild.fetch_member(ctx.author.id) + if role in member.roles: + allowed = True + break + + if allowed: + return True + else: + await ctx.respond("Du hast nicht die nötigen Rechte um diesen Befehl auszuführen.", ephemeral=True) #You are not allowed to perform this command + return + +async def isAdmin(ctx:slash.Context, bot): + allowed = False + for id in admin_roles: + guild = bot.get_guild(ctx.guild.id) + role = guild.get_role(id) + member = await guild.fetch_member(ctx.author.id) + if role in member.roles: + allowed = True + break + + if allowed: + return True + else: + await ctx.respond("You are not mighty enough to perform this operation.", ephemeral=True) + return \ No newline at end of file diff --git a/modules/init.py b/modules/init.py index 68071ec..e407a30 100644 --- a/modules/init.py +++ b/modules/init.py @@ -11,8 +11,8 @@ class config(): self.token = p['token'] self.pterodactyl_domain = p['pterodactyl_domain'] self.pterodactyl_apikey = p['pterodactyl_apikey'] - self.guild_id = p['guild_id'] - self.guild_admin_id = p['guild_admin_id'] + self.mod_roles = p['mod_roles'] + self.admin_roles = p['admin_roles'] def get_token(self): return self.token @@ -20,10 +20,10 @@ class config(): return self.pterodactyl_domain def get_pterodactyl_apikey(self): return self.pterodactyl_apikey - def get_guild_id(self): - return self.guild_id - def get_guild_admin_id(self): - return self.guild_admin_id + def get_mod_roles(self): + return self.mod_roles + def get_admin_roles(self): + return self.admin_roles con = sqlite3.connect('data/database.sqlite')