Skip to content

Commit 2e5ac57

Browse files
Update readme with a lot more instructions
1 parent 5334f74 commit 2e5ac57

File tree

2 files changed

+85
-24
lines changed

2 files changed

+85
-24
lines changed

README.md

+71-18
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,79 @@ CYSCOM VIT's discord bot
44

55
# Setup
66

7-
1. Place a `.env` file in this same folder [here](./.env)
7+
### 1. Place a `.env` file
88

9-
Format of `.env` file
9+
Format of `.env` file to be placed [here](./.env)
1010

11-
```{env}
12-
BOT_TOKEN=
13-
SPAM_BAIT_CHANNEL_ID=
14-
SPAM_LOG_CHANNEL_ID=
15-
CURRENT_ACT=
16-
DEBUG=
17-
FIREBASE_DB=
18-
FIREBASE_STORAGE=
19-
```
11+
```env
12+
BOT_TOKEN=xxxx
13+
SPAM_BAIT_CHANNEL_ID=12451321
14+
SPAM_LOG_CHANNEL_ID=123213125
15+
CURRENT_ACT=6
16+
DEBUG=FALSE
17+
FIREBASE_DB=https://project-id-full-name.firebaseio.com
18+
FIREBASE_STORAGE=something.appspot.com
19+
ROOT_USER=handydandyrandy
20+
```
2021

21-
Spam bait channel is a separate channel open to all. If anyone sends a message in that channel, can assume account is compromised and a bot is trying to spam messages. Enough warnings in the channel to prevent members from actually sending messages.
22+
1. Bot token is the discord bot token from the developer token. `str`
23+
2. Spam bait channel is a separate channel open to all. If anyone sends a message in that channel, can assume account is compromised and a bot is trying to spam messages. Enough warnings in the channel to prevent members from actually sending messages. `int` This feature was added to prevent hacked accounts from spamming messages in the server.
24+
3. Logs of people banned and messages they sent are sent in this channel. `int`
25+
4. Current act `int`
26+
5. DEBUG True or False `bool`. Sets the command prefix to !cyscom-dev to differentiate from production deployement and for testing.
27+
6. Firebase DB url `str`
28+
7. Firebase storage url `str`
29+
8. Root user `int`. Discord user ID (NOT USERNAME) of the account that can trigger adding new members from a google sheet. Typically the person who deployed the bot. For multilple IDs, comma separated.
2230

23-
This feature was added to prevent hacked accounts from spamming messages in the server.
31+
There are no data types in ENV, have to convert everything from str to required type. Types are just mentioned to give approx idea of what kind of values are expected for each key.
2432

25-
2. Run using docker
26-
```{sh}
27-
docker build -t cyscomvit/discord-bot:latest .
28-
docker run --detach --name discord-bot-deploy cyscomvit/discord-bot:latest
29-
```
33+
### 2. Run using docker
34+
35+
```sh
36+
docker build -t cyscomvit/discord-bot:latest .
37+
docker run --detach --name discord-bot-deploy cyscomvit/discord-bot:latest
38+
```
39+
40+
# Development
41+
42+
The project uses [Poetry](https://python-poetry.org/) to manage dependencies.
43+
44+
<details>
45+
<summary>Why?</summary>
46+
<br>
47+
Poetry helps manage virtual environments easily.
48+
49+
It also pins versions of both dependencies and their dependencies recursively, unlike Pip. This means every package has an exact version and hash to check and download against.
50+
51+
With dependencies like `discord.py`, it became an issue since it's dependencies were not pinned and pip was installing the latest version, leading to many issues.
52+
<br>
53+
54+
</details>
55+
56+
1. Download `poetry` using Pip, or by following any of the other methods listed on their [website](https://python-poetry.org/docs/#installation)
57+
58+
```sh
59+
pip install poetry
60+
```
61+
62+
2. Create a virtual env and install all dependencies using poetry.
63+
64+
```sh
65+
poetry install
66+
```
67+
68+
This will create and activate a virtual env. It will also install all dependencies from the poetry.lock file.
69+
70+
To add new dependencies:
71+
72+
```sh
73+
poetry add package-name
74+
```
75+
76+
Update it in the `requirements.txt` (**USING POETRY COMMANDS, DON'T EDIT IT MANUALLY**) file too. Even though we use poetry, having a usable requirements.txt file might be convient for others. It is also used to build the docker image, since having poetry installed makes the image larger (smaller image better). Since the requirements.txt file is kept up-to-date, the image can use `pip` to install it, without ever downloading or installing poetry.
77+
78+
```sh
79+
poetry export -f requirements.txt -o requirements.txt
80+
```
81+
82+
**MAKE SURE YOU ADD DEPENDENCIES USING POETRY FIRST, AND DO NOT USE PIP TO INSTALL ANY PACKAGE FOR THIS PROJECT**. This ensures the package's dependencies are also pinned in the `poetry.lock` file as well.

bot.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ def check_if_required_env_variables_are_present():
2525
"FIREBASE_DB",
2626
"FIREBASE_STORAGE",
2727
"BOT_TOKEN",
28+
"ROOT_USERS",
2829
}
2930
if not all(env in environ for env in required_env_variables):
3031
raise RuntimeError(
31-
f"The following required environmental variables have not been set - {(x for x in required_env_variables if x not in environ)}"
32+
f"The following required environmental variables have not been set - {(x for x in required_env_variables if x not in environ)}. Refer to code and Readme.MD for seeing what env keys are needed"
3233
)
3334

3435

@@ -49,7 +50,9 @@ def check_if_required_env_variables_are_present():
4950
"""The current leaderboard act"""
5051

5152

52-
leaderboard_ref = db.reference("vitcc").child("owasp").child(f"leaderboard-act{current_act}")
53+
leaderboard_ref = (
54+
db.reference("vitcc").child("owasp").child(f"leaderboard-act{current_act}")
55+
)
5356

5457
# project_ref = base_ref.child("projects")
5558
# certificate_ref = base_ref.child("certificates")
@@ -58,6 +61,7 @@ def check_if_required_env_variables_are_present():
5861

5962
spam_bait_channel_id = getenv("SPAM_BAIT_CHANNEL_ID")
6063
spam_log_channel_id = getenv("SPAM_LOG_CHANNEL_ID")
64+
root_users = map(int, getenv("ROOT_USERS").split(","))
6165

6266
command_prefix = "!cyscom" if not debug else "!cyscom-dev"
6367

@@ -69,7 +73,7 @@ def check_if_required_env_variables_are_present():
6973
)
7074

7175

72-
cyscom_logo_url = "https://cyscomvit.com/assets/images/logo.png"
76+
CYSCOM_LOGO_URL = "https://cyscomvit.com/assets/images/logo.png"
7377

7478

7579
def embed_generator(
@@ -87,7 +91,7 @@ def embed_generator(
8791
embed.add_field(name="Rating", value=rating)
8892
embed.add_field(name="Contributions", value=contributions)
8993

90-
embed.set_thumbnail(url=cyscom_logo_url)
94+
embed.set_thumbnail(url=CYSCOM_LOGO_URL)
9195

9296
return embed
9397

@@ -192,7 +196,7 @@ async def add_recruits(ctx):
192196
return None
193197

194198
for name in members:
195-
await add_data(ctx, name)
199+
await add_member(ctx, name)
196200

197201
except Exception as e:
198202
print(e)
@@ -393,7 +397,7 @@ async def on_message(message):
393397
)
394398
embed.add_field(name="Name", value=f"{message.author.id}")
395399
embed.add_field(name="Message", value=f"{message.content}")
396-
embed.set_thumbnail(url=cyscom_logo_url)
400+
embed.set_thumbnail(url=CYSCOM_LOGO_URL)
397401
await ctx.send(embed=embed)
398402
return
399403

@@ -406,6 +410,10 @@ async def on_message(message):
406410
await bot.process_commands(message)
407411

408412

413+
def check_if_root_user(user_id: int | str):
414+
return int(user_id) in root_users
415+
416+
409417
def add_members_to_act(
410418
act_num: int,
411419
member_names: list[str],

0 commit comments

Comments
 (0)