add logging

This commit is contained in:
Adora Laura Kalb 2024-01-01 15:41:24 +01:00
parent e08a3e55b0
commit 34c81b98a7
Signed by: adoralaura
GPG key ID: 7A4552166FC8C056

40
app.py
View file

@ -4,18 +4,26 @@ from lib import *
import discord import discord
import logging 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 = 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}') logger.info(f'Logged in as {client.user}')
@client.event @client.event
async def on_message(message): async def on_message(message):
#if isinstance(message.channel, discord.channel.DMChannel) or message.channel.name: # Only allow chats in DM or whitelisted channels
# return # 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) # print(message.channel.name)
# Handle own messages # Handle own messages
if message.author == client.user: if message.author == client.user:
@ -23,14 +31,18 @@ async def on_message(message):
# Handle whitelist # Handle whitelist
if message.author.name not in config['bot']['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.") await message.channel.send(f"Sorry {message.author.mention}, you are not authorized to do that.")
return return
if message.content == f"{client.user.mention} ping": 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 return
# if message.content == f"{client.user.mention} version": # if message.content == f"{client.user.mention} version":
# logger.info(f"Version by {message.author.name}")
# await message.channel.send(f"{message.author.mention} ") # await message.channel.send(f"{message.author.mention} ")
# return # return
@ -41,40 +53,50 @@ async def on_message(message):
else: else:
return return
if messagearray[0] == "help": if messagearray[0] == "help":
logger.info(f"Help by {message.author.name}")
await message.channel.send(BuildHelpList()) await message.channel.send(BuildHelpList())
return return
if messagearray[0] in ["list", "info"]: if messagearray[0] in ["list", "info"]:
logger.info(f"{messagearray[0]} by {message.author.name}")
await message.channel.send(BuildGameList()) await message.channel.send(BuildGameList())
return return
if messagearray[0] in ["start", "stop", "restart"] and len(messagearray) >= 2: if messagearray[0] in ["start", "stop", "restart"] and len(messagearray) >= 2:
game = GetGameByGameSlug(messagearray[1]) game = GetGameByGameSlug(messagearray[1])
if not game: if not game:
logger.error(
f"{messagearray[0]} for invalid Game \"{messagearray[1]}\" by {message.author.name}")
await message.add_reaction("") await message.add_reaction("")
await message.channel.send(f"Invalid game: {messagearray[1]}") await message.channel.send(f"Invalid game: {messagearray[1]}")
return return
logger.info(
f"{messagearray[0]} by {message.author.name}, Game: {messagearray[1]}")
success = HandleSystemdService(game, messagearray[0]) success = HandleSystemdService(game, messagearray[0])
# :white_check_mark: :x: # :white_check_mark: :x:
if success: if success:
logger.info(
f"{messagearray[0]} by {message.author.name}, Game: {messagearray[1]} was SUCESSFUL")
await message.add_reaction("") await message.add_reaction("")
else: else:
logger.error(
f"{messagearray[0]} by {message.author.name}, Game: {messagearray[1]} FAILED!")
await message.add_reaction("") await message.add_reaction("")
await message.channel.send(f"Sorry {message.author.mention}, that didn't work. Ping @lauralani") await message.channel.send(f"Sorry {message.author.mention}, that didn't work. Ping @lauralani")
if messagearray[0] == "status": if messagearray[0] == "status":
status = BuildSingleGameStatus(messagearray[1]) status = BuildSingleGameStatus(messagearray[1])
if not status: if not status:
logger.error(
f"Status for invalid Game {messagearray[1]} by {message.author.name}")
await message.add_reaction("") await message.add_reaction("")
await message.channel.send(f"Invalid game: {messagearray[1]}") await message.channel.send(f"Invalid game: {messagearray[1]}")
return return
logger.info(
f"Status for Game {messagearray[1]} by {message.author.name}")
await message.channel.send(status) await message.channel.send(status)
return return
client.run(config['bot']['token']) client.run(config['bot']['token'])