mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-05 06:28:52 +00:00
feat: better autocomplete by caching and debounce
This commit is contained in:
1 parent
68a8274b44
commit
a9a918392a
2 files changed
+39
-14
No files matched your search
@@ -7,6 +7,8 @@ const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
|
|||||||
const { useMainPlayer, useQueue, QueryType } = require('discord-player');
|
const { useMainPlayer, useQueue, QueryType } = require('discord-player');
|
||||||
const { lyricsExtractor } = require('@discord-player/extractor');
|
const { lyricsExtractor } = require('@discord-player/extractor');
|
||||||
|
|
||||||
|
const recentQueries = new Map();
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
isNew: false,
|
isNew: false,
|
||||||
isBeta: false,
|
isBeta: false,
|
||||||
@@ -26,12 +28,27 @@ module.exports = {
|
|||||||
),
|
),
|
||||||
autocomplete: async ({ interaction }) => {
|
autocomplete: async ({ interaction }) => {
|
||||||
const query = interaction.options.getString('query', true);
|
const query = interaction.options.getString('query', true);
|
||||||
if (query.length < 2) {
|
|
||||||
return;
|
const { lastQuery, result, timestamp } = recentQueries.get(interaction.user.id) || {};
|
||||||
|
|
||||||
|
if (lastQuery && (query.startsWith(lastQuery) || lastQuery.startsWith(query)) && Date.now() - timestamp < 500) {
|
||||||
|
logger.debug(
|
||||||
|
{ action: 'autocomplete_responded' },
|
||||||
|
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Autocomplete search responded with results from lastQuery for query: '${query}'`
|
||||||
|
);
|
||||||
|
return interaction.respond(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (query.length < 3) {
|
||||||
|
logger.debug(
|
||||||
|
{ action: 'autocomplete_responded' },
|
||||||
|
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Autocomplete search responded with empty results due to < 3 length for query '${query}'`
|
||||||
|
);
|
||||||
|
return interaction.respond([]);
|
||||||
|
}
|
||||||
|
|
||||||
const genius = lyricsExtractor();
|
const genius = lyricsExtractor();
|
||||||
const lyricsResult = await genius.search(query).catch(() => null);
|
const lyricsResult = await genius.search(query).catch(() => null);
|
||||||
|
|
||||||
let response = [];
|
let response = [];
|
||||||
|
|
||||||
if (!lyricsResult) {
|
if (!lyricsResult) {
|
||||||
@@ -41,7 +58,7 @@ module.exports = {
|
|||||||
name:
|
name:
|
||||||
`${track.title} [Artist: ${track.author}]`.length > 100
|
`${track.title} [Artist: ${track.author}]`.length > 100
|
||||||
? `${track.title}`.slice(0, 100)
|
? `${track.title}`.slice(0, 100)
|
||||||
: `${track.title} [Author: ${track.author}]`,
|
: `${track.title} [Artist: ${track.author}]`,
|
||||||
value: track.title.slice(0, 100)
|
value: track.title.slice(0, 100)
|
||||||
}));
|
}));
|
||||||
} else {
|
} else {
|
||||||
@@ -57,7 +74,16 @@ module.exports = {
|
|||||||
return interaction.respond([]);
|
return interaction.respond([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug(`[Shard ${interaction.guild.shardId}] Autocomplete search responded for query: ${query}`);
|
recentQueries.set(interaction.user.id, {
|
||||||
|
lastQuery: query,
|
||||||
|
result: response,
|
||||||
|
timestamp: Date.now()
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
{ action: 'autocomplete_responded' },
|
||||||
|
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Autocomplete search responded for query: '${query}'`
|
||||||
|
);
|
||||||
return interaction.respond(response);
|
return interaction.respond(response);
|
||||||
},
|
},
|
||||||
execute: async ({ interaction }) => {
|
execute: async ({ interaction }) => {
|
||||||
|
|||||||
@@ -40,12 +40,12 @@ module.exports = {
|
|||||||
|
|
||||||
const { lastQuery, results, timestamp } = recentQueries.get(interaction.user.id) || {};
|
const { lastQuery, results, timestamp } = recentQueries.get(interaction.user.id) || {};
|
||||||
|
|
||||||
if (lastQuery && query.startsWith(lastQuery) && Date.now() - timestamp < 500) {
|
if (lastQuery && (query.startsWith(lastQuery) || lastQuery.startsWith(query)) && Date.now() - timestamp < 500) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
{ action: 'autocomplete_responded' },
|
{ action: 'autocomplete_responded' },
|
||||||
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Autocomplete search responded with results from lastQuery for query: '${query}'`
|
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Autocomplete search responded with results from lastQuery for query: '${query}'`
|
||||||
);
|
);
|
||||||
return results;
|
return interaction.respond(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (query.length < 3) {
|
if (query.length < 3) {
|
||||||
@@ -57,13 +57,6 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
const searchResults = await player.search(query);
|
const searchResults = await player.search(query);
|
||||||
|
|
||||||
// Store the query and current timestamp for this user.
|
|
||||||
recentQueries.set(interaction.user.id, {
|
|
||||||
lastQuery: query,
|
|
||||||
results: searchResults.tracks.slice(0, 5),
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
|
|
||||||
let response = [];
|
let response = [];
|
||||||
|
|
||||||
response = searchResults.tracks.slice(0, 5).map((track) => {
|
response = searchResults.tracks.slice(0, 5).map((track) => {
|
||||||
@@ -79,6 +72,12 @@ module.exports = {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
recentQueries.set(interaction.user.id, {
|
||||||
|
lastQuery: query,
|
||||||
|
results: response,
|
||||||
|
timestamp: Date.now()
|
||||||
|
});
|
||||||
|
|
||||||
if (!response || response.length === 0) {
|
if (!response || response.length === 0) {
|
||||||
return interaction.respond([]);
|
return interaction.respond([]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user