a402c7b0bb
- Add package.json with dependencies and scripts for building, testing, and running the bot. - Implement bot startup logic in src/bot.ts, handling interactions and commands. - Create command structure in src/commands/index.ts, including a ping command in src/commands/ping.ts. - Add configuration loading from environment variables in src/config.ts. - Implement command registration in Discord API in src/deploy-commands.ts. - Bootstrap the bot in src/index.ts. - Configure TypeScript settings in tsconfig.json.
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import {
|
|
Client,
|
|
Events,
|
|
GatewayIntentBits,
|
|
Interaction,
|
|
Partials,
|
|
} from "discord.js";
|
|
import { commandMap } from "./commands";
|
|
import { BotConfig } from "./config";
|
|
|
|
export async function startBot(config: BotConfig): Promise<Client> {
|
|
const client = new Client({
|
|
intents: [GatewayIntentBits.Guilds],
|
|
partials: [Partials.Channel],
|
|
});
|
|
|
|
client.once(Events.ClientReady, (readyClient) => {
|
|
console.log(`Bot ready as ${readyClient.user.tag}`);
|
|
});
|
|
|
|
client.on(Events.InteractionCreate, async (interaction: Interaction) => {
|
|
if (!interaction.isChatInputCommand()) {
|
|
return;
|
|
}
|
|
|
|
const command = commandMap.get(interaction.commandName);
|
|
if (!command) {
|
|
await interaction.reply({
|
|
content: `Unknown command: ${interaction.commandName}`,
|
|
ephemeral: true,
|
|
});
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await command.execute(interaction);
|
|
} catch (error: unknown) {
|
|
console.error(`Command failed: ${interaction.commandName}`, error);
|
|
|
|
if (interaction.replied || interaction.deferred) {
|
|
await interaction.followUp({
|
|
content: "Command failed. Try again.",
|
|
ephemeral: true,
|
|
});
|
|
} else {
|
|
await interaction.reply({
|
|
content: "Command failed. Try again.",
|
|
ephemeral: true,
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
await client.login(config.discordToken);
|
|
return client;
|
|
}
|