From 16d4424875956c3c5c1220104002f5bc76614bec Mon Sep 17 00:00:00 2001 From: RainyXeon / Date: Thu, 21 Dec 2023 11:27:09 +0700 Subject: [PATCH] Remove 'autoComplete' event, pack all autocomplete function into command file --- src/commands/prefix/Music/Play.ts | 33 +++++---- src/commands/slash/Music/Play.ts | 83 ++++++++++++++++++----- src/commands/slash/Playlist/Add.ts | 56 ++++++++++++++++ src/events/guild/autoComplete.ts | 97 --------------------------- src/events/guild/interactionCreate.ts | 16 +++-- src/manager.ts | 17 ++++- 6 files changed, 166 insertions(+), 136 deletions(-) delete mode 100644 src/events/guild/autoComplete.ts diff --git a/src/commands/prefix/Music/Play.ts b/src/commands/prefix/Music/Play.ts index 34c3ca35..d134c839 100644 --- a/src/commands/prefix/Music/Play.ts +++ b/src/commands/prefix/Music/Play.ts @@ -1,4 +1,4 @@ -import { EmbedBuilder, Message, PermissionsBitField } from "discord.js"; +import { EmbedBuilder, Message } from "discord.js"; import { ConvertTime } from "../../../structures/ConvertTime.js"; import { StartQueueDuration } from "../../../structures/QueueDuration.js"; import { Manager } from "../../../manager.js"; @@ -57,8 +57,17 @@ export default class implements PrefixCommand { textId: message.channel.id, deaf: true, }); - else if (player && !this.checkSameVoice(message, client, language, msg)) { - return; + else if (player && !this.checkSameVoice(message)) { + msg.edit({ + embeds: [ + new EmbedBuilder() + .setDescription( + `${client.i18n.get(language, "noplayer", "no_voice")}` + ) + .setColor(client.color), + ], + }); + return } const result = await player.search(value, { requester: message.author }); @@ -128,27 +137,15 @@ export default class implements PrefixCommand { } } - checkSameVoice( + private checkSameVoice( message: Message, - client: Manager, - language: string, - msg: Message ) { if ( message.member!.voice.channel !== message.guild!.members.me!.voice.channel ) { - msg.edit({ - embeds: [ - new EmbedBuilder() - .setDescription( - `${client.i18n.get(language, "noplayer", "no_voice")}` - ) - .setColor(client.color), - ], - }); return false; + } else { + return true; } - - return true; } } diff --git a/src/commands/slash/Music/Play.ts b/src/commands/slash/Music/Play.ts index 448c79d8..1879b7dc 100644 --- a/src/commands/slash/Music/Play.ts +++ b/src/commands/slash/Music/Play.ts @@ -6,11 +6,13 @@ import { CommandInteractionOptionResolver, GuildMember, Message, + AutocompleteInteraction, } from "discord.js"; import { ConvertTime } from "../../../structures/ConvertTime.js"; import { StartQueueDuration } from "../../../structures/QueueDuration.js"; import { Manager } from "../../../manager.js"; import { Accessableby, SlashCommand } from "../../../@types/Command.js"; +import { AutocompleteInteractionChoices, GlobalInteraction } from "../../../@types/Interaction.js"; export default class implements SlashCommand { name = ["play"]; @@ -82,8 +84,17 @@ export default class implements SlashCommand { }); } else if ( player && - !this.checkSameVoice(interaction, client, language, msg) + !this.checkSameVoice(interaction) ) { + msg.edit({ + embeds: [ + new EmbedBuilder() + .setDescription( + `${client.i18n.get(language, "noplayer", "no_voice")}` + ) + .setColor(client.color), + ], + }); return; } @@ -166,28 +177,70 @@ export default class implements SlashCommand { } catch (e) {} } - checkSameVoice( + private checkSameVoice( interaction: CommandInteraction, - client: Manager, - language: string, - msg: Message ) { if ( (interaction.member as GuildMember)!.voice.channel !== interaction.guild!.members.me!.voice.channel ) { - msg.edit({ - embeds: [ - new EmbedBuilder() - .setDescription( - `${client.i18n.get(language, "noplayer", "no_voice")}` - ) - .setColor(client.color), - ], - }); return false; + } else { + return true; + } + } + + // Autocomplete function + async autocomplete( + client: Manager, + interaction: GlobalInteraction, + language: string, + ) { + let choice: AutocompleteInteractionChoices[] = []; + const url = String((interaction as CommandInteraction).options.get( + "search" + )!.value); + + const Random = + client.config.lavalink.DEFAULT[ + Math.floor(Math.random() * client.config.lavalink.DEFAULT.length) + ]; + + const match = client.REGEX.some((match) => { + return match.test(url) == true; + }); + + if (match == true) { + choice.push({ name: url, value: url }); + await (interaction as AutocompleteInteraction) + .respond(choice) + .catch(() => {}); + return; + } + + if (client.lavalink_using.length == 0) { + choice.push({ + name: `${client.i18n.get(language, "music", "no_node")}`, + value: `${client.i18n.get(language, "music", "no_node")}`, + }); + return; + } + const searchRes = await client.manager.search(url || Random); + + if (searchRes.tracks.length == 0 || !searchRes.tracks) { + return choice.push({ name: "Error song not matches", value: url }); + } + + for (let i = 0; i < 10; i++) { + const x = searchRes.tracks[i]; + choice.push({ + name: x.title ? x.title : "Unknown track name", + value: x.uri ? x.uri : url, + }); } - return true; + await (interaction as AutocompleteInteraction) + .respond(choice) + .catch(() => {}); } } diff --git a/src/commands/slash/Playlist/Add.ts b/src/commands/slash/Playlist/Add.ts index c5a6736a..652d6d41 100644 --- a/src/commands/slash/Playlist/Add.ts +++ b/src/commands/slash/Playlist/Add.ts @@ -3,12 +3,14 @@ import { CommandInteraction, ApplicationCommandOptionType, CommandInteractionOptionResolver, + AutocompleteInteraction, } from "discord.js"; import { ConvertTime } from "../../../structures/ConvertTime.js"; import { StartQueueDuration } from "../../../structures/QueueDuration.js"; import { KazagumoTrack } from "better-kazagumo"; import { Manager } from "../../../manager.js"; import { Accessableby, SlashCommand } from "../../../@types/Command.js"; +import { AutocompleteInteractionChoices, GlobalInteraction } from "../../../@types/Interaction.js"; const TrackAdd: KazagumoTrack[] = []; @@ -206,4 +208,58 @@ export default class implements SlashCommand { } } catch (e) {} } + + // Autocomplete function + async autocomplete( + client: Manager, + interaction: GlobalInteraction, + language: string, + ) { + let choice: AutocompleteInteractionChoices[] = []; + const url = String((interaction as CommandInteraction).options.get( + "search" + )!.value); + + const Random = + client.config.lavalink.DEFAULT[ + Math.floor(Math.random() * client.config.lavalink.DEFAULT.length) + ]; + + const match = client.REGEX.some((match) => { + return match.test(url) == true; + }); + + if (match == true) { + choice.push({ name: url, value: url }); + await (interaction as AutocompleteInteraction) + .respond(choice) + .catch(() => {}); + return; + } + + if (client.lavalink_using.length == 0) { + choice.push({ + name: `${client.i18n.get(language, "music", "no_node")}`, + value: `${client.i18n.get(language, "music", "no_node")}`, + }); + return; + } + const searchRes = await client.manager.search(url || Random); + + if (searchRes.tracks.length == 0 || !searchRes.tracks) { + return choice.push({ name: "Error song not matches", value: url }); + } + + for (let i = 0; i < 10; i++) { + const x = searchRes.tracks[i]; + choice.push({ + name: x.title ? x.title : "Unknown track name", + value: x.uri ? x.uri : url, + }); + } + + await (interaction as AutocompleteInteraction) + .respond(choice) + .catch(() => {}); + } } diff --git a/src/events/guild/autoComplete.ts b/src/events/guild/autoComplete.ts deleted file mode 100644 index b07d0061..00000000 --- a/src/events/guild/autoComplete.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { Manager } from "../../manager.js"; -import { - InteractionType, - CommandInteraction, - AutocompleteInteraction, -} from "discord.js"; -import { - AutocompleteInteractionChoices, - GlobalInteraction, -} from "../../@types/Interaction.js"; -import { SlashCommand } from "../../@types/Command.js"; - -const REGEX = [ - /(?:https?:\/\/)?(?:www\.)?youtu(?:\.be\/|be.com\/\S*(?:watch|embed)(?:(?:(?=\/[-a-zA-Z0-9_]{11,}(?!\S))\/)|(?:\S*v=|v\/)))([-a-zA-Z0-9_]{11,})/, - /^.*(youtu.be\/|list=)([^#\&\?]*).*/, - /^(?:spotify:|https:\/\/[a-z]+\.spotify\.com\/(track\/|user\/(.*)\/playlist\/|playlist\/))(.*)$/, - /^https?:\/\/(?:www\.)?deezer\.com\/[a-z]+\/(track|album|playlist)\/(\d+)$/, - /^(?:(https?):\/\/)?(?:(?:www|m)\.)?(soundcloud\.com|snd\.sc)\/(.*)$/, - /(?:https:\/\/music\.apple\.com\/)(?:.+)?(artist|album|music-video|playlist)\/([\w\-\.]+(\/)+[\w\-\.]+|[^&]+)\/([\w\-\.]+(\/)+[\w\-\.]+|[^&]+)/, - /^https?:\/\/(?:www\.|secure\.|sp\.)?nicovideo\.jp\/watch\/([a-z]{2}[0-9]+)/, -]; - -export default class { - async execute( - client: Manager, - interaction: GlobalInteraction, - language: string, - command: SlashCommand - ) { - // Push Function - async function AutoCompletePush( - url: string, - choice: AutocompleteInteractionChoices[] - ) { - const Random = - client.config.lavalink.DEFAULT[ - Math.floor(Math.random() * client.config.lavalink.DEFAULT.length) - ]; - - const match = REGEX.some((match) => { - return match.test(url) == true; - }); - - if (match == true) { - choice.push({ name: url, value: url }); - await (interaction as AutocompleteInteraction) - .respond(choice) - .catch(() => {}); - return; - } - - if (client.lavalink_using.length == 0) { - choice.push({ - name: `${client.i18n.get(language, "music", "no_node")}`, - value: `${client.i18n.get(language, "music", "no_node")}`, - }); - return; - } - const searchRes = await client.manager.search(url || Random); - - if (searchRes.tracks.length == 0 || !searchRes.tracks) { - return choice.push({ name: "Error song not matches", value: url }); - } - - for (let i = 0; i < 10; i++) { - const x = searchRes.tracks[i]; - choice.push({ - name: x.title ? x.title : "Unknown track name", - value: x.uri ? x.uri : url, - }); - } - - await (interaction as AutocompleteInteraction) - .respond(choice) - .catch(() => {}); - } - - if ( - (interaction as CommandInteraction).commandName == "play" || - (interaction as CommandInteraction).commandName + command!.name[1] == - "playlist" + "add" - ) { - let choice: AutocompleteInteractionChoices[] = []; - const url = (interaction as CommandInteraction).options.get( - "search" - )!.value; - return AutoCompletePush(url as string, choice); - } else if ( - (interaction as CommandInteraction).commandName + command!.name[1] == - "playlist" + "edit" - ) { - let choice: AutocompleteInteractionChoices[] = []; - const url = (interaction as CommandInteraction).options.get("add")!.value; - return AutoCompletePush(url as string, choice); - } - } -} diff --git a/src/events/guild/interactionCreate.ts b/src/events/guild/interactionCreate.ts index e30bf8ba..d496a22f 100644 --- a/src/events/guild/interactionCreate.ts +++ b/src/events/guild/interactionCreate.ts @@ -86,9 +86,18 @@ export default class { if ( Number(interaction.type) == - InteractionType.ApplicationCommandAutocomplete - ) - return client.emit("autoComplete", interaction, language, command); + InteractionType.ApplicationCommandAutocomplete && (command as any).autocomplete !== undefined + ) { + try { + (command as any).autocomplete(client, interaction, language); + } catch (error) { + client.logger.log({ + level: "error", + message: error, + }); + } + return + } const msg_cmd = [ `[COMMAND] ${command.name[0]}`, @@ -255,7 +264,6 @@ export default class { }); } - if (!command) return; if (command) { try { command.run(interaction, client, language); diff --git a/src/manager.ts b/src/manager.ts index 22af833c..397e99aa 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -37,10 +37,19 @@ import { DeployService } from "./services/DeployService.js"; config(); const __dirname = dirname(fileURLToPath(import.meta.url)); - const loggerService = new LoggerService().init(); const configData = new ConfigDataService().data; +const REGEX = [ + /(?:https?:\/\/)?(?:www\.)?youtu(?:\.be\/|be.com\/\S*(?:watch|embed)(?:(?:(?=\/[-a-zA-Z0-9_]{11,}(?!\S))\/)|(?:\S*v=|v\/)))([-a-zA-Z0-9_]{11,})/, + /^.*(youtu.be\/|list=)([^#\&\?]*).*/, + /^(?:spotify:|https:\/\/[a-z]+\.spotify\.com\/(track\/|user\/(.*)\/playlist\/|playlist\/))(.*)$/, + /^https?:\/\/(?:www\.)?deezer\.com\/[a-z]+\/(track|album|playlist)\/(\d+)$/, + /^(?:(https?):\/\/)?(?:(?:www|m)\.)?(soundcloud\.com|snd\.sc)\/(.*)$/, + /(?:https:\/\/music\.apple\.com\/)(?:.+)?(artist|album|music-video|playlist)\/([\w\-\.]+(\/)+[\w\-\.]+|[^&]+)\/([\w\-\.]+(\/)+[\w\-\.]+|[^&]+)/, + /^https?:\/\/(?:www\.|secure\.|sp\.)?nicovideo\.jp\/watch\/([a-z]{2}[0-9]+)/, +]; + loggerService.info("Booting client..."); export class Manager extends Client { @@ -78,6 +87,7 @@ export class Manager extends Client { enSwitchMod!: ActionRowBuilder; icons: IconType; cluster?: ClusterClient; + REGEX: RegExp[]; // Main class constructor() { @@ -103,6 +113,8 @@ export class Manager extends Client { GatewayIntentBits.GuildMessages, ], }); + + // Initial basic bot config this.logger = loggerService; this.config = configData; this.metadata = new ManifestService().data.metadata.bot; @@ -116,8 +128,9 @@ export class Manager extends Client { }); this.prefix = this.config.features.MESSAGE_CONTENT.commands.prefix || "d!"; this.shard_status = false; + this.REGEX = REGEX - // Auto fix lavalink varibles + // Initial autofix lavalink varibles this.lavalink_list = []; this.lavalink_using = []; this.fixing_nodes = false;