diff --git a/pwsBot/README.md b/pwsBot/README.md new file mode 100644 index 0000000..8f89977 --- /dev/null +++ b/pwsBot/README.md @@ -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. + diff --git a/pwsBot/bot.py b/pwsBot/bot.py index 7a56f36..1b03956 100644 --- a/pwsBot/bot.py +++ b/pwsBot/bot.py @@ -1,4 +1,6 @@ import discord +import logging +import sys from resource.credentials import get_token from resource.intents import get_intents @@ -9,9 +11,12 @@ intents = get_intents() client = discord.Client(intents=intents) 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 async def on_ready(): - print('Logged in as {0.user}'.format(client)) + logging.info('Logged in as {0.user}'.format(client)) @client.event async def on_message(message): diff --git a/pwsBot/commands/message_handler.py b/pwsBot/commands/message_handler.py index 3782b99..c4ff22d 100644 --- a/pwsBot/commands/message_handler.py +++ b/pwsBot/commands/message_handler.py @@ -1,4 +1,6 @@ +import logging import re +import sys from commands.echo import echo from commands.hello_world import hello_world @@ -7,8 +9,11 @@ pws_command_prefix = '/pws ' pws_command_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): - print(message.content) + logging.info('Received message: ' + str(message.content)) #Ignore messages this bot has sent to avoid answering itself. if message.author == client.user: @@ -23,20 +28,22 @@ async def handle_message(client, message): command = msg[2] args = msg[3] - print("command: " + command + ", args: " + args) + logging.info("Received Command: " + command + ", with Args: " + args) match command: case 'helloworld': 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.') else: await hello_world(message) case 'echo': 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.') + else: + await echo(message) 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.') diff --git a/pwsBot/docker-compose.yml b/pwsBot/docker-compose.yml index fe670b0..98bc506 100644 --- a/pwsBot/docker-compose.yml +++ b/pwsBot/docker-compose.yml @@ -5,4 +5,6 @@ services: image: pwsbot:latest container_name: pwsBot build: . + environment: + - PYTHONUNBUFFERED=1 diff --git a/pwsBot/resource/credentials.py b/pwsBot/resource/credentials.py index 84219d4..ae0e101 100644 --- a/pwsBot/resource/credentials.py +++ b/pwsBot/resource/credentials.py @@ -1,8 +1,12 @@ import json +import logging import sys 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): try: with open(file_path, 'r') as file: @@ -10,10 +14,10 @@ def get_token(file_path): if TOKEN in data: return data[TOKEN] else: - print("Bot token file missing " + str(TOKEN) + " field. See README.") + logging.error("Bot token file missing " + str(TOKEN) + " field. See README.") except FileNotFoundError: - print("No bot token file. See README.") + logging.error("No bot token file. See README.") 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)