goofybot/index.js
2026-04-16 17:30:09 +02:00

102 lines
3.4 KiB
JavaScript

require('dotenv').config();
const fs = require('node:fs');
const path = require('node:path');
const deploy = require('./src/discord/deploy_command.js')
const { update, handlePinboardSelection } = require('./src/pterodactyl/displayer.js');
const clean = require('./src/pterodactyl/cleaner.js');
const { Client, GatewayIntentBits, Collection, Events, Partials } = require('discord.js');
const intents = [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions];
if (process.env.ENABLE_GUILD_MEMBERS_INTENT === 'true') {
intents.push(GatewayIntentBits.GuildMembers);
}
if (process.env.ENABLE_MESSAGE_CONTENT_INTENT === 'true') {
intents.push(GatewayIntentBits.MessageContent);
}
const client = new Client({ intents,
partials: [Partials.Message, Partials.Channel, Partials.Reaction, Partials.User],
});
const headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer " + process.env.PTERODACTYL_API_TOKEN
};
client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
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);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
client.once('clientReady', async () => {
client.user.setActivity({name: 'Ta mère', type: 1, url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'});
console.log('Bot started !');
deploy(process.env.DISCORD_TOKEN);
clean(client);
await update(client);
});
client.on(Events.InteractionCreate, async interaction => {
if (interaction.isStringSelectMenu() || interaction.isButton()) {
const handled = await handlePinboardSelection(interaction);
if (handled) {
return;
}
}
if (!interaction.isChatInputCommand()) return;
// Vérifier que la commande provient du bon serveur
if (interaction.guildId !== process.env.GUILD_ID) {
return interaction.reply({
content: 'Ce bot ne peut être utilisé que sur un serveur spécifique.',
ephemeral: true
});
}
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
if (interaction.commandName === 'start-server' || interaction.commandName === 'reboot-server') {
await command.execute(interaction, headers);
} else {
await command.execute(interaction);
}
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
});
client.login(process.env.DISCORD_TOKEN);
const displayerIntervalMs = Number(process.env.DISPLAYER_INTERVAL_MS || 120000);
setInterval(() => { update(client); }, displayerIntervalMs);