two-k-two-r-name-bot/main.js
2023-04-25 18:56:41 +09:00

42 lines
1.3 KiB
JavaScript
Executable file

const fs = require("node:fs");
const path = require("node:path");
const { Client, Collection, Events, GatewayIntentBits } = require("discord.js");
require("dotenv").config();
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs.readdirSync(commandsPath).filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
}
client.once(Events.ClientReady, (c) => {
console.log(`Ready: discord client 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);
try {
await interaction.reply({ content: "There was an error while executing this command!", ephemeral: true });
} catch (e) {
await interaction.editReply({ content: "There was an error while executing this command!", ephemeral: true });
}
}
});
client.login(process.env.DISCORD_TOKEN);