I made a music bot using discord.py and Google Drive API (tested with Docker → deployed to Heroku)

The used file is placed in here (excluding authentication related files) Basically, I made it for my relatives to enjoy.

Thing you want to do

  1. Send a message on discord
  2. The bot searches for song files from google drive
  3. If found, download and play with voice

Untitled Diagram.png

Also, there are two personal requirements for music bots,

――You can request songs as you like other than yourself --I want to keep the bot running

I decided to use google drive and heroku to meet these requirements.

environment

Directory structure

.
├── Procfile
├── citron.py
├── client_secrets.json
├── commands.py
├── requirements.txt
├── runtime.txt
└── token.pickle

Description of each file

--citron.py: bot body --commands.py: List of commands that the bot responds to --client_secrets.json, token.pickle: google drive authentication related --Procfile, runtime.txt, requirements.txt: Required for Heroku deployment

Test environment construction (Docker container)

docker pull python:3.7
docker images
#Container creation
docker run -d -it --name python --mount type=bind,source=path/to/mydir,target=/home/ python:3.7

#Enter the container
docker exec -it python bash

#The following operations in the container
python3 -m pip install -U discord.py
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
apt update
apt -y upgrade
apt install ffmpeg

I installed what I needed as needed, and finally it became like this. I don't care though it may contain unnecessary things.

requirements.txt


aiohttp==3.6.2
async-timeout==3.0.1
attrs==19.3.0
cachetools==4.0.0
certifi==2019.11.28
cffi==1.13.2
chardet==3.0.4
discord.py==1.3.0
google-api-python-client==1.7.11
google-auth==1.11.0
google-auth-httplib2==0.0.3
google-auth-oauthlib==0.4.1
httplib2==0.17.0
idna==2.8
multidict==4.7.4
oauth2client==4.1.3
oauthlib==3.1.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.19
PyDrive==1.3.1
PyNaCl==1.3.0
PyYAML==5.3
requests==2.22.0
requests-oauthlib==1.3.0
rsa==4.0
six==1.14.0
uritemplate==3.0.1
urllib3==1.25.8
websockets==8.1
yarl==1.4.2
youtube-dl==2020.1.24

Discord Bot https://discordapp.com/developers/applications/ This time I will ask Yuzu to DJ. スクリーンショット 2020-01-28 2.14.27.png

Production environment (Heroku)

#Install Heroku CLI
brew install heroku/brew/heroku

heroku login
#Application creation
heroku create hoge
heroku open
#Deploy
git init
heroku git:remote -a <heroku app name>
git add .
git commit -m "first commit"
git push heroku master

Added build pack referring to here. Screenshot 2020-01-28 3.06.39.png

Where it gets stuck

After that, I wrote it steadily while looking at the official document, but since it was clogged with google drive authentication, I will post only the changes.

#Change before
creds = flow.run_local_server(port=0)

#After change
creds = flow.run_console()

I wondered if it was related to testing with Docker or something related to the network, but I could not solve it and it ended up being after the change. If anyone knows, please let me know.

Complete

It works like this. スクリーンショット 2020-01-28 22.53.10.png

スクリーンショット 2020-01-28 22.54.28.png

In the future, I would like to do something using DB as well. (Tell me the details of the song, the recommendation system, etc.)

Bonus (Heroku add-on)

--Paterrail: For log monitoring --Process Scheduler: Sleeps at midnight

reference

Recommended Posts

I made a music bot using discord.py and Google Drive API (tested with Docker → deployed to Heroku)
I made a LINE BOT with Python and Heroku
Upload to a shared drive with Google Drive API V3
I made a Chatbot using LINE Messaging API and Python
I made a Nyanko tweet form with Python, Flask and Heroku
I made a Twitter Bot with Go x Qiita API x Lambda
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
I tried hitting the Google API with Ruby and Python-Make the database a Spreadsheet and manage it with Google Drive
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I made a stamp substitute bot with line
I tried to automate internal operations with Docker, Python and Twitter API + bonus
Three things I was addicted to when using Python and MySQL with Docker
I made a LINE Bot with Serverless Framework!
I made a system that automatically decides whether to run tomorrow with Python and adds it to Google Calendar.
I tried to make a simple image recognition API with Fast API and Tensorflow
I made a Mattermost bot with Python (+ Flask)
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
I made a program to convert images into ASCII art with Python and OpenCV
[AWS] I made a reminder BOT with LINE WORKS
I made a Twitter BOT with GAE (python) (with a reference)
I made a household account book bot with LINE Bot
I made a ready-to-use syslog server with Play with Docker
How to analyze with Google Colaboratory using Kaggle API
I made a server with Python socket and ssl and tried to access it from a browser
I want to make a voice changer using Python and SPTK with reference to a famous site
How to deploy a web app made with Flask to Heroku
A simple interactive music player made with Chuck and OpenPose
I made a package to filter time series with python
Until API made with Flask + MySQL is converted to Docker
[AWS] I made a reminder BOT with LINE WORKS (implementation)
I made a discord bot
I made a LINE bot that tells me the type and strength of Pokemon in the Galar region with Heroku + Flask + PostgreSQL (Heroku Postgres)
I made a library to easily read config files with Python
A story about adding a REST API to a daemon made with Python
I made a web server with Raspberry Pi to watch anime
Get conversions and revenue with Google Analytics API and report to Slack
I tried to make a periodical process with Selenium and Python
Create a TalkBot easily using Discord.py and A3RT's Talk API (pya3rt).
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to create Bulls and Cows with a shell program
I want to create a pipfile and reflect it in docker
I tried to make a todo application using bottle with python
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Regularly upload files to Google Drive using the Google Drive API in Python
I made a poker game server chat-holdem using websocket with python
[Python] I wrote a REST API using AWS API Gateway and Lambda.
I made a chatbot with Tensor2Tensor and this time it worked
I tried using docomo speech recognition API and Google Speech API in Java
I made a bot to post on twitter by web scraping a dynamic site with AWS Lambda (continued)
I made a Line-bot using Python!
I made a wikipedia gacha bot
I made a fortune with Python.
I made a daemon with Python
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
I tried to introduce a serverless chatbot linked with Rakuten API to Teams
I made a tool to automatically browse multiple sites with Selenium (Python)
[Django] I made a field to enter the date with 4 digit numbers
I tried to draw a system configuration diagram with Diagrams on Docker
I tried to convert datetime <-> string with tzinfo using strftime () and strptime ()
A story about trying to connect to MySQL using Heroku and giving up
I tried to discriminate a 6-digit number with a number discrimination application made with python