Skip to content

Commit

Permalink
Remove 'autoComplete' event, pack all autocomplete function into comm…
Browse files Browse the repository at this point in the history
…and file
  • Loading branch information
RainyXeon committed Dec 21, 2023
1 parent aacbcd0 commit 16d4424
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 136 deletions.
33 changes: 15 additions & 18 deletions src/commands/prefix/Music/Play.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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;
}
}
83 changes: 68 additions & 15 deletions src/commands/slash/Music/Play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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(() => {});
}
}
56 changes: 56 additions & 0 deletions src/commands/slash/Playlist/Add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];

Expand Down Expand Up @@ -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(() => {});
}
}
97 changes: 0 additions & 97 deletions src/events/guild/autoComplete.ts

This file was deleted.

16 changes: 12 additions & 4 deletions src/events/guild/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]}`,
Expand Down Expand Up @@ -255,7 +264,6 @@ export default class {
});
}

if (!command) return;
if (command) {
try {
command.run(interaction, client, language);
Expand Down
Loading

0 comments on commit 16d4424

Please sign in to comment.