Use Cases

Discord Bot mit Claude Code

Erfahren Sie, wie Sie einen Discord Bot mit Claude Code entwickeln. Mit praktischen Tipps und Codebeispielen.

Discord Bot-Entwicklung mit Claude Code optimieren

Discord Bots werden vielseitig eingesetzt: Community-Management, Gameserver-Verwaltung, Benachrichtigungsversand und mehr. Mit der discord.js-Bibliothek lässt sich komfortabel in TypeScript entwickeln. Claude Code hilft beim effizienten Aufbau leistungsfähiger Discord Bots.

Projekt-Setup

> Erstelle ein Discord Bot-Projekt.
> discord.js v14, TypeScript, Command-Handler-Struktur.
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,
  ],
});

client.commands = new Collection();

client.once(Events.ClientReady, (c) => {
  console.log(`Eingeloggt als ${c.user.tag}`);
});

// Command-Handler
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: 'Bei der Ausführung des Befehls ist ein Fehler aufgetreten.', ephemeral: true };
    if (interaction.replied || interaction.deferred) {
      await interaction.followUp(reply);
    } else {
      await interaction.reply(reply);
    }
  }
});

registerCommands(client);
client.login(process.env.DISCORD_TOKEN);

Slash-Commands implementieren

// src/commands/poll.ts
import { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from 'discord.js';

export const data = new SlashCommandBuilder()
  .setName('poll')
  .setDescription('Erstellt eine Abstimmung')
  .addStringOption((opt) => opt.setName('question').setDescription('Frage').setRequired(true))
  .addStringOption((opt) => opt.setName('option1').setDescription('Option 1').setRequired(true))
  .addStringOption((opt) => opt.setName('option2').setDescription('Option 2').setRequired(true));

export async function execute(interaction) {
  const question = interaction.options.getString('question', true);
  const options = [
    interaction.options.getString('option1', true),
    interaction.options.getString('option2', true),
  ];
  const embed = new EmbedBuilder()
    .setTitle('📊 ' + question)
    .setColor(0x6366f1)
    .setDescription(options.map((opt) => `**${opt}**: 0 Stimmen`).join('\n'));
  const row = new ActionRowBuilder().addComponents(
    ...options.map((opt) => new ButtonBuilder().setCustomId(`poll:${opt}`).setLabel(opt).setStyle(ButtonStyle.Primary))
  );
  await interaction.reply({ embeds: [embed], components: [row] });
}

Zusammenfassung

Die Kombination von discord.js und Claude Code ermöglicht den effizienten Aufbau interaktiver Discord Bots. Siehe auch den Chatbot-Entwicklungsleitfaden und API-Entwicklung Grundlagen.

Details zu discord.js finden Sie im offiziellen discord.js-Leitfaden.

#Claude Code #Discord Bot #discord.js #Chatbot #Community