mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-04 06:28:53 +00:00
feat!: Extract logic for autocomplete and component interactions
This commit is contained in:
1 parent
f16a18deb5
commit
1ec1281e98
29 files changed
+694
-442
No files matched your search
@@ -5,13 +5,12 @@ import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
|
||||
import { lyricsExtractor } from '@discord-player/extractor';
|
||||
|
||||
import loggerModule from '../../../services/logger';
|
||||
import { CommandAutocompleteParams, CommandParams } from '../../../types/commandTypes';
|
||||
import { CustomSlashCommandInteraction } from '../../../types/interactionTypes';
|
||||
import { EmbedOptions } from '../../../types/configTypes';
|
||||
import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator';
|
||||
import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
|
||||
|
||||
const embedOptions: EmbedOptions = config.get('embedOptions');
|
||||
const recentQueries = new Map();
|
||||
|
||||
const loggerTemplate = loggerModule.child({
|
||||
source: 'lyrics.js',
|
||||
@@ -19,7 +18,7 @@ const loggerTemplate = loggerModule.child({
|
||||
name: '/lyrics'
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
const command: CustomSlashCommandInteraction = {
|
||||
isNew: false,
|
||||
isBeta: false,
|
||||
data: new SlashCommandBuilder()
|
||||
@@ -36,67 +35,7 @@ module.exports = {
|
||||
.setMaxLength(500)
|
||||
.setAutocomplete(true)
|
||||
),
|
||||
autocomplete: async ({ interaction, executionId }: CommandAutocompleteParams) => {
|
||||
const logger = loggerTemplate.child({
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
guildId: interaction.guild?.id
|
||||
});
|
||||
|
||||
const query = interaction.options.getString('query', true);
|
||||
|
||||
const { lastQuery, result, timestamp } = recentQueries.get(interaction.user.id) || {};
|
||||
|
||||
if (lastQuery && (query.startsWith(lastQuery) || lastQuery.startsWith(query)) && Date.now() - timestamp < 500) {
|
||||
logger.debug(`Responding with results from lastQuery for query '${query}'`);
|
||||
return interaction.respond(result);
|
||||
}
|
||||
|
||||
if (query.length < 3) {
|
||||
logger.debug(`Responding with empty results due to < 3 length for query '${query}'`);
|
||||
return interaction.respond([]);
|
||||
}
|
||||
|
||||
const genius = lyricsExtractor();
|
||||
const lyricsResult = await genius.search(query).catch(() => null);
|
||||
let response = [];
|
||||
|
||||
if (!lyricsResult) {
|
||||
logger.debug(`No Genius lyrics found for query '${query}', using player.search() as fallback.`);
|
||||
const player = useMainPlayer()!;
|
||||
const searchResults = await player.search(query);
|
||||
response = searchResults.tracks.slice(0, 1).map((track) => ({
|
||||
name:
|
||||
`${track.title} [Artist: ${track.author}]`.length > 100
|
||||
? `${track.title}`.slice(0, 100)
|
||||
: `${track.title} [Artist: ${track.author}]`,
|
||||
value: track.title.slice(0, 100)
|
||||
}));
|
||||
} else {
|
||||
logger.debug(`Found Genius lyrics for query '${query}'.`);
|
||||
response = [
|
||||
{
|
||||
name: `${lyricsResult.title} [Artist: ${lyricsResult.artist.name}]`.slice(0, 100),
|
||||
value: lyricsResult.title.slice(0, 100)
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
if (!response || response.length === 0) {
|
||||
logger.debug(`Responding with empty results for query '${query}'`);
|
||||
return interaction.respond([]);
|
||||
}
|
||||
|
||||
recentQueries.set(interaction.user.id, {
|
||||
lastQuery: query,
|
||||
result: response,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
logger.debug(`Responding to autocomplete with results for query: '${query}'.`);
|
||||
return interaction.respond(response);
|
||||
},
|
||||
execute: async ({ interaction, executionId }: CommandParams) => {
|
||||
execute: async ({ interaction, executionId }) => {
|
||||
const logger = loggerTemplate.child({
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
@@ -267,3 +206,5 @@ module.exports = {
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export default command;
|
||||
Reference in new issue
Block a user