diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..343bdc4 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Discord Bot", + "type": "python", + "request": "launch", + "program": "app.py", + "console": "integratedTerminal", + "justMyCode": true + } + ] +} \ No newline at end of file diff --git a/app.py b/app.py index 50b7b69..40c82a6 100644 --- a/app.py +++ b/app.py @@ -1,22 +1,62 @@ from config import config +from lib import * import discord +import logging -intents = discord.Intents(messages=True) + +intents = discord.Intents(messages=True,message_content=True) client = discord.Client(intents=intents) - @client.event async def on_ready(): print(f'We have logged in as {client.user}') @client.event async def on_message(message): - if message.author.name in config['bot']['whitelist']: - if message.content == 'info': - await message.channel.send('Hello!') - else: - print("user not authorized") + #if isinstance(message.channel, discord.channel.DMChannel) or message.channel.name: + # return + #print(message.channel.name) + # Handle own messages + if message.author == client.user: + return + # Handle whitelist + if message.author.name not in config['bot']['whitelist']: + await message.channel.send(f"Sorry {message.author.mention}, you are not authorized to do that.") + return + if message.content == f"{client.user.mention} ping": + await message.channel.send(f"{message.author.mention} ") + return + + # Handle @Bot mention + messagearray = message.content.split() + if messagearray[0] == client.user.mention: + messagearray.pop(0) + + + + if messagearray[0] == "help": + await message.channel.send(BuildHelpList()) + return + if messagearray[0] == "list": + await message.channel.send(BuildGameList()) + return + + if messagearray[0] in ["start", "stop", "restart"] and len(messagearray) >= 2: + success = HandleSystemdService(messagearray[1], messagearray[0]) + + # :white_check_mark: :x: + if success: + await message.add_reaction("✅") + else: + await message.add_reaction("❌") + await message.channel.send(f"Sorry {message.author.mention}, that didn't work. Ping @lauralani") + + if messagearray[0] == "status": + await message.channel.send(BuildSingleGameStatus()) + return + + client.run(config['bot']['token']) \ No newline at end of file diff --git a/lib.py b/lib.py new file mode 100644 index 0000000..2b1c5ab --- /dev/null +++ b/lib.py @@ -0,0 +1,33 @@ +from config import config +import subprocess + +def BuildGameList(): + retlist = "List of currently manageable games:\n\n" + gamearray = [] + for game in config['games']: + line = f"**{game['displayname']}**\n- Status: **{CheckSystemdService(game['systemd'])}**\n- Slug: `{game['slug']}`" + gamearray.append(line) + retlist = retlist + "\n".join(gamearray) + + return retlist + +def BuildSingleGameStatus(game): + return f"**{game['displayname']}**\n- Status: **{CheckSystemdService(game['systemd'])}**\n- Slug: `{game['slug']}`" + +def CheckSystemdService(service : str): + stat = subprocess.call(["systemctl", "is-active", "--quiet", service]) + return "🟢 UP" if not bool(stat) else "🔴 down" + +def HandleSystemdService(service : str, action : str): + stat = subprocess.call(["systemctl", action, "--quiet", service]) + return not bool(stat) + +def BuildHelpList(): + return """Bot has the following commands: + `help`: Get this help message + `info`: List all available games, their status and their slug + `start `: Start game with slug __ + `stop `: Stop game with slug __ + `restart `: Stop game with slug __ + `status `: Show status for game with slug __ + """ \ No newline at end of file