convert mc whitelist to slash commands & refactor

This commit is contained in:
LBlend committed 2022-11-15 01:30:08 +01:00
1 parent 10ed39edd4
commit daba2269a6
3 files changed
+32 -16

No files matched your search

+27 -16
View File
@@ -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!"
)
)
+4
View File
@@ -14,6 +14,10 @@ database:
username:
password:
# Minecraft server
minecraft:
rcon_password:
# Api keys
api:
clarifai:
+1
View File
@@ -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", {})