Skip to content

Instantly share code, notes, and snippets.

@daggy1234
Last active October 19, 2023 20:11
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daggy1234/5d92dd94350f019595a0bbd35c1ed99a to your computer and use it in GitHub Desktop.
Save daggy1234/5d92dd94350f019595a0bbd35c1ed99a to your computer and use it in GitHub Desktop.

Dpy Bot on a VPS

Hosting a bot on ubuntu is not that hard. For this tutorial we will be using ubuntu 20.0.4 and putty on a linux machine to host our bot.

Its a good idea to know git and basic bash before deploying your first app

Begin by connecting to your vps. You should follow the connection instructions on your VPS provider.

Find below the official (and good guides) for connecting to you machine.

AWS: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

GCP: https://cloud.google.com/compute/docs/instances/connecting-to-instance

Digital Ocean: https://www.digitalocean.com/docs/droplets/how-to/connect-with-ssh/

Azure: https://docs.microsoft.com/en-us/azure/virtual-machines/linux/ssh-from-windows

These guides are easy to find.

Once connected install python on your VPS. As of writing this that is python 3.8.6.

Check the default system version of python3 using the following command.

python3 -V

If this version is satisfactory ypu can proceed. Otherwise it's a good idea to find a relevant installation guide for the version of python you prefer.

As of now Ubuntu 20.0.4 comes with python3.8 as the default python3 so I will no proceed.

Then install git. Spme VPS's come with git, but git installation is extremely easy. Just run

sudo apt install git

Now simply clone the repo of your code via basic git and store it. .

For recurring updates/ aldready cloned repos use git pull.

git clone <repo URL>

Now enter the cloned repo. Use the command

cd <repo name>

For dependencies you probably need pip. To install pip for python3 run the following commands.

sudo apt update
sudo apt install python-pip
pip3 --version

Once in repo make sure you install the requirements.

If you have a requirements.txt simply use.

python3.8 -m pip install -r requirements.txt

Now you are ready to run your bot.

There are 2 ways of doing this.

A) Screen


While not a project manager, I recommend option b, but this will be a lot simpler.

Screen is a terminal multiplexer that allows you to attach virtual terminal sessions to a terminal.

To run the bot simply use

screen python3.8 <python file>

This will create a screen session. To detach from the session you must use “Ctrl-a” “d”.

To end a screen session use screen -ls to view all the available screen sessions. The id is unique and will be used for the following commands. Then you can exit in two ways.

You can reattach the screen using either of the two.

screen -r [session]

And then you have access to the session. You can quit the session and kill it using

screen -XS [session]

That is, it!

B) Systemctl


A superior way, this is systemctl a process manager that lets you run services. This allows for stuff like bot restart commands.

It is best to do this without root.

First create the service file.

nano /lib/systemd/system/serviename.service

This will take you to nano a text editor on ubuntu. Here you will edit the file by pasting in this data.

[Unit]	
Description=mybot
After=network-online.target

[Service]
Type=simple
WorkingDirectory=<directory where bot is>
ExecStart=/usr/bin/python3.8 <file containing bot (main)>
User=<your user>
Restart=always

[Install]
WantedBy=multi-user.target

With this you can use Ctrl C to exit. Save your progress.

Reload the systemctl service using this

systemctl daemon-reload

After this you may start your service.

systemctl start <service name>.service

Check the status of your service using the command

systemctl status <service name>.service

You can also view the logs via

journalctl -u <service name>

After this your bot should run well on your vps!

C) Docker


Docker is a platform which packages an application and all its dependencies together in the form of containers. This containerization aspect ensures that the application works in any environment.

Its good the read about Docker and install the daemon.

The official guide is the best for this https://docs.docker.com/engine/install/ubuntu/

Docker will use something called a Dockerfile to host a set of instructions to build the container.

For good examples on how to write dpy dockerfiles Use the official base images at https://github.com/Gorialis/discord.py-docker/tree/master/dockerfiles

This also includes a detailed guide. I recommend adding the --restart always flag when running a container so you can have restart commands.

@DJJ05
Copy link

DJJ05 commented Sep 15, 2020

thank 👌

@ShadowFox88
Copy link

give one for windows please

@daggy1234
Copy link
Author

give one for windows please

Uhh windows will work with docker. Screen and systemctl don't exist. You can just run the bot file with python or use Docker.

@averwhy
Copy link

averwhy commented Dec 2, 2020

nice

@dastanozgeldi
Copy link

This gist helped out a lot with setting my bot, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment