[PYTHON] Create and run Discord Bot on one Android device

For those who "want to make a Discord Bot but do not have a PC", I will introduce how to make a Discord Bot with one Android device and actually operate it. The explanation is for beginners as much as possible, but detailed explanations such as Python, discord.py, Linux commands, Vim commands, etc. are omitted.

Operating environment

1. Install Termux

Termux can be installed from the Google Play store. https://play.google.com/store/apps/details?id=com.termux

Termux is an emulator that does not require rooting and can prepare a Linux environment. Here's how to write a Discord Bot using Python and Vim.

2. Preparation with Termux

First, execute the following command in the terminal to update apt and install Python and Vim. ($ Indicates that it is a command line and no input is required)

$ apt update
$ apt upgrade
$ apt install python
$ apt install vim

Next, install a library called discord.py that allows you to easily run DiscordBot in Python.

$ pip install discord.py

Create a directory to put the Python file to be executed next and move it.

$ mkdir {Favorite directory name}
$ cd {Directory name created above}

3. Create Python file

$ vim {Favorite file name}.py

The above command will open Vim for a new file, so type ʻi` on your keyboard to enter INSERT mode. Copy the code below here. (I borrowed some of the ones on the site below)


import discord

client = discord.Client()

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

@client.event
async def on_message(message):
    #I don't want to react if the sender is a bot
    if message.author.bot:
        return

    #Find out if it starts with "Good morning"
    if message.content.startswith("Good morning"):
        #Write a message
        m = "Good morning" + message.author.name + "San!"
        #Send a message to the channel to which the message was sent
        await message.channel.send(m)

client.run("token")

4. Create a bot account

Open the site of "https://qiita.com/PinappleHunter/items/af4ccdbb04727437477f" with a browser such as Chrome, and follow the section "Getting a token for bot" to create a bot account and use it on the server you want to use. Add a bot. Make a copy of the token that says "I'll use it later" here.

5. Run the Python file

Open Termux, paste the token you copied earlier into the token part of cliant.run ("token "), save the file with ʻESC: wq`, and close Vim.

Run the Python file with the command below.

$ python {File name created earlier}.py

If you see Logged in as ..., your bot is working.

Try sending a good morning etc. on the server where you added the bot and see if you get a response. If you want to quit, quit Termux and the bot will not work either. If you want to keep your bot running all the time, you can use Heroku. By the way, Git can also be used with Termux, so it is possible to deploy to Heroku only on Android.

Reference site

How to make a simple Discord Bot with Python Run Discord bot on Android device (Termux) Create a Linux environment using Termux on Android without rooting!

Recommended Posts

Create and run Discord Bot on one Android device
USB device programming with native C on Android 5.0 and above
Install and run dropbox on Ubuntu 20.04
Run PoseNet on RaspberryPi 4 and compare edge device performance (Bonus: Jetson Xavier)
Create and run embulk config in Jupyter
Run flake8 and pytest on GitLab CI
Run py.test on Windows Anaconda and MinGW
Share VLC playlists on PC and Android
Install and run Python3.5 + NumPy + SciPy on Windows 10
[Note] Discord Bot ① Create Bot-Reply to specific content
Run OpenVino on macOS and pyenv and pipenv environment
[Ubuntu] Install Android Studio and create a shortcut
TensorFlow: Run data learned in Python on Android
Create a discord bot that notifies unilaterally with python (use only requests and json)
Until you create a machine learning environment with Python on Windows 7 and run it