From c3643f5d0c30a7bc5f1cb7660cef3263f73d7536 Mon Sep 17 00:00:00 2001 From: Jake R Date: Tue, 23 Apr 2024 00:09:44 -0700 Subject: [PATCH] Add basic PWSBot --- .gitignore | 3 +++ README.md | 28 ++++++++++++++++++++++++++++ pwsBot/Dockerfile | 11 +++++++++++ pwsBot/bot.py | 25 +++++++++++++++++++++++++ pwsBot/docker-compose.yml | 10 ++++++++++ pwsBot/resource/__init__.py | 0 pwsBot/resource/credentials.py | 19 +++++++++++++++++++ requirements.txt | 1 + 8 files changed, 97 insertions(+) create mode 100644 .gitignore create mode 100644 pwsBot/Dockerfile create mode 100644 pwsBot/bot.py create mode 100644 pwsBot/docker-compose.yml create mode 100644 pwsBot/resource/__init__.py create mode 100644 pwsBot/resource/credentials.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7b34d42 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +env/ +pwsBot/resource/__pycache__/ +token.json \ No newline at end of file diff --git a/README.md b/README.md index 90501a6..28d2783 100644 --- a/README.md +++ b/README.md @@ -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. + diff --git a/pwsBot/Dockerfile b/pwsBot/Dockerfile new file mode 100644 index 0000000..83abd87 --- /dev/null +++ b/pwsBot/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.11 + +WORKDIR /discord + +COPY ./ /discord + +EXPOSE 3000 + +WORKDIR /discord + +CMD [ "python", "bot.py" ] diff --git a/pwsBot/bot.py b/pwsBot/bot.py new file mode 100644 index 0000000..b17c141 --- /dev/null +++ b/pwsBot/bot.py @@ -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)) diff --git a/pwsBot/docker-compose.yml b/pwsBot/docker-compose.yml new file mode 100644 index 0000000..d283948 --- /dev/null +++ b/pwsBot/docker-compose.yml @@ -0,0 +1,10 @@ +version: "3" + +services: + website: + image: pwsBot:latest + container_name: pwsBot + build: . + ports: + - 8000:8000 + diff --git a/pwsBot/resource/__init__.py b/pwsBot/resource/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pwsBot/resource/credentials.py b/pwsBot/resource/credentials.py new file mode 100644 index 0000000..01cfa38 --- /dev/null +++ b/pwsBot/resource/credentials.py @@ -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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..446ed9b --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +discord.py==2.3.2 \ No newline at end of file