A discord bot for teal.fm
discord tealfm music
at main 1.8 kB view raw
1import { env } from "@tealfmbot/common/constants"; 2import { logger } from "@tealfmbot/common/logger"; 3import { Client, Collection, Events, GatewayIntentBits, MessageFlags } from "discord.js"; 4import fs from "node:fs"; 5import path from "node:path"; 6 7const client = new Client({ intents: [GatewayIntentBits.Guilds] }); 8 9client.once(Events.ClientReady, (readyClient) => { 10 console.log(`teal.fm bot ready and logged in as ${readyClient.user.tag}`); 11}); 12 13client.login(env.DISCORD_BOT_TOKEN); 14 15client.on(Events.InteractionCreate, async (interaction) => { 16 if (!interaction.isChatInputCommand()) return; 17 const command = interaction.client.commands.get(interaction.commandName); 18 if (!command) { 19 console.error(`No command matching ${interaction.commandName} was found.`); 20 return; 21 } 22 try { 23 await command.execute(interaction); 24 } catch (error) { 25 console.error(error); 26 if (interaction.replied || interaction.deferred) { 27 await interaction.followUp({ 28 content: "There was an error while executing this command!", 29 flags: MessageFlags.Ephemeral, 30 }); 31 } else { 32 await interaction.reply({ 33 content: "There was an error while executing this command!", 34 flags: MessageFlags.Ephemeral, 35 }); 36 } 37 } 38}); 39 40client.commands = new Collection(); 41 42const commandPaths = fs.globSync("dist/commands/*.js"); 43for await (const file of commandPaths) { 44 const absoluteCommandPath = path.join(import.meta.dirname, file); 45 const command = await import(absoluteCommandPath); 46 if ("data" in command.default && "execute" in command.default) { 47 client.commands.set(command.default.data.name, command.default); 48 } else { 49 logger.warn( 50 `[Warning] The command at ${absoluteCommandPath} is missing a required "data" or "execute" property.`, 51 ); 52 } 53}