Add some logging and readme for PWSBot

This commit is contained in:
whitney 2024-05-16 15:01:53 -07:00
parent 426ab7bf89
commit f1dbb82e1d
5 changed files with 50 additions and 9 deletions

23
pwsBot/README.md Normal file
View File

@ -0,0 +1,23 @@
#PWSBot
Simple Discord bot that does some useful things.
### Running
Build and run the docker container with a few commands:
`docker stop pwsBot && docker system prune && docker-compose build && docker-compose up -d && docker logs -f pwsBot`
### Functions
##### Echo
`/pws echo [text]`
The bot will echo the text in the channel.
##### Hello World
`/pws helloworld`
Prints "Hello, World!" to the channel.

View File

@ -1,4 +1,6 @@
import discord import discord
import logging
import sys
from resource.credentials import get_token from resource.credentials import get_token
from resource.intents import get_intents from resource.intents import get_intents
@ -9,9 +11,12 @@ intents = get_intents()
client = discord.Client(intents=intents) client = discord.Client(intents=intents)
client_token = get_token('resource/token.json') client_token = get_token('resource/token.json')
log_format = '%(asctime)s %(levelname)s\t%(filename)s %(message)s'
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=log_format)
@client.event @client.event
async def on_ready(): async def on_ready():
print('Logged in as {0.user}'.format(client)) logging.info('Logged in as {0.user}'.format(client))
@client.event @client.event
async def on_message(message): async def on_message(message):

View File

@ -1,4 +1,6 @@
import logging
import re import re
import sys
from commands.echo import echo from commands.echo import echo
from commands.hello_world import hello_world from commands.hello_world import hello_world
@ -7,8 +9,11 @@ pws_command_prefix = '/pws '
pws_command_regex = r'/pws (\S+)' pws_command_regex = r'/pws (\S+)'
pws_command_with_args_regex = r'/pws \S+(.*)' pws_command_with_args_regex = r'/pws \S+(.*)'
log_format = '%(asctime)s %(levelname)s\t%(filename)s %(message)s'
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=log_format)
async def handle_message(client, message): async def handle_message(client, message):
print(message.content) logging.info('Received message: ' + str(message.content))
#Ignore messages this bot has sent to avoid answering itself. #Ignore messages this bot has sent to avoid answering itself.
if message.author == client.user: if message.author == client.user:
@ -23,20 +28,22 @@ async def handle_message(client, message):
command = msg[2] command = msg[2]
args = msg[3] args = msg[3]
print("command: " + command + ", args: " + args) logging.info("Received Command: " + command + ", with Args: " + args)
match command: match command:
case 'helloworld': case 'helloworld':
if args: if args:
print('Command ' + command + ' got unexpected arguments: ' + message.content + '.') logging.warn('Command ' + command + ' got unexpected arguments: ' + message.content + '.')
await message.author.send('Unexpected arguments for command: `' + message.content + '`. Check list of commands for valid usage.') await message.author.send('Unexpected arguments for command: `' + message.content + '`. Check list of commands for valid usage.')
else: else:
await hello_world(message) await hello_world(message)
case 'echo': case 'echo':
if not args: if not args:
print('Command ' + command + ' did not specify argument(s): ' + message.content + '.') logging.warn('Command ' + command + ' did not specify argument(s): ' + message.content + '.')
await message.author.send('Did not specify argument(s) for command: `' + message.content + '`. Check list of commands for valid usage.') await message.author.send('Did not specify argument(s) for command: `' + message.content + '`. Check list of commands for valid usage.')
else:
await echo(message)
case _: case _:
print('Invalid command ' + command + ' received.') logging.warn('Invalid command ' + command + ' received.')
await message.author.send('Invalid command `' + command + '`. Check list of commands for valid usage.') await message.author.send('Invalid command `' + command + '`. Check list of commands for valid usage.')

View File

@ -5,4 +5,6 @@ services:
image: pwsbot:latest image: pwsbot:latest
container_name: pwsBot container_name: pwsBot
build: . build: .
environment:
- PYTHONUNBUFFERED=1

View File

@ -1,8 +1,12 @@
import json import json
import logging
import sys import sys
TOKEN = 'token' TOKEN = 'token'
log_format = '%(asctime)s %(levelname)s\t%(filename)s %(message)s'
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=log_format)
def get_token(file_path): def get_token(file_path):
try: try:
with open(file_path, 'r') as file: with open(file_path, 'r') as file:
@ -10,10 +14,10 @@ def get_token(file_path):
if TOKEN in data: if TOKEN in data:
return data[TOKEN] return data[TOKEN]
else: else:
print("Bot token file missing " + str(TOKEN) + " field. See README.") logging.error("Bot token file missing " + str(TOKEN) + " field. See README.")
except FileNotFoundError: except FileNotFoundError:
print("No bot token file. See README.") logging.error("No bot token file. See README.")
except json.JSONDecodeError: except json.JSONDecodeError:
print("Invalid JSON format for bot token file. See README.") logging.error("Invalid JSON format for bot token file. See README.")
sys.exit(1) sys.exit(1)