CharCord/main.ts
2025-06-18 19:23:32 +02:00

64 lines
1.8 KiB
TypeScript

import {
Client as _client,
ClientOptions,
Collection,
Events,
GatewayIntentBits,
MessageFlags,
} from "npm:discord.js";
const token = Deno.env.get("token");
import { Command } from "./types.ts";
import { gatherCommandsPaths } from "./commands.ts";
class Client extends _client {
commands: Collection<string, Command>;
constructor(options: ClientOptions) {
super(options);
this.commands = new Collection();
}
}
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
async function register(commandPath: string) {
const { default: command }: { default: Command } = await import(commandPath);
if ("data" in command && "execute" in command) {
client.commands.set(command.data.name, command);
console.log(`Registered ${command.data.name}.`);
} else {
console.log(
`[WARNING] The command at ${commandPath} is missing a required "data" or "execute" property.`
);
}
}
(await gatherCommandsPaths()).forEach(register)
client.once(Events.ClientReady, (readyClient) => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
}
}
});
client.login(token);