Skip to content

shs-cse/discord-bot

 
 

Repository files navigation

Discord Bot

This code is meant for maintaining official discord servers of CSE250, CSE251, CSE350, CSE460, and CSE428 at Brac University, Dhaka, Bangladesh.

Instructions for Bot Setup


Steps for the Very First Time

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).

Preliminaries

If on Windows...

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.

Discord Bot Account

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.
    • Go to Left Pane >
        Bot  
      button.

    • Keep the public bot switch turned on.

    • Turn on all 3 intents.

Google Credentials

  • 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 (or Google Auth Platform/Audience) > Make External and add your gsuite email as a Test user.

Buy Cloud Service

We shall be using a DigitalOcean Droplet server for our cloud service.

  • DigitalOcean -> Droplet -> 1GiB RAM ($6)
  • Make sure to use a Singapore server

SSH Key Generation

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
    Info

    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
    Info

    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
    Danger

    Never share the id_rsa file, it is meant to be private. Rather you are to share the id_rsa.pub file (.pub stands for "public").

Upload SSH Key to Cloud Server

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 (use cat to print) in the "SSH Key content" field.
    • Set any appropriate name for future reference in the "Name" field.
  • Set a reserved/static IP for your droplet:
  • Note down the IP of your droplet. for the rest of this document, we will use IP_ADDRESS to refer to this IP.

Remotely Login from Your PC

  • Use the ssh command to log in as the root user. (type yes -> press
     Enter 
    )

    ssh root@IP_ADDRESS
    Note

    If the previous command fails, please use the command
    ssh -i ~/.ssh/id_rsa root@IP_ADDRESS

    Note

    If you see a prompt to confirm connecting, enter yes
    Are you sure you want to continue connecting (yes/no/[fingerprint])?
    Type yes and press
      Enter  

  • Using root user is insecure. Let's create a new user:

    adduser username
    Warning

    Uppercase letters are not allowed in usernames. We recommend using your faculty initial in all lowercase.

    Example

    To add a user with the name shs and the password 123456 use the command:
    adduser shs
    Then type when prompted for a password: 123456

  • Give this new user sudo (admin) access:

    • Add username to the sudo user group:
      usermod -aG sudo username
      Example

      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 (like root), add the following line under root ALL=(ALL:ALL) ALL:
      username  ALL=(ALL:ALL) ALL
      Example

      For the user shs:
      shs ALL=(ALL:ALL) ALL

    • For saving the file:
      • Press
          Ctrl  
        +
          O  
      • Press
          Enter  
      • Press
          Ctrl  
        +
          X  
      • Press
          Enter  
    • Log out from root and log in as the new username : (su stands for "substitute user"):
      su username
      Example

      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 to username's ssh: (contains SSH keys from DigitalOcean's trusted devices)
      cp ~/.ssh/authorized_keys /home/username/.ssh/
      Example

      To copy to the .ssh folder of the user shs:
      cp ~/.ssh/authorized_keys /home/shs/.ssh/

    • Change ownership from root to username (under the user group username):
      chown -R username:username /home/username/.ssh
      Example

      To change ownership of the .ssh folder to the user shs:
      chown -R shs:shs /home/shs/.ssh

    • Log back in as username:
      su username
      Example

      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 by username 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  
    • Restart ssh service:
      sudo systemctl restart ssh.service

Set a Name for Droplet's IP Address:

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 as root):
      exit
    • Log out from root user (goes back to your own PC):
      exit
  • 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
    
    
    Warning

    This might fail. In that case, please use:

    Host memorable_host
      Hostname IP_ADDRESS
      User username
      IdentityFile ~/.ssh/id_rsa
    
    Example

    To save the IP address 192.158.1.38 and username shs for future, with the memorable host bot-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
    Example

    To log in as shs at the IP address 192.158.1.38 with the memorable host bot-250:

    ssh bot-250

Install Miniconda on Droplet

  • 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")
  • Refresh bash source:
    . ~/.bashrc
  • Test if Python is working:
    which python

Google Credentials

  • 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

Steps for the Re-running the Bot

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, click More > 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

If you want to update code or start running again

  • cd to the desired directory, e.g.,
cd ~/summer_2023
  • run the script
bash -i script.sh

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 98.5%
  • Shell 1.5%