mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-04 14:38:49 +00:00
fix: Added player query validation, will transform country-based Spotify URLs
This commit is contained in:
1 parent
2445337ccc
commit
40204420fa
2 files changed
+29
-3
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
const { embedOptions, playerOptions, botOptions } = require('../../config');
|
const { embedOptions, playerOptions, botOptions } = require('../../config');
|
||||||
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
|
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
|
||||||
|
const { transformQuery } = require('../../utils/validation/searchQueryValidation');
|
||||||
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
|
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
|
||||||
const { useMainPlayer, useQueue } = require('discord-player');
|
const { useMainPlayer, useQueue } = require('discord-player');
|
||||||
|
|
||||||
@@ -17,7 +18,9 @@ module.exports = {
|
|||||||
const player = useMainPlayer();
|
const player = useMainPlayer();
|
||||||
const query = interaction.options.getString('query');
|
const query = interaction.options.getString('query');
|
||||||
|
|
||||||
const searchResult = await player.search(query, {
|
const transformedQuery = await transformQuery(query);
|
||||||
|
|
||||||
|
const searchResult = await player.search(transformedQuery, {
|
||||||
requestedBy: interaction.user
|
requestedBy: interaction.user
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -26,7 +29,7 @@ module.exports = {
|
|||||||
embeds: [
|
embeds: [
|
||||||
new EmbedBuilder()
|
new EmbedBuilder()
|
||||||
.setDescription(
|
.setDescription(
|
||||||
`**${embedOptions.icons.warning} No track found**\nNo results found for \`${query}\`.\n\nIf you specified a URL, please make sure it is valid and public.`
|
`**${embedOptions.icons.warning} No track found**\nNo results found for \`${transformedQuery}\`.\n\nIf you specified a URL, please make sure it is valid and public.`
|
||||||
)
|
)
|
||||||
.setColor(embedOptions.colors.warning)
|
.setColor(embedOptions.colors.warning)
|
||||||
]
|
]
|
||||||
@@ -151,7 +154,7 @@ module.exports = {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (queue.currentTrack === track) {
|
if (queue.currentTrack === track && queue.tracks.data.length === 0) {
|
||||||
return await interaction.editReply({
|
return await interaction.editReply({
|
||||||
embeds: [
|
embeds: [
|
||||||
new EmbedBuilder()
|
new EmbedBuilder()
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
const logger = require('../../services/logger');
|
||||||
|
|
||||||
|
exports.transformQuery = async (query) => {
|
||||||
|
if (query.includes('open.spotify.com')) {
|
||||||
|
try {
|
||||||
|
// regex to check for country-based Spotify url
|
||||||
|
const regex = new RegExp(
|
||||||
|
'https://open.spotify.com/((?:[a-z]{2,4}-[a-z]{2,4}/)|(?:[a-z]{2,4}/))?track/([a-zA-Z0-9]+)'
|
||||||
|
);
|
||||||
|
const isCountryBased = regex.test(query);
|
||||||
|
|
||||||
|
// if country-based, transform to normal Spotify url
|
||||||
|
if (isCountryBased) {
|
||||||
|
const trackId = query.match(regex)[2];
|
||||||
|
query = `https://open.spotify.com/track/${trackId}`;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return query;
|
||||||
|
};
|
||||||
Reference in new issue
Block a user