2022-02-27 19:21:50 +07:00
|
|
|
import { Message, CommandInteraction, Interaction, VoiceChannel, MessageActionRow, Permissions, MessageButton, TextChannel } from "discord.js";
|
2022-01-30 20:10:44 +07:00
|
|
|
import { makeErrorEmbed, makeSuccessEmbed, makeProcessingEmbed, sendMessage, sendMessageOrInteractionResponse, makeInfoEmbed } from "../../utils/DiscordMessage";
|
|
|
|
|
import DiscordProvider from "../../providers/Discord";
|
|
|
|
|
import Environment from "../../providers/Environment";
|
|
|
|
|
import DiscordMusicPlayer, { ValidTracks } from "../../providers/DiscordMusicPlayer";
|
|
|
|
|
import { joinVoiceChannelProcedure } from "./Join";
|
|
|
|
|
|
|
|
|
|
const EMBEDS = {
|
2022-02-27 19:42:05 +07:00
|
|
|
PLAY_INFO: (data: Message | Interaction) => {
|
|
|
|
|
return makeInfoEmbed ({
|
|
|
|
|
title: 'Play',
|
|
|
|
|
description: `Play a song`,
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
name: 'Arguments',
|
|
|
|
|
value: '``Search query or Link``'
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
user: (data instanceof Interaction) ? data.user : data.author
|
|
|
|
|
});
|
|
|
|
|
},
|
2022-01-30 20:10:44 +07:00
|
|
|
YOUTUBE_ONLY: (data: Message | Interaction) => {
|
|
|
|
|
return makeErrorEmbed({
|
|
|
|
|
title: `Only youtube.com link is supported for now`,
|
|
|
|
|
user: (data instanceof Interaction) ? data.user : data.author
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
INVALID_LINK: (data: Message | Interaction) => {
|
|
|
|
|
return makeErrorEmbed({
|
|
|
|
|
title: `Invalid Link`,
|
|
|
|
|
user: (data instanceof Interaction) ? data.user : data.author
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
ADDED_QUEUE: (data: Message | Interaction, track: ValidTracks) => {
|
|
|
|
|
let embed = makeSuccessEmbed({
|
|
|
|
|
title: `Song added to queue!`,
|
|
|
|
|
description: `${track.title}`,
|
|
|
|
|
user: (data instanceof Interaction) ? data.user : data.author
|
|
|
|
|
});
|
|
|
|
|
embed.setImage(track.thumbnails[0].url);
|
|
|
|
|
return embed;
|
2022-01-30 22:42:00 +07:00
|
|
|
},
|
|
|
|
|
USER_NOT_IN_VOICECHANNEL: (data: Message | Interaction) => {
|
|
|
|
|
return makeErrorEmbed({
|
|
|
|
|
title: `You need to be in the voice channel first!`,
|
|
|
|
|
user: (data instanceof Interaction) ? data.user : data.author
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
USER_NOT_IN_SAME_VOICECHANNEL: (data: Message | Interaction) => {
|
|
|
|
|
return makeErrorEmbed({
|
|
|
|
|
title: `You are not in the same voice channel!`,
|
|
|
|
|
user: (data instanceof Interaction) ? data.user : data.author
|
|
|
|
|
});
|
|
|
|
|
},
|
2022-01-30 20:10:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default class Play {
|
|
|
|
|
|
|
|
|
|
async onCommand(command: string, args: any, message: Message) {
|
|
|
|
|
if (command.toLowerCase() !== 'play' && command.toLowerCase() !== 'p') return;
|
|
|
|
|
await this.process(message, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async interactionCreate(interaction: Interaction) {
|
|
|
|
|
if (interaction.isCommand()) {
|
|
|
|
|
if (typeof interaction.commandName === 'undefined') return;
|
|
|
|
|
if ((interaction.commandName).toLowerCase() !== 'play') return;
|
|
|
|
|
await this.process(interaction, interaction.options);
|
|
|
|
|
}
|
|
|
|
|
else if (interaction.isSelectMenu()) {
|
|
|
|
|
if(!interaction.guild || !interaction.guildId) return;
|
|
|
|
|
if (!this.tryParseJSONObject(interaction.customId)) return;
|
|
|
|
|
|
|
|
|
|
let payload = JSON.parse(interaction.customId);
|
2022-01-30 22:42:00 +07:00
|
|
|
if(!interaction.member?.user?.id) return;
|
2022-01-30 20:10:44 +07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Discord have 100 char custom id char limit
|
|
|
|
|
So we need to shorten our json.
|
|
|
|
|
MP_SM stands for MusicPlayer_SearchMenu
|
|
|
|
|
data.r stands for data.requester
|
|
|
|
|
data.v stands for data.voiceChannel
|
|
|
|
|
*/
|
2022-02-27 17:15:20 +07:00
|
|
|
|
2022-01-30 20:10:44 +07:00
|
|
|
if (typeof payload.module === 'undefined' ||
|
|
|
|
|
typeof payload.action === 'undefined' ||
|
|
|
|
|
payload.module !== 'MP_SM' ||
|
|
|
|
|
payload.action !== 'play') return;
|
|
|
|
|
|
2022-01-30 22:42:00 +07:00
|
|
|
await interaction.deferReply();
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-01-30 22:42:00 +07:00
|
|
|
let voiceChannel = DiscordProvider.client.guilds.cache.get(interaction.guildId)?.channels.cache.get(payload.data.v);
|
|
|
|
|
let member = DiscordProvider.client.guilds.cache.get(interaction.guildId)?.members.cache.get(interaction.member?.user?.id);
|
2022-01-30 20:10:44 +07:00
|
|
|
|
|
|
|
|
if(!member) return;
|
|
|
|
|
if (!voiceChannel || !(voiceChannel instanceof VoiceChannel)) return;
|
2022-01-30 22:42:00 +07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!member.voice.channel)
|
|
|
|
|
return await sendMessageOrInteractionResponse(interaction, { embeds: [EMBEDS.USER_NOT_IN_VOICECHANNEL(interaction)] }, true);
|
2022-01-30 20:10:44 +07:00
|
|
|
|
|
|
|
|
if (!DiscordMusicPlayer.isGuildInstanceExists(interaction.guildId)) {
|
|
|
|
|
await joinVoiceChannelProcedure(interaction, null, voiceChannel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let instance = DiscordMusicPlayer.getGuildInstance(interaction.guildId);
|
|
|
|
|
|
2022-01-30 22:42:00 +07:00
|
|
|
if(instance!.voiceChannel.id !== member.voice.channel.id)
|
|
|
|
|
return await sendMessageOrInteractionResponse(interaction, { embeds: [EMBEDS.USER_NOT_IN_SAME_VOICECHANNEL(interaction)] }, true);
|
|
|
|
|
|
2022-01-30 20:10:44 +07:00
|
|
|
if (!instance!.isConnected()) {
|
|
|
|
|
await joinVoiceChannelProcedure(interaction, instance!, voiceChannel);
|
|
|
|
|
instance = DiscordMusicPlayer.getGuildInstance(interaction.guildId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!instance) return;
|
|
|
|
|
|
|
|
|
|
let result = await DiscordMusicPlayer.searchYouTubeByYouTubeLink(DiscordMusicPlayer.parseYouTubeLink(interaction.values[0]));
|
|
|
|
|
if (!result) return;
|
|
|
|
|
|
|
|
|
|
instance.addTrackToQueue(result);
|
|
|
|
|
//interaction.editReply({ embeds: [EMBEDS.ADDED_QUEUE(interaction, result)] });
|
|
|
|
|
return await sendMessageOrInteractionResponse(interaction, { embeds: [EMBEDS.ADDED_QUEUE(interaction, result)] }, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async process(data: Interaction | Message, args: any) {
|
|
|
|
|
const isSlashCommand = data instanceof CommandInteraction && data.isCommand();
|
|
|
|
|
const isMessage = data instanceof Message;
|
|
|
|
|
|
|
|
|
|
if (!isSlashCommand && !isMessage) return;
|
|
|
|
|
|
|
|
|
|
if (!data.member) return;
|
|
|
|
|
if (!data.guild) return;
|
|
|
|
|
if (!data.guildId) return;
|
|
|
|
|
|
|
|
|
|
let query;
|
|
|
|
|
|
|
|
|
|
if (isMessage) {
|
|
|
|
|
if (data === null || !data.guildId || data.member === null || data.guild === null) return;
|
|
|
|
|
|
|
|
|
|
if (args.length === 0) {
|
2022-02-27 19:42:05 +07:00
|
|
|
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.PLAY_INFO(data)] });
|
2022-01-30 20:10:44 +07:00
|
|
|
}
|
2022-02-27 17:47:13 +07:00
|
|
|
query = args.join(' ');
|
2022-01-30 20:10:44 +07:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (isSlashCommand) {
|
|
|
|
|
query = args.getSubcommand();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!query) return;
|
|
|
|
|
if (!(data.channel instanceof TextChannel)) return;
|
|
|
|
|
|
2022-01-30 22:42:00 +07:00
|
|
|
if (!data.member.voice.channel)
|
|
|
|
|
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.USER_NOT_IN_VOICECHANNEL(data)] });
|
|
|
|
|
|
2022-01-30 20:10:44 +07:00
|
|
|
let voiceChannel = data.member.voice.channel;
|
|
|
|
|
|
|
|
|
|
if (!DiscordMusicPlayer.isGuildInstanceExists(data.guildId)) {
|
|
|
|
|
await joinVoiceChannelProcedure(data, null, voiceChannel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let instance = DiscordMusicPlayer.getGuildInstance(data.guildId);
|
|
|
|
|
|
2022-01-30 22:42:00 +07:00
|
|
|
if(instance!.voiceChannel.id !== data.member.voice.channel.id)
|
|
|
|
|
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.USER_NOT_IN_SAME_VOICECHANNEL(data)] }, true);
|
|
|
|
|
|
2022-01-30 20:10:44 +07:00
|
|
|
if (!instance!.isConnected()) {
|
|
|
|
|
await joinVoiceChannelProcedure(data, instance!, voiceChannel);
|
|
|
|
|
instance = DiscordMusicPlayer.getGuildInstance(data.guildId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!instance) return;
|
|
|
|
|
|
|
|
|
|
if (DiscordMusicPlayer.isYouTubeLink(query)) {
|
|
|
|
|
let result = await DiscordMusicPlayer.searchYouTubeByYouTubeLink(DiscordMusicPlayer.parseYouTubeLink(query));
|
|
|
|
|
|
|
|
|
|
if (!result) return;
|
|
|
|
|
|
|
|
|
|
if (isMessage && (data as Message).embeds.length > 0) {
|
|
|
|
|
(data as Message).suppressEmbeds(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
instance.addTrackToQueue(result);
|
|
|
|
|
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.ADDED_QUEUE(data, result)] });
|
|
|
|
|
} else {
|
|
|
|
|
let result = await DiscordMusicPlayer.searchYouTubeByQuery(query);
|
|
|
|
|
|
|
|
|
|
if (!result) return;
|
|
|
|
|
instance.addTrackToQueue(result[0]);
|
|
|
|
|
|
2022-02-27 17:47:13 +07:00
|
|
|
if(result.length > 1) {
|
2022-02-27 19:21:50 +07:00
|
|
|
|
|
|
|
|
// If the first result title is exact match with the query or first title contains half the space of the query, it's probably a sentence
|
|
|
|
|
// then we don't need to show the search results
|
|
|
|
|
if(result[0].title === query || result[0].title && (Math.ceil((result[0].title.split(" ").length - 1) / 2) === Math.ceil((query.split(" ").length - 1) / 2))) return;
|
|
|
|
|
|
|
|
|
|
// The query length is too long to fit in json
|
|
|
|
|
if(query.length > 100 - 51) return;
|
|
|
|
|
|
|
|
|
|
const row = new MessageActionRow()
|
|
|
|
|
.addComponents(
|
|
|
|
|
new MessageButton()
|
|
|
|
|
.setEmoji('🔎')
|
|
|
|
|
.setCustomId(JSON.stringify({
|
|
|
|
|
module: 'MP_S',
|
|
|
|
|
action: 'search',
|
|
|
|
|
data: {
|
|
|
|
|
q: query
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
.setLabel(' Not this? Search!')
|
|
|
|
|
.setStyle('PRIMARY')
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.ADDED_QUEUE(data, result[0])], components: [row] });
|
2022-02-27 17:47:13 +07:00
|
|
|
}
|
|
|
|
|
|
2022-01-30 20:10:44 +07:00
|
|
|
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.ADDED_QUEUE(data, result[0])] });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tryParseJSONObject(jsonString: string) {
|
|
|
|
|
try {
|
|
|
|
|
let o = JSON.parse(jsonString);
|
|
|
|
|
if (o && typeof o === "object") {
|
|
|
|
|
return true;
|
|
|
|
|
//return o;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (e) { }
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|