From a74f3513b32f69c1ee803969e573e45c2e1e2772 Mon Sep 17 00:00:00 2001 From: "Ryan J. Gray" Date: Sun, 30 Oct 2022 12:07:17 +0000 Subject: [PATCH] Initial work on command groups and cogs --- Pipfile | 2 +- Pipfile.lock | 12 +++--- app/bot.py | 90 +++------------------------------------- app/cogs/config.py | 36 ++++++++++++++++ app/cogs/poweractions.py | 59 ++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 91 deletions(-) create mode 100644 app/cogs/config.py create mode 100644 app/cogs/poweractions.py diff --git a/Pipfile b/Pipfile index 028616b..f9caab7 100644 --- a/Pipfile +++ b/Pipfile @@ -12,4 +12,4 @@ pylint = "*" autopep8 = "*" [requires] -python_version = "3.8" +python_version = "3.11" diff --git a/Pipfile.lock b/Pipfile.lock index db9851c..ba6002f 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,11 +1,11 @@ { "_meta": { "hash": { - "sha256": "b15e2973f84e61990d7951a4d8e597d50c5312545a76ab329e6f296ba49d7a06" + "sha256": "9d4885e682572bc2fce3e530983ce226bf26c37cc64eddc1c2abed4c913b2fcc" }, "pipfile-spec": 6, "requires": { - "python_version": "3.8" + "python_version": "3.11" }, "sources": [ { @@ -388,11 +388,11 @@ }, "autopep8": { "hashes": [ - "sha256:99afd5a60d30c6d426319e15db2ed7833d2da38f7f94a8753a1953509cc78e5d", - "sha256:f0058220e4cc0ef6121996fc8ec1c32f0735e446be23c4e1f692de0bf23174dd" + "sha256:8b1659c7f003e693199f52caffdc06585bb0716900bbc6a7442fd931d658c077", + "sha256:ad924b42c2e27a1ac58e432166cc4588f5b80747de02d0d35b1ecbd3e7d57207" ], "index": "pypi", - "version": "==1.7.1" + "version": "==2.0.0" }, "dill": { "hashes": [ @@ -558,7 +558,7 @@ "sha256:ee6acae74a2b91865910eef5e7de37dc6895ad96fa23603d1d27ea69df545015", "sha256:ef3f72c9666bba2bab70d2a8b79f2c6d2c1a42a7f7e2b0ec83bb2f9e383950af" ], - "markers": "python_version < '3.11'", + "markers": "python_version >= '3.11'", "version": "==1.14.1" } } diff --git a/app/bot.py b/app/bot.py index 4c6a91e..ae8652e 100644 --- a/app/bot.py +++ b/app/bot.py @@ -1,49 +1,12 @@ """ Discord bot that controls the power of a physical server """ -import os -import sys -import json import traceback import requests import discord +from cogs import config -def env_defined(key): - """ - Checks if a given env var key is defined in the OS environment - """ - return key in os.environ and len(os.environ[key]) > 0 - - -DISCORD_CHANNEL = [] -# env variables are defaults, if no config file exists it'll be created. -# If no env is set, stop the bot -if not env_defined("DISCORD_TOKEN"): - print("Missing bot token from .env") - sys.exit() -DISCORD_TOKEN = os.environ["DISCORD_TOKEN"] - -if not env_defined("WOL_URL"): - print("Missing wake on lan URL from .env") - sys.exit() -WOL_URL = os.environ["WOL_URL"] - -if not env_defined("SHUTDOWN_URL"): - print("Missing shutdown URL from .env") - sys.exit() -SHUTDOWN_URL = os.environ["SHUTDOWN_URL"] - -if not env_defined("REBOOT_URL"): - print("Missing liveness URL from .env") - sys.exit() -REBOOT_URL = os.environ["REBOOT_URL"] - -if not env_defined("LIVENESS_URL"): - print("Missing liveness URL from .env") - sys.exit() -LIVENESS_URL = os.environ["LIVENESS_URL"] - intents = discord.Intents.default() DESC = "Bot to control the power to physical game server" bot = discord.Bot(description=DESC, intents=intents) @@ -60,53 +23,10 @@ async def on_ready(): print('Connected to API') -@ bot.slash_command(name="boot", description="Boots the game server") -async def _boot(ctx): - try: - response = requests.get(WOL_URL, timeout=2) - jsonresponse = json.loads(response.content.decode()) - if jsonresponse.get('success') is True: - game = discord.Activity( - name="Booting...", type=discord.ActivityType.playing) - await bot.change_presence(status=discord.Status.online, activity=game) - await ctx.respond('Server booted!') - except Exception: - await ctx.respond('Something went wrong, have an adult check the logs') - traceback.print_exc() - - -@ bot.slash_command(name="shutdown", description="Shuts down the game server") -async def _shutdown(ctx): - try: - response = requests.get(SHUTDOWN_URL, timeout=2) - if response.status_code == 200: - game = discord.Activity( - name="Powering down...", type=discord.ActivityType.playing) - await bot.change_presence(status=discord.Status.do_not_disturb, activity=game) - await ctx.respond('Server shut down!') - except Exception: - await ctx.respond('Server is already offline') - traceback.print_exc() - - -@ bot.slash_command(name="reboot", description="Reboots the game server") -async def _reboot(ctx): - try: - response = requests.get(REBOOT_URL, timeout=2) - if response.status_code == 200: - game = discord.Activity( - name="Rebooting...", type=discord.ActivityType.playing) - await bot.change_presence(status=discord.Status.streaming, activity=game) - await ctx.respond('Server rebooting!') - except Exception: - await ctx.respond('Server is already offline') - traceback.print_exc() - - -@ bot.slash_command(name="status", description="Checks current power status of game server") +@bot.slash_command(name="status", description="Checks current power status of game server") async def _status(ctx): try: - response = requests.get(LIVENESS_URL, timeout=2) + response = requests.get(config.LIVENESS_URL, timeout=2) if response.status_code == 200: game = discord.Activity( name="Server online", type=discord.ActivityType.playing) @@ -120,4 +40,6 @@ async def _status(ctx): print("Server host is offline") traceback.print_exc() -bot.run(DISCORD_TOKEN) +bot.load_extension('cogs.poweractions') + +bot.run(config.DISCORD_TOKEN) diff --git a/app/cogs/config.py b/app/cogs/config.py new file mode 100644 index 0000000..64a9052 --- /dev/null +++ b/app/cogs/config.py @@ -0,0 +1,36 @@ +import os +import sys + + +def env_defined(key): + """ + Checks if a given env var key is defined in the OS environment + """ + return key in os.environ and len(os.environ[key]) > 0 + +# env variables are defaults, if no config file exists it'll be created. +# If no env is set, stop the bot +if not env_defined("DISCORD_TOKEN"): + print("Missing bot token from .env") + sys.exit() +DISCORD_TOKEN = os.environ["DISCORD_TOKEN"] + +if not env_defined("WOL_URL"): + print("Missing wake on lan URL from .env") + sys.exit() +WOL_URL = os.environ["WOL_URL"] + +if not env_defined("SHUTDOWN_URL"): + print("Missing shutdown URL from .env") + sys.exit() +SHUTDOWN_URL = os.environ["SHUTDOWN_URL"] + +if not env_defined("REBOOT_URL"): + print("Missing liveness URL from .env") + sys.exit() +REBOOT_URL = os.environ["REBOOT_URL"] + +if not env_defined("LIVENESS_URL"): + print("Missing liveness URL from .env") + sys.exit() +LIVENESS_URL = os.environ["LIVENESS_URL"] diff --git a/app/cogs/poweractions.py b/app/cogs/poweractions.py new file mode 100644 index 0000000..74e7c0c --- /dev/null +++ b/app/cogs/poweractions.py @@ -0,0 +1,59 @@ +import json +import traceback +import requests +import discord +from discord.ext import commands +import config + + +class PowerActions(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.group() + async def actions(self, ext): + pass + + @actions.slash_command(name="boot", description="Boots the game server") + async def _boot(self, ctx): + try: + response = requests.get(config.WOL_URL, timeout=2) + jsonresponse = json.loads(response.content.decode()) + if jsonresponse.get('success') is True: + game = discord.Activity( + name="Booting...", type=discord.ActivityType.playing) + await self.bot.change_presence(status=discord.Status.online, activity=game) + await ctx.respond('Server booted!') + except Exception: + await ctx.respond('Something went wrong, have an adult check the logs') + traceback.print_exc() + + @actions.slash_command(name="shutdown", description="Shuts down the game server") + async def _shutdown(self, ctx): + try: + response = requests.get(config.SHUTDOWN_URL, timeout=2) + if response.status_code == 200: + game = discord.Activity( + name="Powering down...", type=discord.ActivityType.playing) + await self.bot.change_presence(status=discord.Status.do_not_disturb, activity=game) + await ctx.respond('Server shut down!') + except Exception: + await ctx.respond('Server is already offline') + traceback.print_exc() + + @actions.slash_command(name="reboot", description="Reboots the game server") + async def _reboot(self, ctx): + try: + response = requests.get(config.REBOOT_URL, timeout=2) + if response.status_code == 200: + game = discord.Activity( + name="Rebooting...", type=discord.ActivityType.playing) + await self.bot.change_presence(status=discord.Status.streaming, activity=game) + await ctx.respond('Server rebooting!') + except Exception: + await ctx.respond('Server is already offline') + traceback.print_exc() + + +def setup(bot): + bot.add_cog(PowerActions(bot))