From 7debcda4bc463941b67accfacc9a0b9fd091e441 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Tue, 27 Feb 2024 11:53:24 +0100 Subject: [PATCH 1/8] ci: :construction_worker: Create CI build system --- .gitea/workflows/push-docker.yml | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 .gitea/workflows/push-docker.yml diff --git a/.gitea/workflows/push-docker.yml b/.gitea/workflows/push-docker.yml new file mode 100644 index 0000000..a0fb4e9 --- /dev/null +++ b/.gitea/workflows/push-docker.yml @@ -0,0 +1,44 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +# GitHub recommends pinning actions to a commit SHA. +# 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 + +on: + release: + types: [published] + +jobs: + push_to_registry: + name: Push Docker image to Docker Hub + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v4 + + - name: Log in to Docker Hub + uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a + with: + registry: gitea.louisgallet.fr + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: lgallet/bot-louis + + - name: Build and push Docker image + uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 + with: + context: . + file: ./Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} From 62bf1d12b9afe69e064f5ae6b92a924f4ce280de Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Tue, 27 Feb 2024 11:59:23 +0100 Subject: [PATCH 2/8] ci: :construction_worker: Add build docker ci --- {.gitea => .github}/workflows/push-docker.yml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) rename {.gitea => .github}/workflows/push-docker.yml (82%) diff --git a/.gitea/workflows/push-docker.yml b/.github/workflows/push-docker.yml similarity index 82% rename from .gitea/workflows/push-docker.yml rename to .github/workflows/push-docker.yml index a0fb4e9..c45ca2c 100644 --- a/.gitea/workflows/push-docker.yml +++ b/.github/workflows/push-docker.yml @@ -12,6 +12,7 @@ name: Publish Docker image on: release: types: [published] + push: jobs: push_to_registry: @@ -28,11 +29,6 @@ jobs: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 - with: - images: lgallet/bot-louis - name: Build and push Docker image uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 @@ -40,5 +36,5 @@ jobs: context: . file: ./Dockerfile push: true - tags: ${{ steps.meta.outputs.tags }} + tags: gitea.louisgallet.fr/lgallet/bot-louis:staging labels: ${{ steps.meta.outputs.labels }} From 9d2a66a1fb869ef75dfb77dcbb54dc3c83432960 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Tue, 27 Feb 2024 15:45:05 +0100 Subject: [PATCH 3/8] ci: :green_heart: Change CI to install docker --- .github/workflows/push-docker.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/push-docker.yml b/.github/workflows/push-docker.yml index c45ca2c..17451f1 100644 --- a/.github/workflows/push-docker.yml +++ b/.github/workflows/push-docker.yml @@ -16,12 +16,13 @@ on: jobs: push_to_registry: - name: Push Docker image to Docker Hub + name: Push Docker image to Gitea Container Registry runs-on: ubuntu-latest steps: - name: Check out the repo uses: actions/checkout@v4 - + - name: Install Docker + run: curl -fsSL https://get.docker.com | sh - name: Log in to Docker Hub uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a with: From 30e9eab1a63474b04478ef400ff6a6f21d97e296 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Tue, 27 Feb 2024 15:46:46 +0100 Subject: [PATCH 4/8] ci: :green_heart: Fix labels --- .github/workflows/push-docker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/push-docker.yml b/.github/workflows/push-docker.yml index 17451f1..34bf26a 100644 --- a/.github/workflows/push-docker.yml +++ b/.github/workflows/push-docker.yml @@ -38,4 +38,4 @@ jobs: file: ./Dockerfile push: true tags: gitea.louisgallet.fr/lgallet/bot-louis:staging - labels: ${{ steps.meta.outputs.labels }} + labels: VERSION=staging From 320cdc2b1485dbf8239874c723dbc644ae3c2224 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Tue, 27 Feb 2024 15:49:16 +0100 Subject: [PATCH 5/8] ci: :green_heart: Change version --- .github/workflows/push-docker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/push-docker.yml b/.github/workflows/push-docker.yml index 34bf26a..82b0090 100644 --- a/.github/workflows/push-docker.yml +++ b/.github/workflows/push-docker.yml @@ -32,7 +32,7 @@ jobs: - name: Build and push Docker image - uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 + uses: docker/build-push-action@v2 with: context: . file: ./Dockerfile From a0656c5831c8e88bf33a28c580d93fff9392b7bd Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Sun, 3 Mar 2024 15:10:08 +0100 Subject: [PATCH 6/8] 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 From de489fe79a0051bc956d08c88dd331600a9ce229 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Sun, 3 Mar 2024 15:16:07 +0100 Subject: [PATCH 7/8] ci: :green_heart: Update ci with stagging and stable --- .github/workflows/push-docker-stable.yml | 50 +++++++++++++++++++ ...sh-docker.yml => push-docker-stagging.yml} | 3 +- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/push-docker-stable.yml rename .github/workflows/{push-docker.yml => push-docker-stagging.yml} (97%) diff --git a/.github/workflows/push-docker-stable.yml b/.github/workflows/push-docker-stable.yml new file mode 100644 index 0000000..45cabb3 --- /dev/null +++ b/.github/workflows/push-docker-stable.yml @@ -0,0 +1,50 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +# GitHub recommends pinning actions to a commit SHA. +# 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 staging Docker image + +on: + push: + branches: + - main + +jobs: + push_to_registry: + name: Push Docker image to Gitea Container Registry + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v4 + - name: Install Docker + run: curl -fsSL https://get.docker.com | sh + - name: Log in to Docker Hub + uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a + with: + registry: gitea.louisgallet.fr + 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: + context: . + platforms: "linux/amd64,linux/arm64" + file: ./Dockerfile + push: true + tags: gitea.louisgallet.fr/lgallet/bot-louis:latest + labels: VERSION=stable diff --git a/.github/workflows/push-docker.yml b/.github/workflows/push-docker-stagging.yml similarity index 97% rename from .github/workflows/push-docker.yml rename to .github/workflows/push-docker-stagging.yml index 25676ae..df72163 100644 --- a/.github/workflows/push-docker.yml +++ b/.github/workflows/push-docker-stagging.yml @@ -38,11 +38,12 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - + - name: Build and push Docker image uses: docker/build-push-action@v2 with: context: . + platforms: "linux/amd64,linux/arm64" file: ./Dockerfile push: true tags: gitea.louisgallet.fr/lgallet/bot-louis:staging From 88c010b0fe860693185c3b4a2055e4b98aecc205 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Sun, 3 Mar 2024 15:16:58 +0100 Subject: [PATCH 8/8] fix: :bug: Fix file name --- .../{push-docker-stagging.yml => push-docker-staging.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{push-docker-stagging.yml => push-docker-staging.yml} (100%) diff --git a/.github/workflows/push-docker-stagging.yml b/.github/workflows/push-docker-staging.yml similarity index 100% rename from .github/workflows/push-docker-stagging.yml rename to .github/workflows/push-docker-staging.yml