Merge pull request 'feature/eslint' (#2) from feature/eslint into main

Reviewed-on: #2
This commit is contained in:
Louis Gallet 2024-02-27 10:43:22 +00:00
commit 8b803c47f0
13 changed files with 238 additions and 210 deletions

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</component>

6
.idea/jsLinters/eslint.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EslintConfiguration">
<option name="fix-on-save" value="true" />
</component>
</project>

View File

@ -1,11 +1,12 @@
const { ButtonBuilder, ButtonStyle, SlashCommandBuilder, ActionRow, ActionRowBuilder} = require('discord.js') const { ButtonBuilder, ButtonStyle, SlashCommandBuilder, ActionRowBuilder } = require('discord.js');
const config = require('../../config.json') const config = require('../../config.json');
if (!config.proxmoxUser || !config.proxmoxPass || !config.proxmoxHostname) { if (!config.proxmoxUser || !config.proxmoxPass || !config.proxmoxHostname) {
console.error("Proxmox credentials not found in config.json. Exiting."); console.error('Proxmox credentials not found in config.json. Exiting.');
process.exit(1); process.exit(1);
} }
proxmox = require("proxmox")(config.proxmoxUser, config.proxmoxPass, config.proxmoxHostname); // eslint-disable-next-line no-undef
proxmox = require('proxmox')(config.proxmoxUser, config.proxmoxPass, config.proxmoxHostname);
module.exports = { module.exports = {
cooldowns: 60, cooldowns: 60,
@ -16,43 +17,48 @@ module.exports = {
const confirm = new ButtonBuilder() const confirm = new ButtonBuilder()
.setCustomId('confirm') .setCustomId('confirm')
.setLabel('Confirm') .setLabel('Confirm')
.setStyle(ButtonStyle.Primary) .setStyle(ButtonStyle.Primary);
const cancel = new ButtonBuilder() const cancel = new ButtonBuilder()
.setCustomId('cancel') .setCustomId('cancel')
.setLabel('Cancel') .setLabel('Cancel')
.setStyle(ButtonStyle.Danger) .setStyle(ButtonStyle.Danger);
const row = new ActionRowBuilder() const row = new ActionRowBuilder()
.addComponents(cancel, confirm) .addComponents(cancel, confirm);
const response = await interaction.reply({ const response = await interaction.reply({
content: 'Are you sure you want to start the Minecraft server?', content: 'Are you sure you want to start the Minecraft server?',
components: [row] components: [row],
}) });
const collectorFilter = i => i.user.id === interaction.user.id; const collectorFilter = i => i.user.id === interaction.user.id;
try { try {
const confirmation = await response.awaitMessageComponent({ filter: collectorFilter, time: 30_000 }); const confirmation = await response.awaitMessageComponent({ filter: collectorFilter, time: 30_000 });
if (confirmation.customId === 'confirm') { if (confirmation.customId === 'confirm') {
proxmox.qemu.start("pve", "102", function(err, res) { // eslint-disable-next-line no-undef
proxmox.qemu.start('pve', '102', function(err) {
if (err) { if (err) {
confirmation.update("Error starting the Minecraft server: " + err); confirmation.update('Error starting the Minecraft server: ' + err);
} else { }
else {
const linkButton = new ButtonBuilder() const linkButton = new ButtonBuilder()
.setStyle(ButtonStyle.Link) .setStyle(ButtonStyle.Link)
.setLabel('Web Interface') .setLabel('Web Interface')
.setURL('https://mc.louisgallet.fr') .setURL('https://mc.louisgallet.fr');
const row = new ActionRowBuilder() const linkRow = new ActionRowBuilder()
.addComponents(linkButton) .addComponents(linkButton);
confirmation.update({content: "Minecraft server started successfully. Please wait a few minutes for it to boot.", components: [row]}); confirmation.update({ content: 'Minecraft server started successfully. Please wait a few minutes for it to boot.', components: [linkRow] });
} }
}) });
} else if (confirmation.customId === 'cancel') { }
else if (confirmation.customId === 'cancel') {
await confirmation.update({ content: 'Minecraft server start cancelled.', components: [] }); await confirmation.update({ content: 'Minecraft server start cancelled.', components: [] });
} }
} catch (e) { }
catch (e) {
// eslint-disable-next-line no-undef
await confirmation.update({ content: 'You took too long to respond.', components: [] }); await confirmation.update({ content: 'You took too long to respond.', components: [] });
} }
} },
} };

View File

@ -1,23 +1,24 @@
const { SlashCommandBuilder } = require('discord.js') const { SlashCommandBuilder } = require('discord.js');
const config = require('../../config.json') const config = require('../../config.json');
if (!config.proxmoxUser || !config.proxmoxPass || !config.proxmoxHostname) { if (!config.proxmoxUser || !config.proxmoxPass || !config.proxmoxHostname) {
console.error("Proxmox credentials not found in config.json. Exiting."); console.error('Proxmox credentials not found in config.json. Exiting.');
process.exit(1); process.exit(1);
} }
proxmox = require("proxmox")(config.proxmoxUser, config.proxmoxPass, config.proxmoxHostname); const proxmox = require('proxmox')(config.proxmoxUser, config.proxmoxPass, config.proxmoxHostname);
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName('stop-mc') .setName('stop-mc')
.setDescription('Stops the minecraft server machine.'), .setDescription('Stops the minecraft server machine.'),
async execute(interaction) { async execute(interaction) {
proxmox.qemu.shutdown("pve", "102", function(err, res) { proxmox.qemu.shutdown('pve', '102', function(err) {
if (err) { if (err) {
interaction.reply("Error starting the Minecraft server: " + err); interaction.reply('Error starting the Minecraft server: ' + err);
} else {
interaction.reply("Minecraft server stopped successfully.");
} }
}) else {
interaction.reply('Minecraft server stopped successfully.');
} }
} });
},
};

View File

@ -1,10 +1,10 @@
const { SlashCommandBuilder } = require('discord.js') const { SlashCommandBuilder } = require('discord.js');
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName('ping') .setName('ping')
.setDescription('Replies with Pong'), .setDescription('Replies with Pong'),
async execute(interaction) { async execute(interaction) {
await interaction.reply('Pong') await interaction.reply('Pong');
}, },
} };

View File

@ -24,7 +24,8 @@ module.exports = {
const newCommand = require(`../${command.category}/${command.data.name}.js`); const newCommand = require(`../${command.category}/${command.data.name}.js`);
await interaction.client.commands.set(newCommand.data.name, newCommand); await interaction.client.commands.set(newCommand.data.name, newCommand);
await interaction.reply(`Command \`${newCommand.data.name}\` was reloaded!`); await interaction.reply(`Command \`${newCommand.data.name}\` was reloaded!`);
} catch (error) { }
catch (error) {
console.error(error); console.error(error);
await interaction.reply(`There was an error while reloading a command \`${command.data.name}\`:\n\`${error.message}\``); await interaction.reply(`There was an error while reloading a command \`${command.data.name}\`:\n\`${error.message}\``);
} }

View File

@ -18,7 +18,8 @@ for (const folder of commandFolders) {
const command = require(filePath); const command = require(filePath);
if ('data' in command && 'execute' in command) { if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON()); commands.push(command.data.toJSON());
} else { }
else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
} }
} }
@ -39,7 +40,8 @@ const rest = new REST().setToken(token);
); );
console.log(`Successfully reloaded ${data.length} application (/) commands.`); console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) { }
catch (error) {
// And of course, make sure you catch and log any errors! // And of course, make sure you catch and log any errors!
console.error(error); console.error(error);
} }

View File

@ -14,11 +14,13 @@ module.exports = {
try { try {
await command.execute(interaction); await command.execute(interaction);
} catch (error) { }
catch (error) {
console.error(error); console.error(error);
if (interaction.replied || interaction.deferred) { if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else { }
else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
} }
} }

View File

@ -18,7 +18,8 @@ for (const folder of commandFolders) {
const command = require(filePath); const command = require(filePath);
if ('data' in command && 'execute' in command) { if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command); client.commands.set(command.data.name, command);
} else { }
else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
} }
} }
@ -62,11 +63,13 @@ client.on(Events.InteractionCreate, async interaction => {
try { try {
await command.execute(interaction); await command.execute(interaction);
} catch (error) { }
catch (error) {
console.error(error); console.error(error);
if (interaction.replied || interaction.deferred) { if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else { }
else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
} }
} }

View File

@ -6,7 +6,8 @@
"scripts": { "scripts": {
"dev": "node deploy-commands.js && nodemon index.js", "dev": "node deploy-commands.js && nodemon index.js",
"start": "node deploy-commands.js && node index.js", "start": "node deploy-commands.js && node index.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint . --fix"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",