game-discord-bot/app.py

75 lines
2.2 KiB
Python
Raw Normal View History

2024-01-01 15:08:48 +01:00
#!/usr/bin/env python3
2023-12-31 14:56:45 +01:00
from config import config
2024-01-01 13:10:24 +01:00
from lib import *
2023-12-31 14:56:45 +01:00
import discord
2024-01-01 13:10:24 +01:00
import logging
2023-12-31 14:56:45 +01:00
2024-01-01 13:10:24 +01:00
intents = discord.Intents(messages=True,message_content=True)
client = discord.Client(intents=intents)
2023-12-31 14:56:45 +01:00
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
@client.event
async def on_message(message):
2024-01-01 13:10:24 +01:00
#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
2024-01-01 15:08:48 +01:00
#if message.content == f"{client.user.mention} version":
# await message.channel.send(f"{message.author.mention} ")
# return
2024-01-01 13:10:24 +01:00
# Handle @Bot mention
messagearray = message.content.split()
if messagearray[0] == client.user.mention:
messagearray.pop(0)
2024-01-01 15:08:48 +01:00
else:
return
2023-12-31 14:56:45 +01:00
2024-01-01 13:10:24 +01:00
if messagearray[0] == "help":
await message.channel.send(BuildHelpList())
return
2024-01-01 15:08:48 +01:00
if messagearray[0] in ["list", "info"]:
2024-01-01 13:10:24 +01:00
await message.channel.send(BuildGameList())
return
2023-12-31 14:56:45 +01:00
2024-01-01 13:10:24 +01:00
if messagearray[0] in ["start", "stop", "restart"] and len(messagearray) >= 2:
2024-01-01 15:08:48 +01:00
game = GetGameByGameSlug(messagearray[1])
if not game:
await message.add_reaction("")
await message.channel.send(f"Invalid game: {messagearray[1]}")
return
success = HandleSystemdService(game, messagearray[0])
2024-01-01 13:10:24 +01:00
# :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":
2024-01-01 15:08:48 +01:00
await message.channel.send(BuildSingleGameStatus(messagearray[1]))
2024-01-01 13:10:24 +01:00
return
2023-12-31 14:56:45 +01:00
client.run(config['bot']['token'])