126 lines
5 KiB
Python
126 lines
5 KiB
Python
#!/usr/bin/env python3
|
|
from config import config
|
|
from lib import *
|
|
|
|
import discord
|
|
import logging
|
|
logger = logging.getLogger('discord.gamebot')
|
|
logger.setLevel(logging.INFO)
|
|
|
|
intents = discord.Intents(messages=True, message_content=True)
|
|
client = discord.Client(intents=intents)
|
|
|
|
|
|
@client.event
|
|
async def on_ready():
|
|
logger.info(f'Logged in as {client.user}')
|
|
|
|
|
|
@client.event
|
|
async def on_message(message):
|
|
# Only allow chats in DM or whitelisted channels
|
|
if not (isinstance(message.channel, discord.channel.DMChannel)) and not (message.channel.id in config['bot']['channels']):
|
|
return
|
|
|
|
# Handle own messages
|
|
if message.author == client.user:
|
|
return
|
|
|
|
# Handle whitelist
|
|
if message.author.name not in config['bot']['whitelist']:
|
|
logger.error(
|
|
f"Unauthorized command by {message.author.name}. message: \"{message.content}\"")
|
|
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":
|
|
logger.info(f"Ping by {message.author.name}")
|
|
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)
|
|
else:
|
|
return
|
|
|
|
command = messagearray[0].lower()
|
|
|
|
# message starts with "help"
|
|
if command == "help":
|
|
logger.info(f"Help by {message.author.name}")
|
|
await message.channel.send(BuildHelpList())
|
|
return
|
|
|
|
# message starts with "list" or "info"
|
|
if command in ["list", "info"]:
|
|
logger.info(f"{command} by {message.author.name}")
|
|
await message.channel.send(BuildGameList())
|
|
return
|
|
|
|
if command in ["start", "stop", "restart"]:
|
|
if len(messagearray) != 2:
|
|
logger.error(
|
|
f"{command} with invalid numer of arguments by {message.author.name}. Command: \"{message.content}\"")
|
|
await message.add_reaction("❌")
|
|
await message.channel.send(f"Invalid number of arguments")
|
|
return
|
|
game = GetGameByGameSlug(messagearray[1])
|
|
if not game:
|
|
logger.error(
|
|
f"{command} for invalid Game \"{messagearray[1]}\" by {message.author.name}")
|
|
await message.add_reaction("❌")
|
|
await message.channel.send(f"Invalid game: {messagearray[1]}")
|
|
return
|
|
logger.info(
|
|
f"{command} by {message.author.name}, Game: {messagearray[1]}")
|
|
await message.add_reaction("⏳")
|
|
success = HandleSystemdService(game, command)
|
|
|
|
# :white_check_mark: :x:
|
|
if success:
|
|
await message.remove_reaction("⏳")
|
|
logger.info(
|
|
f"{command} by {message.author.name}, Game: {messagearray[1]} was SUCESSFUL")
|
|
await message.add_reaction("✅")
|
|
if command == "start":
|
|
await message.channel.send(f"Successfully started {game['displayname']}! Happy gaming {message.author.mention}! 🎮")
|
|
elif command == "stop":
|
|
await message.channel.send(f"Successfully stopped {game['displayname']}! Good night {message.author.mention}! 😴💤")
|
|
elif command == "restart":
|
|
await message.channel.send(f"Successfully restarted {game['displayname']}! Happy gaming {message.author.mention}! 🎮")
|
|
else:
|
|
await message.remove_reaction("⏳")
|
|
logger.error(
|
|
f"{command} by {message.author.name}, Game: {messagearray[1]} FAILED!")
|
|
await message.add_reaction("❌")
|
|
await message.channel.send(f"Sorry {message.author.mention}, that didn't work. Ping @lauralani")
|
|
|
|
if command == "status":
|
|
if len(messagearray) < 2:
|
|
logger.error(
|
|
f"Status with no arguments by {message.author.name}. Command: \"{message.content}\"")
|
|
await message.add_reaction("❌")
|
|
await message.channel.send(f"{message.author.mention}: This comand is for checking the status of a single game!\nDid you maybe mean to use the `info` command to display all games?")
|
|
return
|
|
if len(messagearray) > 2:
|
|
logger.error(
|
|
f"Status with invalid numer of arguments by {message.author.name}. Command: \"{message.content}\"")
|
|
await message.add_reaction("❌")
|
|
await message.channel.send(f"Invalid number of arguments")
|
|
return
|
|
status = BuildSingleGameStatus(messagearray[1])
|
|
if not status:
|
|
logger.error(
|
|
f"Status for invalid Game {messagearray[1]} by {message.author.name}")
|
|
await message.add_reaction("❌")
|
|
await message.channel.send(f"Invalid game: {messagearray[1]}")
|
|
return
|
|
logger.info(
|
|
f"Status for Game {messagearray[1]} by {message.author.name}")
|
|
await message.channel.send(status)
|
|
return
|
|
|
|
|
|
client.run(config['bot']['token'])
|