Add basic PWSBot

This commit is contained in:
Jake R 2024-04-23 00:09:44 -07:00
parent 269997f97e
commit c3643f5d0c
8 changed files with 97 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
env/
pwsBot/resource/__pycache__/
token.json

View File

@ -1,2 +1,30 @@
# discord
Some assorted discord bots.
### Env Setup
Env creation: `python3.11 -m venv env`, `source env/bin/activate`
Requirements: `pip install -r requirements.txt`
### Discord Setup
`https://discord.com/developers/applications`
`New Application > Create`, fill out the information.
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":"AAAAAAAAAAAAAA..."
}
```
If you need to configure permissions for the bot itself, that happens in the `Bot` tab. Can be done later.
For now, go to `OAuth2 > Oauth2 URL Generator` and select Bot with the desired permissions. Can be done later.
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.

11
pwsBot/Dockerfile Normal file
View File

@ -0,0 +1,11 @@
FROM python:3.11
WORKDIR /discord
COPY ./ /discord
EXPOSE 3000
WORKDIR /discord
CMD [ "python", "bot.py" ]

25
pwsBot/bot.py Normal file
View File

@ -0,0 +1,25 @@
import discord
import sys
from resource.credentials import get_token
PWS_command_prefix = '/pws '
client = discord.Client()
client_token = get_token()
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
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!')
print("Authenticating with token " + str(client_token))
client.run(os.getenv(client_token))

10
pwsBot/docker-compose.yml Normal file
View File

@ -0,0 +1,10 @@
version: "3"
services:
website:
image: pwsBot:latest
container_name: pwsBot
build: .
ports:
- 8000:8000

View File

View File

@ -0,0 +1,19 @@
import json
import sys
TOKEN = 'token'
def get_token():
try:
with open(file_path, 'r') as file:
data = json.load(file)
if TOKEN in data:
return data[TOKEN]
else:
print("Bot token file missing " + str(TOKEN) + " field. See README.")
except FileNotFoundError:
print("No bot token file. See README.")
except json.JSONDecodeError:
print("Invalid JSON format for bot token file. See README.")
sys.exit(1)

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
discord.py==2.3.2