From c7aa9b41c7ccf144a434449c2ba46c7d973db82d Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Sun, 9 Jul 2023 02:24:41 +0200 Subject: [PATCH] Added /filter command with configurable system options --- commands/filters.js | 181 ++++++++++++++++++++++++++++++++++++++++++++ commands/help.js | 3 +- config.json.example | 53 ++++++++++++- 3 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 commands/filters.js diff --git a/commands/filters.js b/commands/filters.js new file mode 100644 index 0000000..660c9ed --- /dev/null +++ b/commands/filters.js @@ -0,0 +1,181 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const { useMainPlayer, useQueue } = require('discord-player'); +const { + EmbedBuilder, + StringSelectMenuBuilder, + StringSelectMenuOptionBuilder, + ActionRowBuilder, + ButtonBuilder +} = require('discord.js'); +const { + embedColors, + filterList, + filterThreadAmount +} = require('../config.json'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('filters') + .setDescription('Toggle various audio filters during playback.'), + run: async ({ interaction }) => { + if (!interaction.member.voice.channel) { + return interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**Failed**\nYou need to be in a voice channel to use this command.` + ) + .setColor(embedColors.colorWarning) + ] + }); + } + + const queue = await useQueue(interaction.guild.id); + + if (!queue) { + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**Failed**\nThere are no tracks in the queue.` + ) + .setColor(embedColors.colorWarning) + ] + }); + } + + let filterOptions = []; + + filterList.forEach((filter) => { + let isEnabled = false; + + if (queue.filters.ffmpeg.filters.includes(filter.value)) { + isEnabled = true; + } + + filterOptions.push( + new StringSelectMenuOptionBuilder() + .setLabel(filter.label) + .setDescription(filter.description) + .setValue(filter.value) + .setEmoji(filter.emoji) + .setDefault(isEnabled) + ); + }); + + const filterSelect = new StringSelectMenuBuilder() + .setCustomId('filters') + .setPlaceholder('Select multiple options.') + .setMinValues(0) + .setMaxValues(filterOptions.length) + .addOptions(filterOptions); + + const filterActionRow = new ActionRowBuilder().addComponents( + filterSelect + ); + + const disableFiltersActionRow = new ActionRowBuilder().addComponents( + new ButtonBuilder() + .setCustomId('disable-filters') + .setLabel('Disable all filters') + .setStyle('Secondary') + ); + + const response = await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**Toggle filters** (beta)\nToggle or disable audio filters for playback.` + ) + .setColor(embedColors.colorInfo) + ], + components: [filterActionRow, disableFiltersActionRow] + }); + + const collectorFilter = (i) => i.user.id === interaction.user.id; + try { + const confirmation = await response.awaitMessageComponent({ + filter: collectorFilter, + time: 60000 + }); + + console.log(confirmation.values); + queue.filters.ffmpeg.setInputArgs([ + '-threads', + filterThreadAmount, + '-reconnect', + '1', + '-reconnect_streamed', + '1', + '-reconnect_delay_max', + '5', + '-vn' + ]); + + // Reset filters before enabling provided filters + if (queue.filters.ffmpeg.filters.length > 0) { + queue.filters.ffmpeg.setFilters(false); + } + + if ( + confirmation.customId === 'disable-filters' || + confirmation.values.length === 0 + ) { + return interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setAuthor({ + name: interaction.user.username, + iconURL: interaction.user.avatarURL() + }) + .setDescription( + `**Disabled filters**\nDisabled all audio filters.` + ) + .setColor(embedColors.colorSuccess) + ], + components: [] + }); + } + + // Enable provided filters + queue.filters.ffmpeg.toggle(confirmation.values); + + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setAuthor({ + name: interaction.user.username, + iconURL: interaction.user.avatarURL() + }) + .setDescription( + `**Filters enabled**\nThese filters are now enabled:\n${confirmation.values + .map( + (enabled) => + `\`${ + filterList.find( + (filter) => + enabled == filter.value + ).label + }\`` + ) + .join(', ')}` + ) + .setColor(embedColors.colorSuccess) + ], + components: [] + }); + } catch (e) { + console.log(e); + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**Cancelled**\nNo confirmation received within 1 minute.` + ) + .setColor(embedColors.colorWarning) + ], + components: [] + }); + } + } +}; diff --git a/commands/help.js b/commands/help.js index 098cc44..904ddff 100644 --- a/commands/help.js +++ b/commands/help.js @@ -20,7 +20,8 @@ module.exports = { `\`/remove\` - Remove a track from the queue.\n` + `\`/leave\` - Remove bot from voice channel and clear queue.\n` + `\`/queue\` - Show the list of tracks added to the queue.\n` + - `\`/volume\` \`[percentage]\` - Show or set the playback volume for tracks.\n` + `\`/volume\` \`[percentage]\` - Show or set the playback volume for tracks.\n` + + `\`/filters\` - Toggle various audio filters during playback.\n` ) .setColor(embedColors.colorInfo) ] diff --git a/config.json.example b/config.json.example index 63260c0..91935a6 100644 --- a/config.json.example +++ b/config.json.example @@ -9,5 +9,56 @@ "colorError": "#F23F43", "colorInfo": "#5865F2", "colorNote": "#80848E" - } + }, + "filterThreadAmount": "4", + "filterList": [ + { + "label": "Bass boost", + "value": "bassboost_low", + "description": "Boost the bass of the audio.", + "emoji": "🔉" + }, + { + "label": "Bass boost high", + "value": "bassboost", + "description": "Boost the bass of the audio a lot.", + "emoji": "🔊" + }, + { + "label": "Night core", + "value": "nightcore", + "description": "Speed up the audio (higher pitch).", + "emoji": "🐱" + }, + { + "label": "Lo-fi", + "value": "lofi", + "description": "Low fidelity effect (lower quality).", + "emoji": "📻" + }, + { + "label": "Vaporwave", + "value": "vaporwave", + "description": "Slow down the audio (lower pitch).", + "emoji": "🌸" + }, + { + "label": "Ear rape", + "value": "earrape", + "description": "Extremely loud and distorted audio.", + "emoji": "👂" + }, + { + "label": "8D", + "value": "8D", + "description": "Simulate 8D audio effect (surround).", + "emoji": "🎧" + }, + { + "label": "Treble", + "value": "treble", + "description": "Increase the high frequencies.", + "emoji": "🎼" + } + ] }