From ab867b3b0c36463d915a9014c469911f13eef7e2 Mon Sep 17 00:00:00 2001 From: whitney Date: Tue, 28 Jan 2025 17:10:27 -0800 Subject: [PATCH] Refactor commands a bit, add links --- pwsBot/commands/echo.py | 2 -- pwsBot/commands/hello_world.py | 2 -- pwsBot/commands/links.py | 8 +++++ pwsBot/commands/message_handler.py | 49 +++++++++++++++++++++--------- 4 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 pwsBot/commands/links.py diff --git a/pwsBot/commands/echo.py b/pwsBot/commands/echo.py index 898a4e7..2d1a47f 100644 --- a/pwsBot/commands/echo.py +++ b/pwsBot/commands/echo.py @@ -1,5 +1,3 @@ -import discord - async def echo(message, echo): await message.channel.send(echo) diff --git a/pwsBot/commands/hello_world.py b/pwsBot/commands/hello_world.py index 9adfcfc..c24b54a 100644 --- a/pwsBot/commands/hello_world.py +++ b/pwsBot/commands/hello_world.py @@ -1,5 +1,3 @@ -import discord - async def hello_world(message): await message.channel.send('Hello World!') diff --git a/pwsBot/commands/links.py b/pwsBot/commands/links.py new file mode 100644 index 0000000..4de7f63 --- /dev/null +++ b/pwsBot/commands/links.py @@ -0,0 +1,8 @@ +linksMessage = """# **Links** +- PWS Homepage [Link](https://www.whitney.rip) +- pZ_aeriaL Youtube [Link](https://www.youtube.com/channel/UCZctf3QSXXk9a5KzrAJ2bmwt) +- My Source Code [Link](https://github.com/runyanjake/discord/tree/main/pwsBot) +""" + +async def links(message): + await message.channel.send(linksMessage) diff --git a/pwsBot/commands/message_handler.py b/pwsBot/commands/message_handler.py index 831df9c..e5970e3 100644 --- a/pwsBot/commands/message_handler.py +++ b/pwsBot/commands/message_handler.py @@ -4,6 +4,7 @@ import sys from commands.echo import echo from commands.hello_world import hello_world +from commands.links import links pws_command_prefix = '/pws ' pws_command_regex = r'/pws (\S+)' @@ -32,24 +33,42 @@ async def handle_message(client, message): match command: case 'helloworld': - if args: - logging.warn('Command ' + command + ' got unexpected arguments: ' + message.content + '.') - await message.author.send('Unexpected arguments for command: `' + message.content + '`. Check list of commands (`/pws help`) for valid usage.') - else: - await hello_world(message) + await handle_command(command, hello_world, args, message, False) case 'echo': - if not args: - 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 (`/pws help`) for valid usage.') - else: - await echo(message, args) + await handle_command(command, echo, args, message, True) + case 'links': + await handle_command(command, links, args, message, False) case 'help' | 'commands': - if args: - logging.warn('Command ' + command + ' got unexpected arguments: ' + message.content + '.') - await message.author.send('Unexpected arguments for command: `' + message.content + '`. Check list of commands (`/pws help`) for valid usage.') - else: - await message.author.send('**Commands**\n- /pws helloworld\n- /pws echo\n- /pws help\n- /pws commands') + await handle_command(command, commands, args, message, False) case _: logging.warn('Invalid command ' + command + ' received.') await message.author.send('Invalid command `' + command + '`. Check list of commands for valid usage.') +commandsMessage = """# **Commands** +- /pws helloworld +- /pws echo +- /pws links +- /pws help +- /pws commands +""" + +async def commands(message): + await message.author.send(commandsMessage) + +async def handle_command(command, func, args, message, should_have_args=True): + await handle_command_with_args(command, func, args, message) if should_have_args else await handle_command_without_args(command, func, args, message) + +async def handle_command_without_args(command, func, args, message): + if args: + logging.warn('Command ' + command + ' got unexpected arguments: ' + message.content + '.') + await message.author.send('Unexpected arguments for command: `' + message.content + '`. Check list of commands (`/pws help`) for valid usage.') + else: + await func(message) + +async def handle_command_with_args(command, func, args, message): + if not args: + 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 (`/pws help`) for valid usage.') + else: + await func(message, args) +