[PYTHON] Let's execute the command on time with the bot of discord

The point


What is discord.py?

Discord has Bot Development Library for Many Languages and a library for python [discord.py](https:: //pypi.org/project/discord.py/).


I tried it

Use discord.py to create a bot that can be used when recruiting online game quests and matchmaking opponents, and actually use it for about a month. I've seen it, so I'll write it as an article.


Outline specifications


Set of code

Click here for the code rather than the theory. https://github.com/coleyon/discord-partypoll


What kind of function?

Roughly speaking, it is a bot that consists of the following two functions.


What kind of environment is it?

Bot as a Python program is configured to run on a Docker container.

aws_vpc_design-Page-3.png


Environment


Prepare the server and OS

For example, set up ʻUbuntu 18.04 LTS x86-64` on EC2. The free tier is sufficient. image.png


Install Docker server environment

Install docker and compose.


Install Docker server environment

I think it looks like this.

$ docker --version
Docker version 19.03.12, build 48a66213fe
$ docker-compose --version
docker-compose version 1.27.2, build 18f557f9
$ id
uid=1000(ubuntu) gid=1000(ubuntu),
#Omission...
,999(docker)
$ 

Build a Docker container

$ git clone  https://github.com/coleyon/discord-partypoll.git
Cloning into 'discord-partypoll'...
...
Resolving deltas: 100% (136/136), done.
$ cd discord-partypoll/
$ docker-compose build
$ docker images | grep partypoll
discord-partypoll_discord-bot  latest ...
$ 

Invite the bot to the Discord server


Make a Discord app

Create an application on the Developer Portal (https://discord.com/developers/applications). image.png


Set the bot's OAuth2 Token in the bot program

Get the Client Secret from the app's General Information menu and set it to DISCORD_BOT_TOKEN in docker-compose.yml.

version: "3"
services:
  discord-bot:
    restart: always
    build: .
    environment: 
      DISCORD_BOT_TOKEN: "yourtoken"

Invite the bot to the Discord server

Get the OAuth2 URL where Scopes is BOT from the ʻOAuth2menu of the app. For Permissions, giveSend Messages, Manage Messages, ʻAttach Files, Mention Everyone, ʻAdd Reactions`.


Invite the bot to the Discord server

Access the OAuth2 URL in your browser and invite the bot to the Discord server. image.png


Launch the bot program


Generate dependent libraries for Bot programs

Generate requirements.yml from Pipfile.

$ pipenv lock -r > requirements.txt

Launch the bot program

Start the Bot program (Docker container as the execution environment).

$ docker-compose up -d
$ docker-compose logs -f
Attaching to discord-partypoll_discord-bot_1
discord-bot_1  | -----Logged in info-----
discord-bot_1  | {BOT_NAME}
discord-bot_1  | {BOT_ID}
discord-bot_1  | {DISCORDPY_VERSION}
discord-bot_1  | ------------------------
discord-bot_1  | Poll Extension Enabled.
discord-bot_1  | Cron Extension Enabled.
^CERROR: Aborting.
$ 

Launching the Bot program brings the bot on Discord online and ready for use.


Try using the party recruitment command ppoll


Recruitment that can limit the total number of recruitment

This command is for recruiting parties with a maximum of 20 people in total for the entire team. / ppoll total 20 people 3 teams 20 TeamA TeamB TeamC image.png


Recruitment that can limit the number of people for each recruitment item

This command is for recruiting parties for up to 4 people in total for each team. / ppoll each 4 people 3 teams [4] TeamA [4] TeamB [4] TeamC image.png


Help command

See the / ppoll help command description or the Git repository readme.md for more information.


Try using the scheduled execution command cron


Execution time zone specification

You can set the execution timezone. The default is Japan time (Asia / Tokyo), but you can set it to something like Eastern Time (EST). image.png


Job definition and activation


Job operation example

It will be like this. See the / cron help command description or the Git repository readme.md for more information. image.png


Reference material


Basically, Official Docs is the bible, but the members of the online game introduced the following books during the making.

When I read it, it was very easy to understand and it was useful because there were many contents that I could not grasp in the official manual, so it is recommended for those who are interested in making Discord bots.

Recommended Posts

Let's execute the command on time with the bot of discord
Create a bot that only returns the result of morphological analysis with MeCab on Discord
Let's change the color scheme of iTerm2 automatically depending on the time zone
At the time of python update on ubuntu
Execute packet capture at the specified time on CentOS7
The story of stopping the production service with the hostname command
Let's clear the ambiguity of the hyphen (-) of the su command now! !!
[Linux] Execute git pull regularly with the crontab command
Make a BOT that shortens the URL of Discord
Execute the command on the web server and display the result
Let's make a Discord Bot.
Get UNIXTIME at the beginning of today with a command
Check the memory status of the server with the Linux free command
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
Check the operating status of the server with the Linux top command
Reformat the timeline of the pandas time series plot with matplotlib
Give the history command a date and time and collect the history files of all users with a script
[Completed] bot that posts the start notification of Nico Nico Live Broadcasting to Discord on discord.py
Until the start of the django tutorial with pycharm on Windows
I created a Discord bot on Docker that reports the number of corona infected people in Tokyo at a specified time.
Let's visualize the number of people infected with coronavirus with matplotlib
Let's use the distributed expression of words quickly with fastText!
[Concept] bot that posts the start notification of Nico Nico Live Broadcasting to Discord on discord.py
The story of making a question box bot with discord.py
Get the host name of the host PC with Docker on Linux
Read the coordinates of the plot on the graph with Python-matplotlib (super beginner)
How is the progress? Let's get on with the boom ?? in Python
Let's measure the test coverage of pushed python code on GitHub.
On Linux, the time stamp of a file is a little past.
Discord Bot with recording function starting with Python: (5) Directly operate the Discord API
I studied with Kaggle Start Book on the subject of kaggle [Part 1]
How to monitor the execution status of sqlldr with the pv command
Let's summarize the degree of coupling between modules with Python code
A command to easily check the speed of the network on the console
I checked the image of Science University on Twitter with Word2Vec.
Let's execute commands regularly with cron!
Completion of docker command on Linux
Let's touch on the Go language
Process the result of% time,% timeit
[DISCORD BOT] Server Manager Command Help
Using cgo with the go command
Hit the top command with htop
Let's decide the winner of bingo
Let's take a look at the forest fire on the west coast of the United States with satellite images.
Upload data to s3 of aws with a command and update it, and delete the used data (on the way)
Get the width of the div on the server side with Selenium + PhantomJS + Python
The story of making a university 100 yen breakfast LINE bot with Python
The story of having a hard time introducing OpenCV with M1 MAC
Edit the file of the SSH connection destination server on the server with VS Code
I measured the run queue wait time of a process on Linux
Play the comment of Nico Nico Douga on the terminal in conjunction with the video
The --security option cannot be used with the dnf command on CentOS Linux
Prepare the environment of Chainer on EC2 spot instance with AWS Lambda
The story of trying to push SSH_AUTH_SOCK obsolete on screen with LD_PRELOAD
Tweet the weather forecast with a bot
Keep getting RSS on the command line
Ask for Pi with the bc command
Align the size of the colorbar with matplotlib
Let's tune the model hyperparameters with scikit-learn!
Let's solve the portfolio with continuous optimization
Execute Python script with cron of TS-220