This code is meant for maintaining official discord servers of CSE250, CSE251, CSE350, CSE460, and CSE428 at Brac University, Dhaka, Bangladesh.
If you have never run this code, please follow the steps below. For a video format tutorial, please check out the server setup process from Summer'23 (only accessible via a @bracu.ac.bd youtube account).
Please install Git Bash for using unix-like commands on Windows. ls
, cd
, ssh
, git
etc. commands can be used on macOS and Linux terminals without any hassle. However, if you're on Windows, you must emulate them to use these commands. That's exactly what Git Bash does.
We need to create an "account" (it's actually called a bot application) for the bot. It's different from regular user account creation. Please follow these steps for that:
- Go to Discord developer portal > Applications.
- Create a new bot application:
-
Video: How to Create A New Bot Application
discord_dev_new_app_small_window.mp4
-
Press the
New Application
button. -
Give it a proper name (e.g.
CSE250
) that the bot will show up as on your discord server. -
Agree to terms and click the
Create
button. -
Keep the public bot switch turned on.
-
Turn on all 3 intents.
-
- Download the
credentials.json
file (rename if necessary) following this tutorial. - Make sure to use google credentials
Oauth Client ID
>Desktop App
- Also,
OAuth Consent Screen
(orGoogle Auth Platform/Audience
) >Make External
and add your gsuite email as aTest user
.
We shall be using a DigitalOcean Droplet server for our cloud service.
- DigitalOcean -> Droplet -> 1GiB RAM ($6)
- Make sure to use a Singapore server
To access the cloud service from your own PC, we need a way for the server to recognize our PCs.
This can be done with SSH. We can send commands to the server via ssh
and check the outputs on our PC's terminal as if they were running on our own PC. But first, we need to create a "key" on our PC that can unlock the "lock" of the cloud server.
-
Generate ssh key on your PC: (need to press
Enter
3 times. -> default file name id_rsa + no passphrase)ssh-keygen -t rsa -b 2048
-
The previous command should generate a folder named
.ssh
in the user's home directory). Check it by using:ls ~/.ssh
The ~ just stands for your (user's) home directory. You can print it by using the command:
echo ~
The output should include these files:
id_rsa
,id_rsa.pub
. -
Give read and write access to your user account, but not execute: (Usergroups and others don't have any access)
chmod 600 ~/.ssh/id_rsa
The ~ just stands for your (user's) home directory. You can print it by using the command:
chmod
stands for "Change mode".600
is for setting the aforementioned read-write accesses:Access Read Write Execute In Binary In Decimal User ✅ ✅ ❌ 110
6 User Group
(every user is under
a user group)❌ ❌ ❌ 000
0 Others ❌ ❌ ❌ 000
0 All Together: 600 -
Print contents of the
id_rsa.pub
file:cat ~/.ssh/id_rsa.pub
Never share the
id_rsa
file, it is meant to be private. Rather you are to share theid_rsa.pub
file (.pub
stands for "public").
On the DigitalOcean website you can find how to upload your ssh key
(contents of your id_rsa.pub
file) as one of the trusted devices.
- Add your SSH key (contents of your
id_rsa.pub
file) to your DigitalOcean account settings.- Go to Left Pane > Settings >
Security
tab - Press the
Add SSH Key
button - Paste the contents of your
id_rsa.pub
file (usecat
to print) in the "SSH Key content" field. - Set any appropriate name for future reference in the "Name" field.
- Go to Left Pane > Settings >
- Set a reserved/static IP for your droplet:
- Go to Manage > Networking > Reserved IPs > Assign a Reserved IP tab.
- Select your droplet from the search box.
- Press
Assign Reserved IP
- Note down the IP of your droplet. for the rest of this document, we will use
IP_ADDRESS
to refer to this IP.
-
Use the
ssh
command to log in as theroot
user. (typeyes
-> press
Enter
)ssh root@IP_ADDRESS
If the previous command fails, please use the command
ssh -i ~/.ssh/id_rsa root@IP_ADDRESS
If you see a prompt to confirm connecting, enter
yes
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Typeyes
and press
Enter
-
Using
root
user is insecure. Let's create a new user:adduser username
Uppercase letters are not allowed in usernames. We recommend using your faculty initial in all lowercase.
To add a user with the name
shs
and the password123456
use the command:
adduser shs
Then type when prompted for a password:123456
-
Give this new user sudo (admin) access:
- Add
username
to thesudo
user group:usermod -aG sudo username
To give
shs
sudo privileges:
usermod -aG sudo shs
- Edit sudoers file: (opens a text editor in terminal)
visudo
- To let
username
use any command (likeroot
), add the following line underroot ALL=(ALL:ALL) ALL
:username ALL=(ALL:ALL) ALL
For the user
shs
:
shs ALL=(ALL:ALL) ALL
- For saving the file:
- Press
Ctrl
+
O
- Press
Enter
- Press
Ctrl
+
X
- Press
Enter
- Press
- Log out from
root
and log in as the newusername
: (su
stands for "substitute user"):su username
To log in as
shs
:
su shs
- Go to
username
's home: (cd
stands for "change directory")cd ~
- Create new ssh folder in the new user's home:
mkdir ~/.ssh
- Log back in as the
root
user:exit
- Copy
root
user's ssh tousername
's ssh: (contains SSH keys from DigitalOcean's trusted devices)cp ~/.ssh/authorized_keys /home/username/.ssh/
To copy to the
.ssh
folder of the usershs
:
cp ~/.ssh/authorized_keys /home/shs/.ssh/
- Change ownership from
root
tousername
(under the user groupusername
):chown -R username:username /home/username/.ssh
To change ownership of the
.ssh
folder to the usershs
:
chown -R shs:shs /home/shs/.ssh
- Log back in as username:
su username
To log in as
shs
:
su shs
- Change read access to the
.ssh
folder:chmod -R go= ~/.ssh
- For security reasons, we will be turning off remote login access to
root
. After this step, the droplet will only be accessible byusername
login. (If multiple users are to be setup, do it before this step)- Edit ssh configurations file:
sudo nano /etc/ssh/sshd_config
- This should bring up a text editor. Edit the line
PermitRootLogin yes
to:PermitRootLogin no
- Save and close the file with:
- Press
Ctrl
+
O
- Press
Enter
- Press
Ctrl
+
X
- Press
Enter
- Press
- Edit ssh configurations file:
- Restart
ssh
service:sudo systemctl restart ssh.service
- Add
From here on out, you will logging in to the server repeatedly, It's a hassle to memorize the IP_ADDRESS
of your droplet. However, we set a name for this IP_ADDRESS
and username
combination.
-
Go back to your own PC
- Log out from
username
(logs back in asroot
):exit
- Log out from
root
user (goes back to your own PC):exit
- Log out from
-
On your own PC, create an
ssh
config file:nano ~/.ssh/config
-
Add a easily memorable name for the
Host
field and set it to your drop these lines. (must end with a new line):Host memorable_host Hostname IP_ADDRESS User username
This might fail. In that case, please use:
Host memorable_host Hostname IP_ADDRESS User username IdentityFile ~/.ssh/id_rsa
To save the IP address
192.158.1.38
and usernameshs
for future, with the memorable hostbot-250
:Host bot-250 Hostname 192.158.1.38 User shs
-
Now you should be able to login in an easier manner: (as opposed to
ssh username@IP_ADDRESS
)ssh memorable_host
To log in as
shs
at the IP address192.158.1.38
with the memorable hostbot-250
:ssh bot-250
- Get link to latest miniconda installation file for linux and download file:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
- Check file name:
ls
->Miniconda3-latest-Linux-x86_64.sh
- Start installation:
bash Miniconda3-latest-Linux-x86_64.sh
- Start: press
Enter
, - More: press
Space
repeatedly, - Input: when prompted, type
yes
for accepting (default: "no")
- Start: press
- Refresh bash source:
. ~/.bashrc
- Test if Python is working:
which python
- Download the
credentials.json
file (rename if necessary) following this tutorial. - Go to your PC's downloads folder:
ls Downloads/
- copy to server securely:
scp Downloads/credentials.json bot-250:~/
- to check:
- log in as username:
ssh bot-250
- credentials file should be visible under home:
ls
- log in as username:
If you are re-running the code (e.g. in the beginning of the semester)
- Create a new folder and
cd
to it:
mkdir summer_2023 && cd summer_2023
- Clone this github repo (your directory must be empty) and make sure changes in
info.json
is not tracked:
git clone https://github.com/shs-cse/discord-bot.git . && git update-index --skip-worktree info.json
- Create a new server by copying the template https://discord.new/RVh3qBrGcsxA. The server name should follow the format
<course-code> <semester> <yyyy>
, for example,CSE251 Fall 2022
. - Add your discord bot the server and add
@bot
role to the bot. - Also, make sure the bot is added to the
EEE Course Team - BracU CSE
server. (admin not needed. used manage server permission but also probably unnecessary) - Update the
info.json
file accordingly. - Start the bot by running the script
bash -i script.sh
- To update
USIS (before)
in the enrolment sheet, upload the.xls
files downloaded from USIS in any channel as a message, clickMore > Apps > Update USIS (Before)
. You may need to repeat this multiple times since you can only upload 10 files at a time.
To check the output/errors, open the tmux session using the command
tmux attach -t discord
To exit the session without closing it, press Ctrl+b
, then d
- cd to the desired directory, e.g.,
cd ~/summer_2023
- run the script
bash -i script.sh