From a0656c5831c8e88bf33a28c580d93fff9392b7bd Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Sun, 3 Mar 2024 15:10:08 +0100 Subject: [PATCH] feat: :sparkles: Introduce role check and timer before server stop --- .github/workflows/push-docker.yml | 16 ++++++++++++---- commands/server/start-mc.js | 20 ++++++++++++++++---- commands/server/stop-mc.js | 2 +- commands/utility/check-perms.js | 17 +++++++++++++++++ commands/utility/test-time.js | 13 +++++++++++++ 5 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 commands/utility/check-perms.js create mode 100644 commands/utility/test-time.js diff --git a/.github/workflows/push-docker.yml b/.github/workflows/push-docker.yml index 82b0090..25676ae 100644 --- a/.github/workflows/push-docker.yml +++ b/.github/workflows/push-docker.yml @@ -7,12 +7,12 @@ # To get a newer version, you will need to update the SHA. # You can also reference a tag or branch, but the action may change without warning. -name: Publish Docker image +name: Publish staging Docker image on: - release: - types: [published] push: + branches: + - feature/* jobs: push_to_registry: @@ -30,7 +30,15 @@ jobs: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - + - + # Add support for more platforms with QEMU (optional) + # https://github.com/docker/setup-qemu-action + name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Build and push Docker image uses: docker/build-push-action@v2 with: diff --git a/commands/server/start-mc.js b/commands/server/start-mc.js index 938c98f..e2355e5 100644 --- a/commands/server/start-mc.js +++ b/commands/server/start-mc.js @@ -29,12 +29,25 @@ module.exports = { content: 'Are you sure you want to start the Minecraft server?', components: [row] }) - + // Before stop variable which return the time between now and 4am in discord timestamp + const date = new Date(new Date().setDate(new Date().getDate() + 1)) + date.setUTCHours(3, 0, 0, 0) + const test = Math.round(date.getTime() / 1000); const collectorFilter = i => i.user.id === interaction.user.id; + let allowed = false; + for (const role of config.allowedRoles) { + if (interaction.member.roles.cache.has(role)) { + allowed = true; + } + } try { const confirmation = await response.awaitMessageComponent({ filter: collectorFilter, time: 30_000 }); if (confirmation.customId === 'confirm') { - proxmox.qemu.start("pve", "102", function(err, res) { + if (!allowed) { + await confirmation.update({ content: 'You do not have permission to start the Minecraft server.', components: [] }); + return; + } + proxmox.qemu.start("pve", "103", function(err, res) { if (err) { confirmation.update("Error starting the Minecraft server: " + err); } else { @@ -44,7 +57,7 @@ module.exports = { .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]}); + confirmation.update({content: "Minecraft server started successfully. Please wait a few minutes for it to boot. Use the command `/stop-mc` to stop it, otherwise, the server will be automatically shut down " + ``, components: [row]}); } }) } else if (confirmation.customId === 'cancel') { @@ -53,6 +66,5 @@ module.exports = { } 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 index fb9861b..6697fef 100644 --- a/commands/server/stop-mc.js +++ b/commands/server/stop-mc.js @@ -12,7 +12,7 @@ module.exports = { .setName('stop-mc') .setDescription('Stops the minecraft server machine.'), async execute(interaction) { - proxmox.qemu.shutdown("pve", "102", function(err, res) { + proxmox.qemu.shutdown("pve", "103", function(err, res) { if (err) { interaction.reply("Error starting the Minecraft server: " + err); } else { diff --git a/commands/utility/check-perms.js b/commands/utility/check-perms.js new file mode 100644 index 0000000..4ff3080 --- /dev/null +++ b/commands/utility/check-perms.js @@ -0,0 +1,17 @@ +const { SlashCommandBuilder } = require('discord.js') +const config = require('../../config.json') + +module.exports = { + data: new SlashCommandBuilder() + .setName('check-perms') + .setDescription('Check if the user has the correct permissions to start the Minecraft server.'), + async execute(interaction) { + for (const role of config.allowedRoles) { + if (interaction.member.roles.cache.has(role)) { + await interaction.reply({ content: 'You have the correct permissions to start the Minecraft server.', ephemeral: true }); + return; + } + } + await interaction.reply({ content: 'You have the correct permissions to start the Minecraft server.', ephemeral: true }); + }, +} \ No newline at end of file diff --git a/commands/utility/test-time.js b/commands/utility/test-time.js new file mode 100644 index 0000000..deb76f1 --- /dev/null +++ b/commands/utility/test-time.js @@ -0,0 +1,13 @@ +const { SlashCommandBuilder } = require('discord.js') + +module.exports = { + data: new SlashCommandBuilder() + .setName('test-time') + .setDescription('Test the time between now and 4am in discord timestamp'), + async execute(interaction) { + const date = new Date(new Date().setDate(new Date().getDate() + 1)) + date.setUTCHours(3, 0, 0, 0) + const test = Math.round(date.getTime() / 1000); + await interaction.reply({ content: `Time between now and 4am: `}, {ephemeral: true}); + }, +} \ No newline at end of file