LoginSignup
6
8

More than 3 years have passed since last update.

dockerでdiscord bot開発環境構築(python)

Posted at

はじめに

この記事がqiita初投稿となります。

最近discord botの開発をwindows端末で行っていたのですが
mac側で開発しよーと思って環境立ち上げるのにdocker使ったんでその備忘録

docker等はインストールされている前提で話をすすめまーす

概要

pythonとcode-serverのコンテナをdocker-composeを使って
開発環境をパパッと立ち上げます。

(今回code-serverいらないし、しかもdocker-network理解してなくて
 くそクオリティですがね・・・)

内容

準備

docker file

FROM python:3
USER root

RUN apt-get update
RUN apt-get -y install locales && \
    localedef -f UTF-8 -i ja_JP ja_JP.UTF-8
ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
ENV LC_ALL ja_JP.UTF-8
ENV TZ JST-9
ENV TERM xterm

RUN apt-get install -y vim less
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN python -m pip install numpy
RUN python -m pip install discord.py

docker-compose.yml

version: "3"
services:
  python:
    restart: always
    build: .
    container_name: "python3"
    working_dir: "/root/"
    tty: true
    volumes:
      - ./app:/root/opt
  code:
    image: codercom/code-server
    restart: always
    ports:
      - "8443:8443"
    volumes:
      - ./app:/home/coder/project

volumesで繋げるところは、なんかチョロっとみた記事とは違かったので
もしうまくデータ連携されない時は一度コンテナ内に入って
ディレクトリ調べることをお勧めします

実行

compose upする

$docker-compose up -d

code-serverにアクセス

http://localhost:8443 
でアクセスできるはず!
pwはdocker-composeのログにあるため

$docker-compose logs

で確認できると思います

discord botを作成

実際にvolumesで繋げたフォルダ内にdiscord botのコードを記述し、保存してください

サンプル
import discord

TOKEN = "自分のトークンID"
client = discord.Client()

@client.event
async def on_ready():
    print('ログイン')

@client.event
async def on_message(message):
    if message.author.bot:
        return
    if message.content == 'docker/python':
        await message.channel.send('docker/pythonテスト完了!')

client.run(TOKEN)

pythonコンテナで実行

本当はdocker-composeでnetworkちょいちょいすれば、code-server側から
ターミナルよんで実行できそうな気がするのですが、そこらへんはわからんです。

pythonコンテナ内に入る

$docker exec -it コンテナID bash

で、volumesで繋げたディレクトリ内で

python test.py

あとはdiscord bot入れたサーバで、docker/pythonって打って
botから返事があったら成功です!

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8