diff --git a/.gitignore b/.gitignore index 7b34d42..b94befc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ env/ +pwsBot/commands/__pycache__/ pwsBot/resource/__pycache__/ -token.json \ No newline at end of file +token.json diff --git a/README.md b/README.md index 28d2783..468806d 100644 --- a/README.md +++ b/README.md @@ -15,16 +15,26 @@ Requirements: `pip install -r requirements.txt` Copy bot's token or regenerate it from the main page. Place this in a file called `token.json` in the root directory here or modify `resource/credentials.py` to find it: -`resource/credentials.py`: +`token.json`: ``` { "token":"AAAAAAAAAAAAAA..." } ``` -If you need to configure permissions for the bot itself, that happens in the `Bot` tab. Can be done later. +Go to `Bot` tab to configure permissions. In my experience, the `Requires OAuth2 Code Grant` toggle broke my ability to add the bot to servers, so avoiding that's probably a good idea for local dev. +In general, if there is a mismatch between the scopes and permissions assigned to the bot, it won't be able to be added to the server. +Additionally you have to grant the bot the right intents. See how this is done in `pwsBot/resources/intents.py`. Also see `https://discordpy.readthedocs.io/en/stable/api.html`. -For now, go to `OAuth2 > Oauth2 URL Generator` and select Bot with the desired permissions. Can be done later. +Go to `OAuth2 > Oauth2 URL Generator` and select Bot with the desired permissions. Can modify these, generate a new invite link, and re-add to the server again later. + +I am currently using the following Scopes/Permissions: + +Scopes +- bot + +Permissions +- administrator Invite bot to server using the url generated above. I've set the redirect URL to `https://github.com/runyanjake/discord` for after users authenticate with OAuth. diff --git a/pwsBot/bot.py b/pwsBot/bot.py index b17c141..5d455d4 100644 --- a/pwsBot/bot.py +++ b/pwsBot/bot.py @@ -1,12 +1,21 @@ import discord +import os +import re import sys from resource.credentials import get_token +from resource.intents import get_intents -PWS_command_prefix = '/pws ' +from commands.echo import echo +from commands.hello_world import hello_world -client = discord.Client() -client_token = get_token() +pws_command_prefix = '/pws ' +pws_command_regex = r'/pws (\S+)' +pws_command_with_args_regex = r'/pws \S+(.*)' + +intents = get_intents() +client = discord.Client(intents=intents) +client_token = get_token('../token.json') @client.event async def on_ready(): @@ -14,12 +23,38 @@ async def on_ready(): @client.event async def on_message(message): + print(message.content) if message.author == client.user: return - if message.content.startswith(PWS_command_prefix): - print('Received Command ' + str(message.content)) - await message.channel.send('Message Received!') + if message.content.startswith(pws_command_prefix): + msg = [3] + msg.append(pws_command_prefix) + msg.append(re.match(pws_command_regex, message.content)[1]) + msg.append(re.match(pws_command_with_args_regex, message.content)[1]) -print("Authenticating with token " + str(client_token)) -client.run(os.getenv(client_token)) + command = msg[2] + args = msg[3] + print("command: " + command + ", args: " + args) + + match command: + case 'helloworld': + if args: + print('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 + '.') + await message.author.send('Did not specify argument(s) for command: `' + message.content + '`. Check list of commands for valid usage.') + else: + await echo(message, args) + case _: + print('Invalid command ' + command + ' received.') + await message.author.send('Invalid command `' + command + '`. Check list of commands for valid usage.') + else: + print('Error parsing command') + await message.author.send('Error parsing command:\n`' + message.content + '`\nMessage format is "`' + pws_command_prefix + '` command (args...)"') + +client.run(client_token) diff --git a/pwsBot/commands/README.md b/pwsBot/commands/README.md new file mode 100644 index 0000000..43cedbd --- /dev/null +++ b/pwsBot/commands/README.md @@ -0,0 +1,3 @@ +# Commands + +Async command functions meant to be called asynchronously with `asyncio`. diff --git a/pwsBot/commands/echo.py b/pwsBot/commands/echo.py new file mode 100644 index 0000000..898a4e7 --- /dev/null +++ b/pwsBot/commands/echo.py @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000..9adfcfc --- /dev/null +++ b/pwsBot/commands/hello_world.py @@ -0,0 +1,5 @@ +import discord + +async def hello_world(message): + await message.channel.send('Hello World!') + diff --git a/pwsBot/resource/credentials.py b/pwsBot/resource/credentials.py index 01cfa38..84219d4 100644 --- a/pwsBot/resource/credentials.py +++ b/pwsBot/resource/credentials.py @@ -3,7 +3,7 @@ import sys TOKEN = 'token' -def get_token(): +def get_token(file_path): try: with open(file_path, 'r') as file: data = json.load(file) diff --git a/pwsBot/resource/intents.py b/pwsBot/resource/intents.py new file mode 100644 index 0000000..959aa98 --- /dev/null +++ b/pwsBot/resource/intents.py @@ -0,0 +1,11 @@ +import discord + +def get_intents(): + intents= discord.Intents.default() + + # Specifying a list of enabled (True) intents. + intents.messages = True + intents.message_content = True + + return intents +