From daba2269a6afb28dd3d4291c39e43d97f233dea5 Mon Sep 17 00:00:00 2001 From: LBlend Date: Tue, 15 Nov 2022 01:30:08 +0100 Subject: [PATCH] convert mc whitelist to slash commands & refactor --- src/cogs/mc_whitelist.py | 43 +++++++++++++++++++++------------- src/config/config.yaml.example | 4 ++++ src/run.py | 1 + 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/cogs/mc_whitelist.py b/src/cogs/mc_whitelist.py index e5e57bb..702db34 100644 --- a/src/cogs/mc_whitelist.py +++ b/src/cogs/mc_whitelist.py @@ -1,8 +1,8 @@ +import discord +from discord import app_commands from discord.ext import commands - -import requests -import json from mcrcon import MCRcon +import requests from cogs.utils import embed_templates @@ -14,6 +14,7 @@ class MCWhitelist(commands.Cog): self.init_db() def init_db(self): + """Create the necessary tables for the mc_whitelist cog to work.""" self.cursor.execute( """ CREATE TABLE IF NOT EXISTS mc_whitelist ( @@ -24,46 +25,56 @@ class MCWhitelist(commands.Cog): ) self.bot.db_connection.commit() - @commands.cooldown(1, 10, commands.BucketType.user) - @commands.command() - async def whitelist(self, ctx, minecraftbrukernavn): + @app_commands.checks.cooldown(1, 5) + @app_commands.command() + async def whitelist(self, interaction: discord.Interaction, minecraftbrukernavn: str): + """Whitelist Minecraftbrukeren din på serveren vår""" - # fetch minecraft uuid from api + # Fetch minecraft uuid from api data = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{minecraftbrukernavn}") if data.status_code != 200: - return await ctx.send( - embed=embed_templates.error_fatal(ctx, text=f"Brukeren `{minecraftbrukernavn}` finnes ikk på minecraft") + return await interaction.response.send_message( + embed=embed_templates.error_fatal(interaction, text=f"Brukeren `{minecraftbrukernavn}` finnes ikke på minecraft"), + ephemeral=True ) data = data.json() # check if the discord user or minecraft user is in the db self.cursor.execute( - "SELECT * FROM mc_whitelist WHERE minecraft_id = %s OR discord_id = %s", (data["id"], ctx.author.id) + """ + SELECT * + FROM mc_whitelist + WHERE minecraft_id = %s OR discord_id = %s + """, (data["id"], interaction.user.id) ) if self.cursor.fetchone(): - return await ctx.send( + return await interaction.response.send_message( embed=embed_templates.error_fatal( - ctx, text="Du har allerede whitelisted en bruker eller så er brukeren du oppga whitelisted" + interaction, + text="Du har allerede whitelisted en bruker eller så er brukeren du oppga whitelisted" ) ) - with MCRcon(host="192.168.0.24", password="hallaballayeet", port=25575) as mcr: + # Whitelist user on minecraft server + # Unfortunately, this requires an active connection to the server, with correct credentials + with MCRcon(host="127.0.0.1", password=self.bot.mc_rcon_password, port=25575) as mcr: mcr.command(f"whitelist add {data['name']}") mcr.command("whitelist reload") + # Add user to db self.cursor.execute( """ INSERT INTO mc_whitelist (discord_id, minecraft_id) VALUES (%s, %s) """, - (ctx.author.id, data["id"]), + (interaction.user.id, data["id"]), ) self.bot.db_connection.commit() - await ctx.send( + await interaction.response.send_message( embed=embed_templates.success( - ctx, text=f"`{data['name']}` er nå tilknyttet din discordbruker og whitelisted!" + interaction, text=f"`{data['name']}` er nå tilknyttet din discordbruker og whitelisted!" ) ) diff --git a/src/config/config.yaml.example b/src/config/config.yaml.example index e0d92d0..3c6870f 100644 --- a/src/config/config.yaml.example +++ b/src/config/config.yaml.example @@ -14,6 +14,10 @@ database: username: password: +# Minecraft server +minecraft: + rcon_password: + # Api keys api: clarifai: diff --git a/src/run.py b/src/run.py index 333c532..b71ecae 100644 --- a/src/run.py +++ b/src/run.py @@ -35,6 +35,7 @@ class Bot(commands.Bot): ) # Fetch misc config values + self.mc_rcon_password = config["minecraft"]["rcon_password"] self.presence = config["bot"].get("presence", {}) self.api_keys = config.get("api", {}) self.emoji = config.get("emoji", {})