# discord-bot-integration > Discord.js Botを使ったロール管理・リアクションイベント処理 - Author: ao-magicianED - Repository: ao-magicianED/antigravity-skills - Version: 20260121102004 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-06 - Source: https://github.com/ao-magicianED/antigravity-skills - Web: https://mule.run/skillshub/@@ao-magicianED/antigravity-skills~discord-bot-integration:20260121102004 --- --- name: discord-bot-integration description: Discord.js Botを使ったロール管理・リアクションイベント処理 tags: [discord, bot, role-management] --- ## 結論(何が便利?) Discord Botを通じて「会員ロール自動付与」「リアクションでポイント付与」などの自動化ができます。 ## いつ使う? - 有料会員にDiscordロールを自動付与したいとき - コミュニティでのアクティビティ(いいね等)を追跡したいとき ## 手順(やること) ### 1. Discord Developer Portalでの設定 - Bot作成 → TOKEN取得 - 必要なIntentsを有効化(SERVER MEMBERS, MESSAGE CONTENT) - BotをサーバーにInvite(必要な権限付与) ### 2. 環境変数 ``` DISCORD_BOT_TOKEN=(Bot Token) DISCORD_GUILD_ID=(サーバーID) ``` ### 3. Bot初期化コード ```typescript import { Client, GatewayIntentBits, Partials } from "discord.js"; const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.MessageContent, ], partials: [Partials.Message, Partials.Channel, Partials.Reaction], }); ``` ### 4. ロール付与関数 ```typescript async function assignRole(discordUserId: string, roleName: string) { const guild = await client.guilds.fetch(GUILD_ID); let role = guild.roles.cache.find((r) => r.name === roleName); // ロールがなければ作成 if (!role) { role = await guild.roles.create({ name: roleName, color: 0xffd700, hoist: true, }); } const member = await guild.members.fetch(discordUserId); await member.roles.add(role); } ``` ### 5. リアクションイベント処理 ```typescript client.on("messageReactionAdd", async (reaction, user) => { if (user.bot) return; // 部分的なデータの取得 if (reaction.partial) { await reaction.fetch(); } // 特定の絵文字(👑など)でポイント付与 if (reaction.emoji.name === "👑") { const authorId = reaction.message.author?.id; // ポイント付与処理... } }); ``` ## 注意(落とし穴) - **Intents有効化必須**: GuildMembers, MessageContentはDiscord Developer Portalで有効化が必要 - **権限のスコープ**: Botに「Manage Roles」権限がないとロール付与できない - **ロールの順序**: Botのロールより上位のロールは操作できない ## 用語ミニ辞典 - **Intent**: Botが受け取れるイベントの種類 - **Partials**: キャッシュにないメッセージ等を取得する機能 - **Guild**: Discordサーバーのこと ## テンプレ - templates/discord-bot.ts