0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DockerFileでデプロイするDiscord.jsの構築

Last updated at Posted at 2024-12-23

Discord.py は Python で書かれた Discord Bot を構築するためのライブラリです。このガイドでは、Discord.py のプロジェクトを Docker を使ってコンテナで実行するための Dockerfile の書き方を紹介します。


準備

  1. Python 環境の構築
    Discord.py をインストールするための Python 環境を用意します。

    requirements.txt の例

    discord.py
    python-dotenv
    
  2. Botコードの書き方
    下記は簡易な Discord Bot のスクリプト例です。

    bot.py の例

    import discord
    from discord.ext import commands
    import os
    
    intents = discord.Intents.default()
    bot = commands.Bot(command_prefix="!", intents=intents)
    
    @bot.event
    async def on_ready():
        print(f'We have logged in as {bot.user}')
    
    @bot.command()
    async def ping(ctx):
        await ctx.send('Pong!')
    
    bot.run(os.getenv('DISCORD_TOKEN'))
    
  3. .env ファイル
    .env ファイルに、ボットのトークンを保存します。

    DISCORD_TOKEN=your_discord_bot_token
    

Dockerfile の書き方

以下は Discord.py プロジェクト用の Dockerfile 例です。

# Python ベースのイメージを指定
FROM python:3.11-slim

# アプリケーションディレクトリを設定
WORKDIR /app

# リソースをコピー
COPY requirements.txt requirements.txt
COPY bot.py bot.py
COPY .env .env

# ライブラリのインストール
RUN pip install --no-cache-dir -r requirements.txt

# ボットコードを実行
CMD ["python", "bot.py"]

Docker イメージの作成と実行

  1. Docker イメージの作成
    以下のコマンドを実行して Docker イメージを作成します。

    docker build -t discord-bot .
    
  2. Docker コンテナでボットを実行
    下記のコマンドでコンテナを起動します。

    docker run --env-file .env discord-bot
    

おわりに

このガイドでは、Discord.py プロジェクトを Docker コンテナで構築するための基本的な手順を紹介しました。Docker を使うことで、デプロイが安定して実行できる環境を構築できます。ぜひお試しください。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?