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