スムージーの備忘録

Discord Botのテンプレート

動作環境

  • Python 3.11.3
  • discord.py 2.3.2

Botの初期設定

Discord Developer Site Discord DeveloperからのBotの作成はいずれ解説します。

Botの作成方法を理解している人はOAuth2 URL Generatorにて画像のように設定してください。
また、BOT PERMISSIONSについてはテストBotのため管理者をつけていますが、使用用途に合わせた必要な権限のみにしてください。

ファイル構成

Bot
├── DiscordKeys
│   ├── token.txt
│   └── guild_id.txt
└── bot.py

DiscordKeysフォルダーについて

DiscordのBotにはトークンを発行する必要があります。 トークンはBot作成時に __一度__ だけ表示されます。 トークンが他人に知られると、そのBotを悪用される可能性があります。 事故防止のためにトークンは別ファイルに保存しています。 Gitで管理する場合フォルダーを.gitignoreに追加してください。

guild_id.txtについて

guild_idはサーバーのIDです。 スラッシュコマンドはギルドコマンドとグローバルコマンドの2種類があり、ギルドコマンドは サーバー内でのみ使用できるかわりに反映が早いらしい 要出典

取得方法は画像の場所にある、開発者モードをオンの状態にしてからサーバーの右クリックからサーバーIDをコピーでできます。 設定→詳細設定→開発者モード

DiscordBotのテンプレート

bot.py
コピー
import os

import discord
from discord import app_commands

#Discord Bot Initialize
client = discord.Client(intents=discord.Intents.default())
tree = discord.app_commands.CommandTree(client)

DiscordIDs = 'DiscordKeys/'

with open(os.path.join(DiscordIDs, 'token.txt'), 'r') as t, \
     open(os.path.join(DiscordIDs, "guild_id.txt"), 'r') as g:
    
    TOKEN = t.read()
    GUILD_ID = g.read()

guild=discord.Object(GUILD_ID)

@tree.command(
    guild=guild,
    name='test',
    description='test command'
)
async def test(ctx:discord.Integration):
    await ctx.response.send_message("test!")

@client.event
async def on_ready():
    print('ready...')
    await tree.sync(guild=guild)
    print('synced...')

client.run(TOKEN)

コード解説

コピー
client = discord.Client(intents=discord.Intents.default())
tree = discord.app_commands.CommandTree(client)

よく言われるおまじないです。
client部分はインテントといってスラッシュコマンドが実装される前は!testだったり/testだったりと、
コマンドを認識させる文字列をintentsを使って設定してました。(確か)

treeはスラッシュコマンドを使うために必要なものです。

コピー
@client.event
async def on_ready():
    print('ready...')
    await tree.sync(guild=guild)
    print('synced...')

client.run(TOKEN)

await tree.sync(guild=guild)にてスラッシュコマンドを同期させています。

コピー
@tree.command(
    guild=guild,
    name='test',
    description='test command'
)
async def test(ctx:discord.Integration):
    await ctx.response.send_message("test!")

Discordのコマンドのテンプレートです。
コマンドを追加する場合これを元に作成します。
guild=guildはスラッシュコマンドを使用するサーバーを指定しています。
name='test'はコマンド名です。小文字のみ使用可能です。
description='test command'はコマンドの説明です。

実行結果

コマンドの予測機能 Botの実行結果

今後もDiscord Botで便利な機能を紹介できればいいなと思っています。

© 2024 Smoothie1023.