From b07844b59b16ebf11e5c3c06ec6ac7ae2217f24f Mon Sep 17 00:00:00 2001 From: AizuddinAkmal <29974506+AizuddinAkmal@users.noreply.github.com> Date: Sun, 7 Apr 2024 06:56:53 +0800 Subject: [PATCH 1/2] Update to use non-root user and assign appropriate permissions --- Dockerfile | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index be11515..4e76020 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,23 +1,31 @@ -FROM node:alpine AS build +FROM node:latest AS build WORKDIR /usr/src/bot -RUN apk add --update alpine-sdk libtool autoconf automake python3 +RUN apt-get update && apt-get install -y build-essential libtool autoconf automake python3 COPY package.json ./ COPY yarn.lock ./ +ENV NODE_ENV production + RUN yarn global add node-gyp -RUN yarn install +RUN yarn install --production + +FROM node:21.7.2-bookworm-slim + +ENV NODE_ENV production + +RUN apt-get update && apt-get install -y dumb-init -FROM node:alpine +USER node WORKDIR /usr/src/bot -COPY --from=build /usr/src/bot/node_modules ./node_modules +COPY --chown=node:node --from=build /usr/src/bot/node_modules ./node_modules -COPY . ./ +COPY --chown=node:node . ./ -CMD ["node", "bot.js"] +CMD ["dumb-init", "node", "bot.js"] From b18b6abbd72be16a83af159844eb7ee51048b0b5 Mon Sep 17 00:00:00 2001 From: AizuddinAkmal <29974506+AizuddinAkmal@users.noreply.github.com> Date: Mon, 8 Apr 2024 01:51:57 +0800 Subject: [PATCH 2/2] Updated Dockerfile to run deploy-command.js. Updated PlayAudio & AudioControl to handle subfolders --- AudioBackend/AudioControl.js | 6 +++++- AudioBackend/PlayAudio.js | 13 +++++++++---- Commands/about.js | 2 +- Dockerfile | 4 +++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/AudioBackend/AudioControl.js b/AudioBackend/AudioControl.js index 7f67838..933f308 100644 --- a/AudioBackend/AudioControl.js +++ b/AudioBackend/AudioControl.js @@ -23,10 +23,14 @@ import { shufflePlaylist, orderPlaylist } from './QueueSystem.js'; import { playAudio, currentTrack, updatePlaylist } from './PlayAudio.js'; import { player } from './VoiceInitialization.js'; import i18next from '../Utilities/i18n.js'; +import { resolve, extname, basename } from 'node:path' const t = i18next.t; +const audioFileExt = ['.mp3', '.flac']; const { shuffle, repeat } = JSON.parse(readFileSync('./config.json', 'utf-8')); -export const files = readdirSync('music'); +export const files = readdirSync('music', { withFileTypes: true, recursive: true }) + .filter(file => file.isFile() && audioFileExt.includes(extname(file.name).toLowerCase())) + .map(file => resolve(file.path, file.name)); export let playerState; export let playerStatus; export let isAudioStatePaused; diff --git a/AudioBackend/PlayAudio.js b/AudioBackend/PlayAudio.js index beb098d..87976b5 100644 --- a/AudioBackend/PlayAudio.js +++ b/AudioBackend/PlayAudio.js @@ -26,6 +26,7 @@ import { player } from './VoiceInitialization.js'; import { audioState, files } from './AudioControl.js'; import { integer } from '../Commands/play.js'; import i18next from '../Utilities/i18n.js'; +import { resolve, extname, basename } from 'node:path'; const { statusChannel, txtFile } = JSON.parse(readFileSync('./config.json', 'utf-8')); const t = i18next.t; @@ -44,9 +45,13 @@ export let audioAlbum; export let audioPicture; export let duration; -const inputFiles = readdirSync('music'); +const audioFileExt = ['.mp3', '.flac']; +const inputFiles = await readdirSync('music', { withFileTypes: true, recursive: true }) + .filter(file => file.isFile() && audioFileExt.includes(extname(file.name.toLowerCase()))) + .map(file => resolve(file.path, file.name)); + export async function playAudio(bot) { - const resource = createAudioResource('music/' + audio); + const resource = createAudioResource(audio); player.play(resource); console.log(t('nowPlayingFile', { audio })); @@ -54,10 +59,10 @@ export async function playAudio(bot) { audioState(0); audioPicture = null; - const audioFile = audio; + const audioFile = basename(audio); try { - const { common, format } = await parseFile('music/' + audio); + const { common, format } = await parseFile(audio); metadataEmpty = false; if (common.title && common.artist && common.year && common.album) { audioTitle = common.title; diff --git a/Commands/about.js b/Commands/about.js index 8b63ca2..7eae862 100644 --- a/Commands/about.js +++ b/Commands/about.js @@ -36,7 +36,7 @@ export default { { name: t('aboutInfo'), value: t('aboutInfoValue') }, { name: t('aboutBotVersion'), value: `DLAP ${npmPackage.version}` }, { name: t('aboutCreator'), value: 'Andrew Lee (alee)' }, // Do not remove this since I created this :) - { name: t('aboutContributors'), value: 'Victor Moraes (Vicktor#7232) (Improving README)\nParlance Translation Team' }, + { name: t('aboutContributors'), value: 'Victor Moraes (Vicktor#7232) (Improving README)\nParlance Translation Team\n\nAizuddin Akmal (AizuddinAkmal) (Improved Dockerfile & Music folder handling)' }, // { name: t('aboutForked'), value: '[your name] (username)' }, { name: t('aboutFrameworks'), value: `Discord.JS ${version}\nmusic-metadata\ni18next` }, { name: t('aboutLicense'), value: 'GNU General Public License v3.0' } diff --git a/Dockerfile b/Dockerfile index 4e76020..30c8e50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,4 +28,6 @@ COPY --chown=node:node --from=build /usr/src/bot/node_modules ./node_modules COPY --chown=node:node . ./ -CMD ["dumb-init", "node", "bot.js"] +ENTRYPOINT ["/usr/bin/dumb-init", "--"] + +CMD ["bash", "-c", "node deploy-command.js && exec node bot.js"]