2024-01-01 15:08:48 +01:00
|
|
|
#!/usr/bin/env python3
|
2024-01-01 15:41:24 +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
|
2024-01-01 15:41:24 +01:00
|
|
|
logger = logging.getLogger('discord.gamebot')
|
|
|
|
logger.setLevel(logging.INFO)
|
2023-12-31 14:56:45 +01:00
|
|
|
|
2024-01-01 15:41:24 +01:00
|
|
|
intents = discord.Intents(messages=True, message_content=True)
|
2024-01-01 13:10:24 +01:00
|
|
|
client = discord.Client(intents=intents)
|
2024-01-01 15:41:24 +01:00
|
|
|
|
|
|
|
|
2023-12-31 14:56:45 +01:00
|
|
|
@client.event
|
|
|
|
async def on_ready():
|
2024-01-01 15:41:24 +01:00
|
|
|
logger.info(f'Logged in as {client.user}')
|
|
|
|
|
2023-12-31 14:56:45 +01:00
|
|
|
|
|
|
|
@client.event
|
|
|
|
async def on_message(message):
|
2024-01-01 15:41:24 +01:00
|
|
|
# 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
|
2024-01-01 18:01:33 +01:00
|
|
|
|
2024-01-01 13:10:24 +01:00
|
|
|
# Handle own messages
|
|
|
|
if message.author == client.user:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Handle whitelist
|
|
|
|
if message.author.name not in config['bot']['whitelist']:
|
2024-01-01 15:41:24 +01:00
|
|
|
logger.error(
|
|
|
|
f"Unauthorized command by {message.author.name}. message: \"{message.content}\"")
|
2024-01-01 13:10:24 +01:00
|
|
|
await message.channel.send(f"Sorry {message.author.mention}, you are not authorized to do that.")
|
|
|
|
return
|
2024-01-01 15:41:24 +01:00
|
|
|
|
2024-01-01 13:10:24 +01:00
|
|
|
if message.content == f"{client.user.mention} ping":
|
2024-01-01 15:41:24 +01:00
|
|
|
logger.info(f"Ping by {message.author.name}")
|
|
|
|
await message.channel.send(f"{message.author.mention} 👋")
|
2024-01-01 13:10:24 +01:00
|
|
|
return
|
2024-01-01 15:08:48 +01:00
|
|
|
|
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
|
2024-01-01 13:10:24 +01:00
|
|
|
|
2024-01-01 18:01:33 +01:00
|
|
|
command = messagearray[0].lower()
|
|
|
|
|
|
|
|
# message starts with "help"
|
|
|
|
if command == "help":
|
2024-01-01 15:41:24 +01:00
|
|
|
logger.info(f"Help by {message.author.name}")
|
|
|
|
await message.channel.send(BuildHelpList())
|
2024-01-01 13:10:24 +01:00
|
|
|
return
|
2024-01-01 18:01:33 +01:00
|
|
|
|
|
|
|
# message starts with "list" or "info"
|
|
|
|
if command in ["list", "info"]:
|
|
|
|
logger.info(f"{command} by {message.author.name}")
|
2024-01-01 13:10:24 +01:00
|
|
|
await message.channel.send(BuildGameList())
|
|
|
|
return
|
2024-01-01 15:41:24 +01:00
|
|
|
|
2024-01-01 18:01:33 +01:00
|
|
|
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
|
2024-01-01 15:08:48 +01:00
|
|
|
game = GetGameByGameSlug(messagearray[1])
|
|
|
|
if not game:
|
2024-01-01 15:41:24 +01:00
|
|
|
logger.error(
|
2024-01-01 18:01:33 +01:00
|
|
|
f"{command} for invalid Game \"{messagearray[1]}\" by {message.author.name}")
|
2024-01-01 15:08:48 +01:00
|
|
|
await message.add_reaction("❌")
|
|
|
|
await message.channel.send(f"Invalid game: {messagearray[1]}")
|
|
|
|
return
|
2024-01-01 15:41:24 +01:00
|
|
|
logger.info(
|
2024-01-01 18:01:33 +01:00
|
|
|
f"{command} by {message.author.name}, Game: {messagearray[1]}")
|
2024-01-01 18:12:09 +01:00
|
|
|
await message.add_reaction("⏳")
|
2024-01-01 18:01:33 +01:00
|
|
|
success = HandleSystemdService(game, command)
|
2024-01-01 15:41:24 +01:00
|
|
|
|
2024-01-01 13:10:24 +01:00
|
|
|
# :white_check_mark: :x:
|
|
|
|
if success:
|
2024-01-01 18:12:09 +01:00
|
|
|
await message.remove_reaction("⏳")
|
2024-01-01 15:41:24 +01:00
|
|
|
logger.info(
|
2024-01-01 18:01:33 +01:00
|
|
|
f"{command} by {message.author.name}, Game: {messagearray[1]} was SUCESSFUL")
|
2024-01-01 13:10:24 +01:00
|
|
|
await message.add_reaction("✅")
|
2024-01-01 18:14:43 +01:00
|
|
|
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}! 🎮")
|
2024-01-01 13:10:24 +01:00
|
|
|
else:
|
2024-01-01 18:12:09 +01:00
|
|
|
await message.remove_reaction("⏳")
|
2024-01-01 15:41:24 +01:00
|
|
|
logger.error(
|
2024-01-01 18:01:33 +01:00
|
|
|
f"{command} by {message.author.name}, Game: {messagearray[1]} FAILED!")
|
2024-01-01 13:10:24 +01:00
|
|
|
await message.add_reaction("❌")
|
|
|
|
await message.channel.send(f"Sorry {message.author.mention}, that didn't work. Ping @lauralani")
|
|
|
|
|
2024-01-01 18:01:33 +01:00
|
|
|
if command == "status":
|
|
|
|
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
|
2024-01-01 15:12:32 +01:00
|
|
|
status = BuildSingleGameStatus(messagearray[1])
|
|
|
|
if not status:
|
2024-01-01 15:41:24 +01:00
|
|
|
logger.error(
|
|
|
|
f"Status for invalid Game {messagearray[1]} by {message.author.name}")
|
2024-01-01 15:12:32 +01:00
|
|
|
await message.add_reaction("❌")
|
|
|
|
await message.channel.send(f"Invalid game: {messagearray[1]}")
|
|
|
|
return
|
2024-01-01 15:41:24 +01:00
|
|
|
logger.info(
|
|
|
|
f"Status for Game {messagearray[1]} by {message.author.name}")
|
2024-01-01 15:12:32 +01:00
|
|
|
await message.channel.send(status)
|
2024-01-01 13:10:24 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
|
2024-01-01 15:41:24 +01:00
|
|
|
client.run(config['bot']['token'])
|