diff --git a/commands/server/start-mc.js b/commands/server/start-mc.js new file mode 100644 index 0000000..938c98f --- /dev/null +++ b/commands/server/start-mc.js @@ -0,0 +1,58 @@ +const { ButtonBuilder, ButtonStyle, SlashCommandBuilder, ActionRow, ActionRowBuilder} = require('discord.js') +const config = require('../../config.json') + +if (!config.proxmoxUser || !config.proxmoxPass || !config.proxmoxHostname) { + console.error("Proxmox credentials not found in config.json. Exiting."); + process.exit(1); +} +proxmox = require("proxmox")(config.proxmoxUser, config.proxmoxPass, config.proxmoxHostname); + +module.exports = { + cooldowns: 60, + data: new SlashCommandBuilder() + .setName('start-mc') + .setDescription('Starts the minecraft server machine.'), + async execute(interaction) { + const confirm = new ButtonBuilder() + .setCustomId('confirm') + .setLabel('Confirm') + .setStyle(ButtonStyle.Primary) + const cancel = new ButtonBuilder() + .setCustomId('cancel') + .setLabel('Cancel') + .setStyle(ButtonStyle.Danger) + + const row = new ActionRowBuilder() + .addComponents(cancel, confirm) + + const response = await interaction.reply({ + content: 'Are you sure you want to start the Minecraft server?', + components: [row] + }) + + const collectorFilter = i => i.user.id === interaction.user.id; + try { + const confirmation = await response.awaitMessageComponent({ filter: collectorFilter, time: 30_000 }); + if (confirmation.customId === 'confirm') { + proxmox.qemu.start("pve", "102", function(err, res) { + if (err) { + confirmation.update("Error starting the Minecraft server: " + err); + } else { + const linkButton = new ButtonBuilder() + .setStyle(ButtonStyle.Link) + .setLabel('Web Interface') + .setURL('https://mc.louisgallet.fr') + const row = new ActionRowBuilder() + .addComponents(linkButton) + confirmation.update({content: "Minecraft server started successfully. Please wait a few minutes for it to boot.", components: [row]}); + } + }) + } else if (confirmation.customId === 'cancel') { + await confirmation.update({ content: 'Minecraft server start cancelled.', components: [] }); + } + } catch (e) { + await confirmation.update({ content: 'You took too long to respond.', components: [] }); + } + + } +} diff --git a/commands/server/stop-mc.js b/commands/server/stop-mc.js new file mode 100644 index 0000000..fb9861b --- /dev/null +++ b/commands/server/stop-mc.js @@ -0,0 +1,23 @@ +const { SlashCommandBuilder } = require('discord.js') +const config = require('../../config.json') + +if (!config.proxmoxUser || !config.proxmoxPass || !config.proxmoxHostname) { + console.error("Proxmox credentials not found in config.json. Exiting."); + process.exit(1); +} +proxmox = require("proxmox")(config.proxmoxUser, config.proxmoxPass, config.proxmoxHostname); + +module.exports = { + data: new SlashCommandBuilder() + .setName('stop-mc') + .setDescription('Stops the minecraft server machine.'), + async execute(interaction) { + proxmox.qemu.shutdown("pve", "102", function(err, res) { + if (err) { + interaction.reply("Error starting the Minecraft server: " + err); + } else { + interaction.reply("Minecraft server stopped successfully."); + } + }) + } +} diff --git a/commands/utility/ping.js b/commands/utility/ping.js new file mode 100644 index 0000000..27edf1b --- /dev/null +++ b/commands/utility/ping.js @@ -0,0 +1,10 @@ +const { SlashCommandBuilder } = require('discord.js') + +module.exports = { + data: new SlashCommandBuilder() + .setName('ping') + .setDescription('Replies with Pong'), + async execute(interaction) { + await interaction.reply('Pong') + }, +} \ No newline at end of file diff --git a/commands/utility/reload.js b/commands/utility/reload.js new file mode 100644 index 0000000..61384cc --- /dev/null +++ b/commands/utility/reload.js @@ -0,0 +1,32 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + category: 'utility', + data: new SlashCommandBuilder() + .setName('reload') + .setDescription('Reloads a command.') + .addStringOption(option => + option.setName('command') + .setDescription('The command to reload.') + .setRequired(true)), + async execute(interaction) { + const commandName = interaction.options.getString('command', true).toLowerCase(); + const command = interaction.client.commands.get(commandName); + + if (!command) { + return interaction.reply(`There is no command with name \`${commandName}\`!`); + } + + delete require.cache[require.resolve(`../${command.category}/${command.data.name}.js`)]; + + try { + await interaction.client.commands.delete(command.data.name); + const newCommand = require(`../${command.category}/${command.data.name}.js`); + await interaction.client.commands.set(newCommand.data.name, newCommand); + await interaction.reply(`Command \`${newCommand.data.name}\` was reloaded!`); + } catch (error) { + console.error(error); + await interaction.reply(`There was an error while reloading a command \`${command.data.name}\`:\n\`${error.message}\``); + } + }, +}; \ No newline at end of file diff --git a/commands/utility/server.js b/commands/utility/server.js new file mode 100644 index 0000000..db682b4 --- /dev/null +++ b/commands/utility/server.js @@ -0,0 +1,11 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('server') + .setDescription('Provides information about the server.'), + async execute(interaction) { + // interaction.guild is the object representing the Guild in which the command was run + await interaction.reply(`This server is ${interaction.guild.name} and has ${interaction.guild.memberCount} members.`); + }, +}; diff --git a/commands/utility/user.js b/commands/utility/user.js new file mode 100644 index 0000000..fdd3097 --- /dev/null +++ b/commands/utility/user.js @@ -0,0 +1,12 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('user') + .setDescription('Provides information about the user.'), + async execute(interaction) { + // interaction.user is the object representing the User who ran the command + // interaction.member is the GuildMember object, which represents the user in the specific guild + await interaction.reply(`This command was run by ${interaction.user.username}, who joined on ${interaction.member.joinedAt}.`); + }, +}; diff --git a/config.json.example b/config.json.example index 56f07a6..2c86260 100644 --- a/config.json.example +++ b/config.json.example @@ -1,3 +1,8 @@ { - "token": "your-token-goes-here" + "token": "your-token-goes-here", + "clientId": "your-application-id-goes-here", + "guildId": "your-server-id-goes-here" + "proxmox-hostname": "your-proxmox-hostname-here" + "proxmox-user": "your-proxmox-user-here" + "proxmox-pass": "your-proxmox-pass-here" } diff --git a/deploy-commands.js b/deploy-commands.js new file mode 100644 index 0000000..dad7f8c --- /dev/null +++ b/deploy-commands.js @@ -0,0 +1,46 @@ +const { REST, Routes } = require('discord.js'); +const { clientId, guildId, token } = require('./config.json'); +const fs = require('node:fs'); +const path = require('node:path'); + +const commands = []; +// Grab all the command folders from the commands directory you created earlier +const foldersPath = path.join(__dirname, 'commands'); +const commandFolders = fs.readdirSync(foldersPath); + +for (const folder of commandFolders) { + // Grab all the command files from the commands directory you created earlier + const commandsPath = path.join(foldersPath, folder); + const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); + // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment + for (const file of commandFiles) { + const filePath = path.join(commandsPath, file); + const command = require(filePath); + if ('data' in command && 'execute' in command) { + commands.push(command.data.toJSON()); + } else { + console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); + } + } +} + +// Construct and prepare an instance of the REST module +const rest = new REST().setToken(token); + +// and deploy your commands! +(async () => { + try { + console.log(`Started refreshing ${commands.length} application (/) commands.`); + + // The put method is used to fully refresh all commands in the guild with the current set + const data = await rest.put( + Routes.applicationGuildCommands(clientId, guildId), + { body: commands }, + ); + + console.log(`Successfully reloaded ${data.length} application (/) commands.`); + } catch (error) { + // And of course, make sure you catch and log any errors! + console.error(error); + } +})(); diff --git a/events/interactionCreate.js b/events/interactionCreate.js new file mode 100644 index 0000000..5f445f1 --- /dev/null +++ b/events/interactionCreate.js @@ -0,0 +1,26 @@ +const { Events } = require('discord.js'); + +module.exports = { + name: Events.InteractionCreate, + async execute(interaction) { + if (!interaction.isChatInputCommand()) return; + + const command = interaction.client.commands.get(interaction.commandName); + + if (!command) { + console.error(`No command matching ${interaction.commandName} was found.`); + return; + } + + try { + 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 }); + } + } + }, +}; diff --git a/events/ready.js b/events/ready.js new file mode 100644 index 0000000..ea615aa --- /dev/null +++ b/events/ready.js @@ -0,0 +1,9 @@ +const { Events } = require('discord.js'); + +module.exports = { + name: Events.ClientReady, + once: true, + execute(client) { + console.log(`Ready! Logged in as ${client.user.tag}`); + }, +}; diff --git a/index.js b/index.js index 9a5cfd4..6449c3d 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,75 @@ -const { Client, Events, GatewayIntentBits} = require("discord.js") -const { token } = require("./config.json") +const fs = require('node:fs'); +const path = require('node:path'); +const { Client, Collection, Events, GatewayIntentBits } = require('discord.js'); +const { token } = require('./config.json'); -const client = new Client({ intents: [GatewayIntentBits.Guilds] }) +const client = new Client({ intents: [GatewayIntentBits.Guilds] }); -client.once(Events.ClientReady, readyClient => { - console.log(`Ready! Logged in as ${readyClient.user.tag}`) -}) +client.cooldowns = new Collection(); +client.commands = new Collection(); +const foldersPath = path.join(__dirname, 'commands'); +const commandFolders = fs.readdirSync(foldersPath); -client.login(token) \ No newline at end of file +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); + 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(Events.ClientReady, c => { + console.log(`Ready! Logged in as ${c.user.tag}`); +}); + +client.on(Events.InteractionCreate, async interaction => { + if (!interaction.isChatInputCommand()) return; + const command = client.commands.get(interaction.commandName); + + if (!command) { + console.error(`No command matching ${interaction.commandName} was found.`); + return; + } + + const { cooldowns } = interaction.client; + + if (!cooldowns.has(command.data.name)) { + cooldowns.set(command.data.name, new Collection()); + } + + const now = Date.now(); + const timestamps = cooldowns.get(command.data.name); + const defaultCooldownDuration = 3; + const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000; + + if (timestamps.has(interaction.user.id)) { + const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount; + + if (now < expirationTime) { + const expiredTimestamp = Math.round(expirationTime / 1000); + return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again .`, ephemeral: true }); + } + } + + timestamps.set(interaction.user.id, now); + setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount); + + try { + 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(token); diff --git a/package.json b/package.json index 1f62008..10e7908 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "dev": "nodemon index.js", - "start": "node index.js", + "dev": "node deploy-commands.js && nodemon index.js", + "start": "node deploy-commands.js && node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [],