mirror of
https://github.com/runyanjake/discord.git
synced 2025-10-04 15:27:28 -07:00
Add basic commands
This commit is contained in:
parent
c3643f5d0c
commit
23d990fd99
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
env/
|
||||
pwsBot/commands/__pycache__/
|
||||
pwsBot/resource/__pycache__/
|
||||
token.json
|
16
README.md
16
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.
|
||||
|
||||
|
@ -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)
|
||||
|
3
pwsBot/commands/README.md
Normal file
3
pwsBot/commands/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Commands
|
||||
|
||||
Async command functions meant to be called asynchronously with `asyncio`.
|
5
pwsBot/commands/echo.py
Normal file
5
pwsBot/commands/echo.py
Normal file
@ -0,0 +1,5 @@
|
||||
import discord
|
||||
|
||||
async def echo(message, echo):
|
||||
await message.channel.send(echo)
|
||||
|
5
pwsBot/commands/hello_world.py
Normal file
5
pwsBot/commands/hello_world.py
Normal file
@ -0,0 +1,5 @@
|
||||
import discord
|
||||
|
||||
async def hello_world(message):
|
||||
await message.channel.send('Hello World!')
|
||||
|
@ -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)
|
||||
|
11
pwsBot/resource/intents.py
Normal file
11
pwsBot/resource/intents.py
Normal file
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user