From 34c81b98a7a59802483c5ca76942202b094d1f3e Mon Sep 17 00:00:00 2001 From: lauralani Date: Mon, 1 Jan 2024 15:41:24 +0100 Subject: [PATCH] add logging --- app.py | 62 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/app.py b/app.py index db2e83f..441a8ce 100644 --- a/app.py +++ b/app.py @@ -1,80 +1,102 @@ #!/usr/bin/env python3 -from config import config +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) +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}') + logger.info(f'Logged in as {client.user}') + @client.event async def on_message(message): - #if isinstance(message.channel, discord.channel.DMChannel) or message.channel.name: - # return - #print(message.channel.name) + # Only allow chats in DM or whitelisted channels + # TODO: FIX + if not (isinstance(message.channel, discord.channel.DMChannel)) and not (message.channel.id in config['bot']['channels']): + print(isinstance(message.channel, discord.channel.DMChannel)) + print(message.channel.id) + 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']: + 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": - await message.channel.send(f"{message.author.mention} ") + logger.info(f"Ping by {message.author.name}") + await message.channel.send(f"{message.author.mention} 👋") return - #if message.content == f"{client.user.mention} version": - # await message.channel.send(f"{message.author.mention} ") + # if message.content == f"{client.user.mention} version": + # logger.info(f"Version 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 - - - if messagearray[0] == "help": - await message.channel.send(BuildHelpList()) + logger.info(f"Help by {message.author.name}") + await message.channel.send(BuildHelpList()) return if messagearray[0] in ["list", "info"]: + logger.info(f"{messagearray[0]} by {message.author.name}") await message.channel.send(BuildGameList()) return - + if messagearray[0] in ["start", "stop", "restart"] and len(messagearray) >= 2: game = GetGameByGameSlug(messagearray[1]) if not game: + logger.error( + f"{messagearray[0]} 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"{messagearray[0]} by {message.author.name}, Game: {messagearray[1]}") success = HandleSystemdService(game, messagearray[0]) - + # :white_check_mark: :x: if success: + logger.info( + f"{messagearray[0]} by {message.author.name}, Game: {messagearray[1]} was SUCESSFUL") await message.add_reaction("✅") else: + logger.error( + f"{messagearray[0]} 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 messagearray[0] == "status": 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']) \ No newline at end of file +client.run(config['bot']['token'])