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