Discord Bot: Claude Code 활용 가이드
discord bot: Claude Code 활용. 실용적인 팁과 코드 예시를 포함합니다.
Discord Bot개발をClaude Code로 효율화하기
Discord Botはコミュニティ運営、ゲーム서버관리、알림配信など幅広い用途で활용されています。discord.js라이브러리を使えばTypeScriptで快適に개발할 수 있습니다が、インタラクション処理やパーミッション관리には多くの구현が필요합니다。Claude Codeを활용して、高機能なDiscord Botを효율적으로구축합시다。
프로젝트のセットアップ
> Discord Botの프로젝트를 생성해줘。
> discord.js v14、TypeScript、コマンド핸들러ー구성で。
npm init -y
npm install discord.js
npm install -D typescript @types/node tsx
// src/index.ts
import { Client, GatewayIntentBits, Collection, Events } from 'discord.js';
import { registerCommands } from './commands';
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
client.commands = new Collection();
client.once(Events.ClientReady, (c) => {
console.log(`Logged in as ${c.user.tag}`);
});
// コマンド핸들러ー
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
const reply = {
content: 'コマンドの実行中にエラーが発生しました。',
ephemeral: true,
};
if (interaction.replied || interaction.deferred) {
await interaction.followUp(reply);
} else {
await interaction.reply(reply);
}
}
});
registerCommands(client);
client.login(process.env.DISCORD_TOKEN);
スラッシュコマンドの구현
> /poll コマンドで投票機能를 구현해줘。
> 버튼で投票できるようにして。
// src/commands/poll.ts
import {
SlashCommandBuilder,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
EmbedBuilder,
ChatInputCommandInteraction,
ButtonInteraction,
} from 'discord.js';
const polls = new Map<string, { question: string; votes: Map<string, Set<string>> }>();
export const data = new SlashCommandBuilder()
.setName('poll')
.setDescription('投票を作成します')
.addStringOption((opt) =>
opt.setName('question').setDescription('質問').setRequired(true)
)
.addStringOption((opt) =>
opt.setName('option1').setDescription('選択肢1').setRequired(true)
)
.addStringOption((opt) =>
opt.setName('option2').setDescription('選択肢2').setRequired(true)
)
.addStringOption((opt) =>
opt.setName('option3').setDescription('選択肢3').setRequired(false)
);
export async function execute(interaction: ChatInputCommandInteraction) {
const question = interaction.options.getString('question', true);
const options = [
interaction.options.getString('option1', true),
interaction.options.getString('option2', true),
interaction.options.getString('option3'),
].filter(Boolean) as string[];
const pollId = `poll-${Date.now()}`;
const votes = new Map<string, Set<string>>();
options.forEach((opt) => votes.set(opt, new Set()));
polls.set(pollId, { question, votes });
const embed = new EmbedBuilder()
.setTitle('📊 ' + question)
.setColor(0x6366f1)
.setDescription(
options.map((opt) => `**${opt}**: 0票`).join('\n')
)
.setFooter({ text: `Poll ID: ${pollId}` });
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
...options.map((opt, i) =>
new ButtonBuilder()
.setCustomId(`${pollId}:${opt}`)
.setLabel(opt)
.setStyle(ButtonStyle.Primary)
)
);
await interaction.reply({ embeds: [embed], components: [row] });
}
// 버튼핸들러ー
export async function handlePollButton(interaction: ButtonInteraction) {
const [pollId, option] = interaction.customId.split(':');
const poll = polls.get(pollId);
if (!poll) return;
const userId = interaction.user.id;
// 既存の投票を삭제
for (const voters of poll.votes.values()) {
voters.delete(userId);
}
// 新しい投票を추가
poll.votes.get(option)?.add(userId);
const description = Array.from(poll.votes.entries())
.map(([opt, voters]) => {
const count = voters.size;
const bar = '█'.repeat(count) || '░';
return `**${opt}**: ${count}票 ${bar}`;
})
.join('\n');
const embed = new EmbedBuilder()
.setTitle('📊 ' + poll.question)
.setColor(0x6366f1)
.setDescription(description)
.setFooter({ text: `Poll ID: ${pollId}` });
await interaction.update({ embeds: [embed] });
}
Embedメッセージ
> 서버情報を표시するコマンドを作って。
> リッチなEmbed形式で표시して。
// src/commands/serverinfo.ts
import {
SlashCommandBuilder,
EmbedBuilder,
ChatInputCommandInteraction,
} from 'discord.js';
export const data = new SlashCommandBuilder()
.setName('serverinfo')
.setDescription('サーバー情報を表示します');
export async function execute(interaction: ChatInputCommandInteraction) {
const guild = interaction.guild;
if (!guild) return;
const embed = new EmbedBuilder()
.setTitle(guild.name)
.setThumbnail(guild.iconURL() || '')
.setColor(0x6366f1)
.addFields(
{ name: 'メンバー数', value: `${guild.memberCount}人`, inline: true },
{ name: 'チャンネル数', value: `${guild.channels.cache.size}`, inline: true },
{ name: 'ロール数', value: `${guild.roles.cache.size}`, inline: true },
{ name: '作成日', value: guild.createdAt.toLocaleDateString('ja-JP'), inline: true },
{ name: 'ブースト', value: `Lv.${guild.premiumTier} (${guild.premiumSubscriptionCount}回)`, inline: true },
)
.setTimestamp();
await interaction.reply({ embeds: [embed] });
}
コマンドの등록
// src/commands/index.ts
import { REST, Routes, Client } from 'discord.js';
import * as poll from './poll';
import * as serverinfo from './serverinfo';
const commands = [poll, serverinfo];
export function registerCommands(client: Client) {
for (const command of commands) {
client.commands.set(command.data.name, command);
}
}
// スラッシュコマンドをDiscordに등록
export async function deployCommands() {
const rest = new REST().setToken(process.env.DISCORD_TOKEN!);
const body = commands.map((c) => c.data.toJSON());
await rest.put(
Routes.applicationCommands(process.env.DISCORD_CLIENT_ID!),
{ body }
);
console.log('Commands deployed');
}
정리
discord.jsとClaude Codeを組み合わせれば、インタラクティブなDiscord Botを효율적으로구축할 수 있습니다。チャットボット개발가이드やAPI개발の基本도 참고하세요.
discord.js의 상세 정보는discord.js公式가이드를 참고하세요.
#Claude Code
#Discord Bot
#discord.js
#chatbot
#コミュニティ
Related Posts
Use Cases
Use Cases
Claude Code로 리팩토링을 자동화하는 방법
Claude Code를 활용해 코드 리팩토링을 효율적으로 자동화하는 방법을 알아봅니다. 실전 프롬프트와 구체적인 리팩토링 패턴을 소개합니다.
Use Cases
Use Cases
Claude Code로 사이드 프로젝트 개발 속도를 극대화하는 방법 [예제 포함]
Claude Code를 활용해 개인 프로젝트 개발 속도를 획기적으로 높이는 방법을 알아봅니다. 실전 예제와 아이디어부터 배포까지의 워크플로를 포함합니다.
Use Cases
Use Cases
Complete CORS Configuration Guide: Claude Code 활용 가이드
complete cors configuration guide: Claude Code 활용. 실용적인 팁과 코드 예시를 포함합니다.