From 8d0aa8b8afb4f277286f81767b62501045887a9b Mon Sep 17 00:00:00 2001 From: SpagettiFisch Date: Wed, 12 Jun 2024 18:59:54 +0200 Subject: [PATCH] added a function for stopping the bot Signed-off-by: SpagettiFisch --- main.py | 6 ++++++ modules/functions.py | 11 +++++++++++ modules/init.py | 7 +++++++ modules/setup.py | 12 ++++++++++-- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index ee02c1e..f186ded 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ 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) @@ -35,4 +36,9 @@ async def mcname(ctx:slash.Context): "Gibt deinen aktuellen Minecraft Namen an" await functions.cmdmcname(ctx) +@bot.slash_cmd(guild_id=guild_id, hidden=True) +async def shutdown(ctx:slash.Context): + "Will shutdown the bot if you are mighty enough." + await functions.cmdshutdown(ctx, bot) + bot.run(token) \ No newline at end of file diff --git a/modules/functions.py b/modules/functions.py index 6b2ea1d..8c5b1cd 100644 --- a/modules/functions.py +++ b/modules/functions.py @@ -58,6 +58,17 @@ async def cmdmcname(ctx:slash.Context): else: await ctx.respond('Du hast deinen Minecraftnamen noch nicht hinzugefügt. Nutze `/mc [name]` um ihn hinzuzufügen.', ephemeral=True) +async def cmdshutdown(ctx:slash.Context, bot): + if str(ctx.channel.id) == str(init.config().get_guild_admin_id()): + await ctx.respond('Logging out and initiating shutdown', ephemeral=True) + print('Start logging out') + await bot.logout() + bot.clear() + print('Log Out succesful\nExiting') + exit() + else: + await ctx.respond('You are not as mighty as you may think you are.') + async def syncWhitelist(): results = cur.execute("SELECT mcname, uuid, iswhitelisted FROM user") results = cur.fetchall() diff --git a/modules/init.py b/modules/init.py index 900e746..68071ec 100644 --- a/modules/init.py +++ b/modules/init.py @@ -11,6 +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'] def get_token(self): return self.token @@ -18,6 +20,11 @@ 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 + con = sqlite3.connect('data/database.sqlite') cur = con.cursor() diff --git a/modules/setup.py b/modules/setup.py index 681408a..b5c2e3c 100644 --- a/modules/setup.py +++ b/modules/setup.py @@ -23,11 +23,17 @@ if not path.exists('config/config.json'): pterodactyl_domain = input() print("Bitte geben Sie den Pterodactyl API Key ein (optional):") pterodactyl_api_key = input() + print("Bitte geben Sie die Guild ID ein:") + guild_id = input() + print("Bitte geben Sie die ID des Admin Channels ein:") + guild_admin_id = input() jsonstructure['discord'].append({ 'token': token, 'pterodactyl_domain': pterodactyl_domain, - 'pterodactyl_apikey': pterodactyl_api_key + 'pterodactyl_apikey': pterodactyl_api_key, + 'guild_id': guild_id, + 'guild_admin_id': guild_admin_id }) elif input1.lower().strip() == 'n': @@ -35,7 +41,9 @@ if not path.exists('config/config.json'): jsonstructure['discord'].append({ 'token': 'Platzhalter', 'pterodactyl_domain': '', - 'pterodactyl_apikey': '' + 'pterodactyl_apikey': '', + 'guild_id': '', + 'guild_admin_id': '' }) else: