add some functionality

This commit is contained in:
Adora Laura Kalb 2024-01-01 13:10:24 +01:00
parent 9fa10528d5
commit f07219aa8e
Signed by: adoralaura
GPG key ID: 7A4552166FC8C056
3 changed files with 96 additions and 7 deletions

16
.vscode/launch.json vendored Normal file
View file

@ -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
}
]
}

52
app.py
View file

@ -1,21 +1,61 @@
from config import config from config import config
from lib import *
import discord import discord
import logging
intents = discord.Intents(messages=True)
intents = discord.Intents(messages=True,message_content=True)
client = discord.Client(intents=intents) client = discord.Client(intents=intents)
@client.event @client.event
async def on_ready(): async def on_ready():
print(f'We have logged in as {client.user}') print(f'We have logged in as {client.user}')
@client.event @client.event
async def on_message(message): async def on_message(message):
if message.author.name in config['bot']['whitelist']: #if isinstance(message.channel, discord.channel.DMChannel) or message.channel.name:
if message.content == 'info': # return
await message.channel.send('Hello!') #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: else:
print("user not authorized") 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

33
lib.py Normal file
View file

@ -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 <slug>`: Start game with slug _<slug>_
`stop <slug>`: Stop game with slug _<slug>_
`restart <slug>`: Stop game with slug _<slug>_
`status <slug>`: Show status for game with slug _<slug>_
"""