penguin-band/index.js
2024-03-27 01:00:11 -05:00

106 lines
2.9 KiB
JavaScript

import fs from "fs";
import path from "path";
import { Client, Collection, GatewayIntentBits, Partials } from "discord.js";
const {
TOKEN,
USER_ID_DEBUG,
TEXT_ID_COMMAND,
} = require("./config.json");
export const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
],
partials: [Partials.Channel, Partials.Message],
});
client.fileCommands = new Collection();
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs.readdirSync(commandsPath);
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
if ("data" in command && "execute" in command) {
client.fileCommands.set(command.data.name, command);
if (command.synonyms) {
for (const synonym of command.synonyms) {
client.fileCommands.set(synonym, command);
}
}
} else {
console.error(`${__filename}: Invalid command file: ${file}`);
}
}
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
console.log(
`Deleting old messages from this bot in command channel: ${TEXT_ID_COMMAND}...`
);
let channel = client.channels.cache.get(TEXT_ID_COMMAND);
if (!channel) return console.error("(ready) Can't find command channel");
channel.messages
.fetch({ limit: 100 })
.then((messages) => {
messages.forEach((message) => {
if (message.author.id === client.user.id) {
message.delete();
}
});
})
.catch(console.error);
});
// send message on error
client.on("error", (error) => {
console.error(error);
let channel = client.channels.cache.get(TEXT_ID_COMMAND);
if (!channel) return console.error("Can't find command channel");
client.channels.cache
.get(TEXT_ID_COMMAND)
.send(`Internal error: ${error.message}`)
.catch(console.error);
});
client.on("interactionCreate", async (interaction) => {
const command = interaction.commandName;
if (!interaction.isChatInputCommand()) return;
const fileCommand = client.fileCommands.get(command);
if (fileCommand) {
try {
if (fileCommand.permissions) {
const member = interaction.member;
for (const permission of fileCommand.permissions) {
if (
!member.roles.cache.has(permission) &&
member.id !== USER_ID_DEBUG
) {
await interaction.reply({
content: "You lack the required permissions",
ephemeral: true,
});
return;
}
}
}
await fileCommand.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "Internal error executing command from file",
ephemeral: true,
});
}
}
});
client.login(TOKEN);