From cf5151fcd7236ca2d86d95684289245c3a4a71cc Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 12 Jul 2023 20:34:54 +0200 Subject: [PATCH 1/7] feat: system commands are no longer restricted to bot owner id, and can be used in list of specified guilds --- config.json.example | 11 +++++++++-- deploy-system-commands.js | 16 +++++++++------- system-commands/guilds.js | 11 ++--------- system-commands/status.js | 11 ++--------- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/config.json.example b/config.json.example index 30d75d1..fc07a16 100644 --- a/config.json.example +++ b/config.json.example @@ -1,8 +1,7 @@ { "clientId": "Your bot Application ID here.", "token": "Your bot auth token here.", - "systemServerGuildId": "Your Discord Server ID for system commands here.", - "botOwnerClientId": "Your own Discord user ID to access system commands here.", + "systemServerGuildIds": ["Server id where system commands can be used", "Another server id where system commands can be used"], "embedColors": { "colorSuccess": "#23A55A", "colorWarning": "#F0B232", @@ -21,6 +20,14 @@ "maxQueueSize": 1000, "maxHistorySize": 100 }, + "progressBarOptions": { + "length": 10, + "timecodes": false, + "separator": "┃", + "indicator": "🔘", + "leftChar": "▬", + "rightChar": "▬" + }, "minimumLogLevel": "debug", "minimumLogLevelConsole": "info", "filterThreadAmount": "4", diff --git a/deploy-system-commands.js b/deploy-system-commands.js index e1ca917..e6940af 100644 --- a/deploy-system-commands.js +++ b/deploy-system-commands.js @@ -1,7 +1,7 @@ const fs = require('node:fs'); const logger = require('./services/logger.js'); const { REST, Routes } = require('discord.js'); -const { token, clientId, systemServerGuildId } = require('./config.json'); +const { token, clientId, systemServerGuildIds } = require('./config.json'); const systemCommands = []; const systemCommandFiles = fs @@ -27,12 +27,14 @@ const rest = new REST({ version: '10' }).setToken(token); logger.info('Started refreshing application (/) system commands.'); - await rest.put( - Routes.applicationGuildCommands(clientId, systemServerGuildId), - { - body: systemCommands - } - ); + for (const systemServerGuildId of systemServerGuildIds) { + await rest.put( + Routes.applicationGuildCommands(clientId, systemServerGuildId), + { + body: systemCommands + } + ); + } logger.info('Successfully refreshed application (/) system commands.'); } catch (error) { diff --git a/system-commands/guilds.js b/system-commands/guilds.js index 15b91a0..9059405 100644 --- a/system-commands/guilds.js +++ b/system-commands/guilds.js @@ -1,20 +1,13 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); const { EmbedBuilder } = require('discord.js'); -const { - embedColors, - systemServerGuildId, - botOwnerClientId -} = require('../config.json'); +const { embedColors, systemServerGuildIds } = require('../config.json'); module.exports = { data: new SlashCommandBuilder() .setName('guilds') .setDescription('Show list of guilds where bot is added.'), run: async ({ interaction, client }) => { - if ( - interaction.guildId !== systemServerGuildId || - interaction.user.id !== botOwnerClientId - ) { + if (!systemServerGuildIds.includes(interaction.guildId)) { return await interaction.editReply({ embeds: [ new EmbedBuilder() diff --git a/system-commands/status.js b/system-commands/status.js index 304bd3c..95b9577 100644 --- a/system-commands/status.js +++ b/system-commands/status.js @@ -1,21 +1,14 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); const { EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); -const { - embedColors, - systemServerGuildId, - botOwnerClientId -} = require('../config.json'); +const { embedColors, systemServerGuildIds } = require('../config.json'); module.exports = { data: new SlashCommandBuilder() .setName('status') .setDescription('Show bot status.'), run: async ({ interaction, client }) => { - if ( - interaction.guildId !== systemServerGuildId || - interaction.user.id !== botOwnerClientId - ) { + if (!systemServerGuildIds.includes(interaction.guildId)) { return await interaction.editReply({ embeds: [ new EmbedBuilder() From cac18dda441517f16060949b44a4e8ecbb69ace5 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 12 Jul 2023 20:51:27 +0200 Subject: [PATCH 2/7] fix: update default value for progress bar length --- config.json.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.json.example b/config.json.example index fc07a16..7051373 100644 --- a/config.json.example +++ b/config.json.example @@ -21,7 +21,7 @@ "maxHistorySize": 100 }, "progressBarOptions": { - "length": 10, + "length": 12, "timecodes": false, "separator": "┃", "indicator": "🔘", From f87d35e635c8d26c0b3886ae561915289f968007 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 12 Jul 2023 21:01:07 +0200 Subject: [PATCH 3/7] fix: updated duration format in messages --- commands/play.js | 2 +- commands/skip.js | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/commands/play.js b/commands/play.js index 3761b97..0cf949c 100644 --- a/commands/play.js +++ b/commands/play.js @@ -105,7 +105,7 @@ module.exports = { let durationFormat = track.raw.duration === 0 || track.duration === '0:00' ? '' - : `\`[${track.duration}]\``; + : `\`${track.duration}\``; if (searchResult.playlist && searchResult.tracks.length > 1) { return await interaction.editReply({ diff --git a/commands/skip.js b/commands/skip.js index befc28f..e338099 100644 --- a/commands/skip.js +++ b/commands/skip.js @@ -57,6 +57,11 @@ module.exports = { }); } else { const skippedTrack = queue.currentTrack; + let durationFormat = + skippedTrack.raw.duration === 0 || + skippedTrack.duration === '0:00' + ? '' + : `\`${skippedTrack.duration}\``; queue.node.skipTo(skipToTrack - 1); return await interaction.editReply({ @@ -67,7 +72,7 @@ module.exports = { iconURL: interaction.user.avatarURL() }) .setDescription( - `**Skipped**\n**[${skippedTrack.title}](${skippedTrack.url})**.` + `**Skipped track**\n${durationFormat} **[${skippedTrack.title}](${skippedTrack.url})**.` ) .setThumbnail(skippedTrack.thumbnail) .setColor(embedColors.colorSuccess) @@ -88,6 +93,11 @@ module.exports = { } const skippedTrack = queue.currentTrack; + let durationFormat = + skippedTrack.raw.duration === 0 || + skippedTrack.duration === '0:00' + ? '' + : `\`${skippedTrack.duration}\``; queue.node.skip(); return await interaction.editReply({ @@ -98,7 +108,7 @@ module.exports = { iconURL: interaction.user.avatarURL() }) .setDescription( - `**Skipped**\n**[${skippedTrack.title}](${skippedTrack.url})**.` + `**Skipped track**\n${durationFormat} **[${skippedTrack.title}](${skippedTrack.url})**.` ) .setThumbnail(skippedTrack.thumbnail) .setColor(embedColors.colorSuccess) From df9b0005ad369bc889cf0a46b2e958606bf39661 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 12 Jul 2023 21:02:12 +0200 Subject: [PATCH 4/7] fix: update progress bar in queue command --- commands/queue.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/commands/queue.js b/commands/queue.js index 6554193..cf3d914 100644 --- a/commands/queue.js +++ b/commands/queue.js @@ -1,7 +1,7 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); const { useQueue } = require('discord-player'); const { EmbedBuilder } = require('discord.js'); -const { embedColors } = require('../config.json'); +const { embedColors, progressBarOptions } = require('../config.json'); module.exports = { data: new SlashCommandBuilder() @@ -71,7 +71,7 @@ module.exports = { let durationFormat = track.raw.duration === 0 || track.duration === '0:00' ? '' - : `\`[${track.duration}]\``; + : `\`${track.duration}\``; return `**${page * 10 + index + 1}.** ${durationFormat} [${ track.title @@ -98,10 +98,17 @@ module.exports = { ] }); } else { - let bar = queue.node.createProgressBar({ + const timestamp = queue.node.getTimestamp(); + let bar = `\`${ + timestamp.current.label + }\` ${queue.node.createProgressBar({ queue: false, - length: 13 - }); + length: progressBarOptions.length ?? 12, + timecodes: progressBarOptions.timecodes ?? false, + indicator: progressBarOptions.indicator ?? '🔘', + leftChar: progressBarOptions.leftChar ?? '▬', + rightChar: progressBarOptions.rightChar ?? '▬' + })} \`${timestamp.total.label}\``; if ( currentTrack.raw.duration === 0 || From da0519319d97ad81d3c0a44ab8fecd904f4ea388 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 12 Jul 2023 22:44:03 +0200 Subject: [PATCH 5/7] fix: Fixed execution time warnings and unexpected error --- index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 5545232..11ecc78 100644 --- a/index.js +++ b/index.js @@ -124,8 +124,12 @@ client.on('interactionCreate', async (interaction) => { const executionTime = outputTime - inputTime; if (executionTime > 20000) { - // don't send warning message for filters command, as collector timeout happens after 60 seconds - if (command.name === 'filters' && executionTime > 55000) { + // don't send warning message for commands with collector timeouts, as collector timeout happens after 60 seconds + if ( + (interaction.commandName === 'filters' || + interaction.commandName === 'nowplaying') && + executionTime > 55000 + ) { logger.info( `(${interaction.guild.memberCount}) ${interaction.guild.name}> Command '${interaction}' executed in ${executionTime} ms.` ); @@ -139,7 +143,7 @@ client.on('interactionCreate', async (interaction) => { // todo: using interaction.editReply() might lead to "unknown interaction" error // it might already have been replied to or deferred // solution might be to use interaction.followUp() instead or send a message to the channel? - return await interaction.editReply({ + return await interaction.followUp({ embeds: [ new EmbedBuilder() .setDescription( @@ -164,7 +168,7 @@ client.on('interactionCreate', async (interaction) => { // todo: using interaction.editReply() might lead to "unknown interaction" error // it might already have been replied to or deferred // solution might be to use interaction.followUp() instead or send a message to the channel? - await interaction.editReply({ + await interaction.followUp({ embeds: [ new EmbedBuilder() .setDescription( From 753432278479da9f27b4210507a8ed0442b771bd Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 12 Jul 2023 22:45:21 +0200 Subject: [PATCH 6/7] fix: defer confirmation on filters command --- commands/filters.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/commands/filters.js b/commands/filters.js index f085f75..15ca651 100644 --- a/commands/filters.js +++ b/commands/filters.js @@ -99,6 +99,8 @@ module.exports = { time: 60000 }); + confirmation.deferUpdate(); + queue.filters.ffmpeg.setInputArgs([ '-threads', filterThreadAmount, @@ -164,16 +166,11 @@ module.exports = { components: [] }); } catch (e) { - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**Cancelled**\nNo confirmation received within 1 minute.` - ) - .setColor(embedColors.colorWarning) - ], - components: [] - }); + if (e.code === 'InteractionCollectorError') { + return; + } + + throw e; } } }; From 16cc50b7eb708ff2940a423afe6a3067bde8b3d5 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 12 Jul 2023 22:46:20 +0200 Subject: [PATCH 7/7] feat: added /nowplaying command with interactive skip button --- commands/nowplaying.js | 208 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 commands/nowplaying.js diff --git a/commands/nowplaying.js b/commands/nowplaying.js new file mode 100644 index 0000000..acab980 --- /dev/null +++ b/commands/nowplaying.js @@ -0,0 +1,208 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const { useQueue } = require('discord-player'); +const { EmbedBuilder, ActionRowBuilder, ButtonBuilder } = require('discord.js'); +const { embedColors, progressBarOptions } = require('../config.json'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('nowplaying') + .setDescription('Show information about the track currently playing.'), + run: async ({ interaction }) => { + if (!interaction.member.voice.channel) { + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**Failed**\nYou need to be in a voice channel to use this command.` + ) + .setColor(embedColors.colorWarning) + ] + }); + } + + const queue = useQueue(interaction.guild.id); + + if (!queue) { + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**Failed**\nThere are no tracks in the queue. Add tracks with \`/play\`!` + ) + .setColor(embedColors.colorWarning) + ] + }); + } + + if (!queue.currentTrack) { + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**Failed**\nThere are no tracks in the queue or currently playing.` + ) + .setColor(embedColors.colorWarning) + ] + }); + } + + const sourceStringsFormatted = { + youtube: 'YouTube', + soundcloud: 'SoundCloud', + spotify: 'Spotify', + apple_music: 'Apple Music', + arbitrary: 'Direct source' + }; + + const currentTrack = queue.currentTrack; + const author = currentTrack.author ? currentTrack.author : 'Unknown'; + if (author === 'cdn.discordapp.com') { + author = 'Unknown'; + } + const plays = currentTrack.views !== 0 ? currentTrack.views : 'Unknown'; + const source = + sourceStringsFormatted[currentTrack.raw.source] ?? 'Unknown'; + const queueLength = queue.tracks.data.length; + const timestamp = queue.node.getTimestamp(); + let bar = `\`${ + timestamp.current.label + }\` ${queue.node.createProgressBar({ + queue: false, + length: progressBarOptions.length ?? 12, + timecodes: progressBarOptions.timecodes ?? false, + indicator: progressBarOptions.indicator ?? '🔘', + leftChar: progressBarOptions.leftChar ?? '▬', + rightChar: progressBarOptions.rightChar ?? '▬' + })} \`${timestamp.total.label}\``; + + if ( + currentTrack.raw.duration === 0 || + currentTrack.duration === '0:00' + ) { + bar = 'No duration available.'; + } + + const nowPlayingActionRow = new ActionRowBuilder().addComponents( + new ButtonBuilder() + .setCustomId('nowplaying-skip') + .setLabel('Skip track') + .setStyle('Secondary') + ); + + const response = await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setAuthor({ + name: interaction.guild.name, + iconURL: interaction.guild.iconURL() + }) + .setDescription( + (queue.node.isPaused() + ? `**Currently Paused**\n` + : `**Now Playing**\n`) + + (currentTrack + ? `**[${currentTrack.title}](${currentTrack.url})**` + : 'None') + + `\nRequested by: <@${currentTrack.requestedBy.id}>` + + `\n ${bar}\n\n` + ) + .addFields( + { + name: '**Author**', + value: author, + inline: true + }, + { + name: '**Plays**', + value: plays.toLocaleString('en-US'), + inline: true + }, + { + name: '**Audio source**', + value: `[${source}](${currentTrack.url})`, + inline: true + } + ) + .setFooter({ + text: queueLength + ? `${queueLength} other tracks in the queue...` + : ' ' + }) + .setThumbnail(queue.currentTrack.thumbnail) + .setColor(embedColors.colorSuccess) + ], + components: [nowPlayingActionRow] + }); + + const collectorFilter = (i) => i.user.id === interaction.user.id; + try { + const confirmation = await response.awaitMessageComponent({ + filter: collectorFilter, + time: 60000 + }); + + confirmation.deferUpdate(); + + if (confirmation.customId === 'nowplaying-skip') { + if ( + !queue || + (queue.tracks.data.length === 0 && !queue.currentTrack) + ) { + return await interaction.followUp({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**Failed**\nThere are no tracks in the queue and nothing currently playing.` + ) + .setColor(embedColors.colorWarning) + ], + components: [] + }); + } + + if (queue.currentTrack !== currentTrack) { + return await interaction.followUp({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**Failed to skip**\nThis track has already been skipped, or is no longer playing.` + ) + .setColor(embedColors.colorWarning) + ], + components: [] + }); + } + + const skippedTrack = queue.currentTrack; + let durationFormat = + skippedTrack.raw.duration === 0 || + skippedTrack.duration === '0:00' + ? '' + : `\`${skippedTrack.duration}\``; + queue.node.skip(); + + return await interaction.followUp({ + embeds: [ + new EmbedBuilder() + .setAuthor({ + name: interaction.user.username, + iconURL: interaction.user.avatarURL() + }) + .setDescription( + `**Skipped track**\n${durationFormat} **[${skippedTrack.title}](${skippedTrack.url})**.` + ) + .setThumbnail(skippedTrack.thumbnail) + .setColor(embedColors.colorSuccess) + ], + components: [] + }); + } + } catch (e) { + if (e.code === 'InteractionCollectorError') { + return; + } + + throw e; + } + } +};