2022-06-07 03:08:27 +07:00
|
|
|
import {
|
|
|
|
|
Message,
|
|
|
|
|
CommandInteraction,
|
|
|
|
|
VoiceChannel,
|
2022-06-29 15:27:42 +07:00
|
|
|
ActionRowBuilder,
|
|
|
|
|
ButtonBuilder,
|
2022-06-07 03:08:27 +07:00
|
|
|
SelectMenuInteraction,
|
2022-06-28 15:19:05 +07:00
|
|
|
ButtonInteraction,
|
2022-06-29 15:27:42 +07:00
|
|
|
StageChannel,
|
2022-07-15 15:56:15 +07:00
|
|
|
ButtonStyle,
|
|
|
|
|
PermissionsBitField
|
2022-06-07 03:08:27 +07:00
|
|
|
} from 'discord.js';
|
|
|
|
|
import { I18n } from 'i18n';
|
2022-03-03 20:56:49 +07:00
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
import { joinVoiceChannelProcedure } from './Join';
|
|
|
|
|
|
|
|
|
|
import DiscordProvider from '../../providers/Discord';
|
2024-06-16 00:34:31 +07:00
|
|
|
import DiscordMusicPlayer, { TrackUtils, ValidTracks } from '../../providers/DiscordMusicPlayerTempFix';
|
2022-06-07 03:08:27 +07:00
|
|
|
import Locale from '../../services/Locale';
|
|
|
|
|
|
|
|
|
|
import DiscordModule, { HybridInteractionMessage } from '../../utils/DiscordModule';
|
|
|
|
|
import {
|
|
|
|
|
makeErrorEmbed,
|
|
|
|
|
makeSuccessEmbed,
|
|
|
|
|
sendHybridInteractionMessageResponse,
|
|
|
|
|
makeInfoEmbed
|
|
|
|
|
} from '../../utils/DiscordMessage';
|
2022-07-15 15:56:15 +07:00
|
|
|
import { checkBotPermissionsInChannel } from '../../utils/DiscordPermission';
|
2022-01-30 20:10:44 +07:00
|
|
|
|
|
|
|
|
const EMBEDS = {
|
2022-06-07 03:08:27 +07:00
|
|
|
PLAY_INFO: (data: HybridInteractionMessage, locale: I18n) => {
|
2022-02-27 21:07:08 +07:00
|
|
|
return makeInfoEmbed({
|
2022-06-07 03:08:27 +07:00
|
|
|
title: locale.__('musicplayer_play.title'),
|
|
|
|
|
description: locale.__('musicplayer_play.info'),
|
2022-02-27 19:42:05 +07:00
|
|
|
fields: [
|
|
|
|
|
{
|
2022-06-07 03:08:27 +07:00
|
|
|
name: locale.__('common.available_args'),
|
2022-07-01 22:04:54 +07:00
|
|
|
value: locale.__('musicplayer_play.valid_args')
|
2022-02-27 19:42:05 +07:00
|
|
|
}
|
|
|
|
|
],
|
2022-06-07 03:08:27 +07:00
|
|
|
user: data.getUser()
|
2022-02-27 19:42:05 +07:00
|
|
|
});
|
|
|
|
|
},
|
2022-07-01 22:04:54 +07:00
|
|
|
ADDED_QUEUE: async (data: HybridInteractionMessage, locale: I18n, track: ValidTracks) => {
|
|
|
|
|
const title = TrackUtils.getTitle(track);
|
|
|
|
|
const thumbnails = await TrackUtils.getThumbnails(track);
|
|
|
|
|
|
2022-01-30 20:10:44 +07:00
|
|
|
let embed = makeSuccessEmbed({
|
2022-06-07 03:08:27 +07:00
|
|
|
title: locale.__('musicplayer_play.queue_added_song'),
|
2022-07-01 22:04:54 +07:00
|
|
|
description: title,
|
2022-06-07 03:08:27 +07:00
|
|
|
user: data.getUser()
|
2022-01-30 20:10:44 +07:00
|
|
|
});
|
2022-03-04 00:29:50 +07:00
|
|
|
|
2022-07-01 22:04:54 +07:00
|
|
|
if (TrackUtils.getHighestResolutionThumbnail(thumbnails))
|
|
|
|
|
embed.setImage(TrackUtils.getHighestResolutionThumbnail(thumbnails).url);
|
2022-03-04 00:29:50 +07:00
|
|
|
|
2022-01-30 20:10:44 +07:00
|
|
|
return embed;
|
2022-01-30 22:42:00 +07:00
|
|
|
},
|
2022-06-07 03:08:27 +07:00
|
|
|
ADDED_SONGS_QUEUE: (data: HybridInteractionMessage, locale: I18n, track: ValidTracks[]) => {
|
2022-02-27 21:07:08 +07:00
|
|
|
let embed = makeSuccessEmbed({
|
2022-06-07 03:08:27 +07:00
|
|
|
title: locale.__('musicplayer_play.queue_added_songs'),
|
2022-06-27 20:28:01 +07:00
|
|
|
description: locale.__('musicplayer_play.queue_added_songs_description', {
|
|
|
|
|
COUNT: track.length.toString()
|
|
|
|
|
}),
|
2022-06-07 03:08:27 +07:00
|
|
|
user: data.getUser()
|
2022-02-27 21:07:08 +07:00
|
|
|
});
|
|
|
|
|
return embed;
|
|
|
|
|
},
|
2022-06-07 03:08:27 +07:00
|
|
|
USER_NOT_IN_VOICECHANNEL: (data: HybridInteractionMessage, locale: I18n) => {
|
2022-01-30 22:42:00 +07:00
|
|
|
return makeErrorEmbed({
|
2022-06-07 03:08:27 +07:00
|
|
|
title: locale.__('musicplayer.not_in_voice'),
|
|
|
|
|
user: data.getUser()
|
2022-01-30 22:42:00 +07:00
|
|
|
});
|
|
|
|
|
},
|
2022-06-07 03:08:27 +07:00
|
|
|
USER_NOT_IN_SAME_VOICECHANNEL: (data: HybridInteractionMessage, locale: I18n) => {
|
2022-01-30 22:42:00 +07:00
|
|
|
return makeErrorEmbed({
|
2022-06-07 03:08:27 +07:00
|
|
|
title: locale.__('musicplayer.different_voice_channel'),
|
|
|
|
|
user: data.getUser()
|
2022-01-30 22:42:00 +07:00
|
|
|
});
|
|
|
|
|
},
|
2022-06-07 03:08:27 +07:00
|
|
|
LOOKUP_ERROR: (data: HybridInteractionMessage, locale: I18n, error: Error) => {
|
|
|
|
|
let errorMessage = '';
|
2022-03-03 22:37:41 +07:00
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
if (error.message === 'While getting info from url\nPrivate video') errorMessage = 'This is a private video';
|
2022-03-03 22:37:41 +07:00
|
|
|
else
|
2022-06-07 03:08:27 +07:00
|
|
|
errorMessage = error.message
|
|
|
|
|
.replace('While getting info from url\n', '')
|
|
|
|
|
.replace('While getting info from url', '');
|
2022-03-03 22:37:41 +07:00
|
|
|
|
|
|
|
|
return makeErrorEmbed({
|
2022-06-07 03:08:27 +07:00
|
|
|
title: locale.__('musicplayer_play.error'),
|
|
|
|
|
description: errorMessage,
|
|
|
|
|
user: data.getUser()
|
2022-03-03 22:37:41 +07:00
|
|
|
});
|
2024-06-16 00:34:31 +07:00
|
|
|
},
|
|
|
|
|
TOO_LONG: (data: HybridInteractionMessage, locale: I18n) => {
|
|
|
|
|
return makeErrorEmbed({
|
|
|
|
|
title: locale.__('musicplayer_play.too_long'),
|
|
|
|
|
user: data.getUser()
|
|
|
|
|
});
|
2022-06-07 03:08:27 +07:00
|
|
|
}
|
|
|
|
|
};
|
2022-03-03 20:56:49 +07:00
|
|
|
export default class Play extends DiscordModule {
|
2022-06-07 03:08:27 +07:00
|
|
|
public id = 'Discord_MusicPlayer_Play';
|
|
|
|
|
public commands = ['play', 'p'];
|
|
|
|
|
public commandInteractionName = 'play';
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
async GuildOnModuleCommand(args: any, message: Message) {
|
|
|
|
|
await this.run(new HybridInteractionMessage(message), args);
|
2022-01-30 20:10:44 +07:00
|
|
|
}
|
|
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) {
|
|
|
|
|
await this.run(new HybridInteractionMessage(interaction), interaction.options);
|
|
|
|
|
}
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
async GuildButtonInteractionCreate(interaction: ButtonInteraction) {
|
|
|
|
|
const hybridData = new HybridInteractionMessage(interaction);
|
|
|
|
|
const guild = hybridData.getGuild();
|
|
|
|
|
const member = hybridData.getMember();
|
|
|
|
|
const user = hybridData.getUser();
|
2022-02-27 21:07:08 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (!guild || !member || !user) return;
|
|
|
|
|
if (!this.isJSONValid(interaction.customId)) return;
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
const locale = await Locale.getGuildLocale(guild.id);
|
|
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
let payload = JSON.parse(interaction.customId);
|
2022-02-27 17:15:20 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (typeof payload.m === 'undefined' || typeof payload.a === 'undefined') return;
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
/*
|
|
|
|
|
Discord have 100 char custom id char limit
|
|
|
|
|
So we need to shorten our json.
|
|
|
|
|
MP_P stands for MusicPlayer_Play
|
|
|
|
|
data.v stands for data.videoId
|
|
|
|
|
data.l stands for data.listId
|
|
|
|
|
*/
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (payload.m === 'MP_P' && payload.a === 'arp') {
|
|
|
|
|
if (typeof payload.d === 'undefined') return;
|
2022-01-30 22:42:00 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
let data = payload.d.split('$');
|
|
|
|
|
if (data.length !== 3) return;
|
2022-01-30 22:42:00 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
payload.d = {
|
|
|
|
|
c: data[0],
|
|
|
|
|
v: data[1],
|
|
|
|
|
l: data[2]
|
2022-06-07 03:08:27 +07:00
|
|
|
};
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
await interaction.deferReply();
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
let voiceChannel = DiscordProvider.client.guilds.cache.get(guild.id)?.channels.cache.get(payload.d.c);
|
|
|
|
|
//let member = DiscordProvider.client.guilds.cache.get(guild.id)?.members.cache.get(interaction.member?.user?.id);
|
2022-01-30 22:42:00 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (!voiceChannel || !(voiceChannel instanceof VoiceChannel)) return;
|
2022-02-27 21:07:08 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (!member.voice.channel)
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(
|
|
|
|
|
hybridData,
|
|
|
|
|
{ embeds: [EMBEDS.USER_NOT_IN_VOICECHANNEL(hybridData, locale)] },
|
|
|
|
|
true
|
|
|
|
|
);
|
2022-02-27 21:07:08 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (!DiscordMusicPlayer.isGuildInstanceExists(guild.id))
|
2022-06-07 03:08:27 +07:00
|
|
|
await joinVoiceChannelProcedure(new HybridInteractionMessage(interaction), null, voiceChannel);
|
2022-02-27 21:07:08 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
let instance = DiscordMusicPlayer.getGuildInstance(guild.id);
|
2022-07-01 22:04:54 +07:00
|
|
|
if (!instance) return;
|
2022-02-27 21:07:08 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (instance!.voiceChannel.id !== member.voice.channel.id)
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(
|
|
|
|
|
hybridData,
|
|
|
|
|
{ embeds: [EMBEDS.USER_NOT_IN_SAME_VOICECHANNEL(hybridData, locale)] },
|
|
|
|
|
true
|
|
|
|
|
);
|
2022-02-27 21:07:08 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (!instance!.isConnected()) {
|
2022-06-06 21:33:02 +07:00
|
|
|
await joinVoiceChannelProcedure(new HybridInteractionMessage(interaction), instance!, voiceChannel);
|
2022-03-03 20:56:49 +07:00
|
|
|
instance = DiscordMusicPlayer.getGuildInstance(guild.id);
|
2022-01-30 20:10:44 +07:00
|
|
|
}
|
2022-03-03 20:56:49 +07:00
|
|
|
|
|
|
|
|
if (!instance) return;
|
|
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
let playlist = await DiscordMusicPlayer.getYouTubeSongsInPlayList(
|
|
|
|
|
`https://www.youtube.com/watch?v=${payload.d.v}&list=${payload.d.l}`
|
|
|
|
|
);
|
2022-03-03 20:56:49 +07:00
|
|
|
const songs = (await playlist.all_videos()).filter((song) => {
|
|
|
|
|
return payload.d.v !== song.id;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (let song of songs) {
|
|
|
|
|
instance.addTrackToQueue(song);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 16:43:55 +07:00
|
|
|
if (hybridData.getStringSelectMenu().message instanceof Message)
|
|
|
|
|
await (hybridData.getStringSelectMenu().message as Message).edit({ components: [] });
|
2022-03-03 20:56:49 +07:00
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(
|
|
|
|
|
hybridData,
|
|
|
|
|
{ embeds: [EMBEDS.ADDED_SONGS_QUEUE(hybridData, locale, songs)] },
|
|
|
|
|
true
|
|
|
|
|
);
|
2022-01-30 20:10:44 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
async GuildSelectMenuInteractionCreate(interaction: SelectMenuInteraction) {
|
|
|
|
|
const hybridData = new HybridInteractionMessage(interaction);
|
|
|
|
|
const guild = hybridData.getGuild();
|
|
|
|
|
const member = hybridData.getMember();
|
|
|
|
|
const user = hybridData.getUser();
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (!guild || !member || !user) return;
|
|
|
|
|
if (!this.isJSONValid(interaction.customId)) return;
|
|
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
const locale = await Locale.getGuildLocale(guild.id);
|
2022-03-03 20:56:49 +07:00
|
|
|
let payload = JSON.parse(interaction.customId);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
if (typeof payload.m === 'undefined' || typeof payload.a === 'undefined') return;
|
|
|
|
|
|
|
|
|
|
if (payload.m === 'MP_SM' && payload.a === 'play') {
|
|
|
|
|
await interaction.deferReply();
|
|
|
|
|
|
|
|
|
|
const voiceChannel = DiscordProvider.client.guilds.cache.get(guild.id)?.channels.cache.get(payload.d.v);
|
|
|
|
|
//let member = DiscordProvider.client.guilds.cache.get(guild.id)?.members.cache.get(user.id);
|
|
|
|
|
|
2022-07-01 22:04:54 +07:00
|
|
|
if (!voiceChannel || !(voiceChannel instanceof VoiceChannel || voiceChannel instanceof StageChannel))
|
|
|
|
|
return;
|
2022-03-03 20:56:49 +07:00
|
|
|
|
|
|
|
|
if (!member.voice.channel)
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(
|
|
|
|
|
hybridData,
|
|
|
|
|
{ embeds: [EMBEDS.USER_NOT_IN_VOICECHANNEL(new HybridInteractionMessage(interaction), locale)] },
|
|
|
|
|
true
|
|
|
|
|
);
|
2022-03-03 20:56:49 +07:00
|
|
|
|
|
|
|
|
if (!DiscordMusicPlayer.isGuildInstanceExists(guild.id))
|
2022-06-06 21:33:02 +07:00
|
|
|
await joinVoiceChannelProcedure(new HybridInteractionMessage(interaction), null, voiceChannel);
|
2022-03-03 20:56:49 +07:00
|
|
|
|
|
|
|
|
let instance = DiscordMusicPlayer.getGuildInstance(guild.id);
|
2022-07-01 22:04:54 +07:00
|
|
|
if (!instance) return;
|
2022-03-03 20:56:49 +07:00
|
|
|
|
|
|
|
|
if (instance!.voiceChannel.id !== member.voice.channel.id)
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(
|
|
|
|
|
hybridData,
|
2022-07-01 22:04:54 +07:00
|
|
|
{
|
|
|
|
|
embeds: [
|
|
|
|
|
EMBEDS.USER_NOT_IN_SAME_VOICECHANNEL(new HybridInteractionMessage(interaction), locale)
|
|
|
|
|
]
|
|
|
|
|
},
|
2022-06-07 03:08:27 +07:00
|
|
|
true
|
|
|
|
|
);
|
2022-03-03 20:56:49 +07:00
|
|
|
|
|
|
|
|
if (!instance!.isConnected()) {
|
2022-06-06 21:33:02 +07:00
|
|
|
await joinVoiceChannelProcedure(new HybridInteractionMessage(interaction), instance!, voiceChannel);
|
2022-03-03 20:56:49 +07:00
|
|
|
instance = DiscordMusicPlayer.getGuildInstance(guild.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!instance) return;
|
|
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
let result = await DiscordMusicPlayer.searchYouTubeByYouTubeLink(
|
|
|
|
|
DiscordMusicPlayer.parseYouTubeLink(interaction.values[0])
|
|
|
|
|
).catch((err) => {
|
|
|
|
|
sendHybridInteractionMessageResponse(
|
|
|
|
|
hybridData,
|
|
|
|
|
{ embeds: [EMBEDS.LOOKUP_ERROR(hybridData, locale, err)] },
|
|
|
|
|
true
|
|
|
|
|
);
|
2022-03-03 22:37:41 +07:00
|
|
|
return null;
|
|
|
|
|
});
|
2022-03-03 20:56:49 +07:00
|
|
|
if (!result) return;
|
|
|
|
|
|
|
|
|
|
instance.addTrackToQueue(result);
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(
|
|
|
|
|
hybridData,
|
2022-07-01 22:04:54 +07:00
|
|
|
{ embeds: [await EMBEDS.ADDED_QUEUE(new HybridInteractionMessage(interaction), locale, result)] },
|
2022-06-07 03:08:27 +07:00
|
|
|
true
|
|
|
|
|
);
|
2022-03-03 20:56:49 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async run(data: HybridInteractionMessage, args: any) {
|
2022-01-30 20:10:44 +07:00
|
|
|
let query;
|
2022-03-03 20:56:49 +07:00
|
|
|
const guild = data.getGuild();
|
2022-07-15 15:56:15 +07:00
|
|
|
const channel = data.getGuildChannel();
|
2022-03-03 20:56:49 +07:00
|
|
|
const member = data.getMember();
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
if (!guild || !channel || !member) return;
|
|
|
|
|
|
|
|
|
|
const locale = await Locale.getGuildLocale(guild.id);
|
|
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (data.isMessage()) {
|
|
|
|
|
if (args.length === 0)
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.PLAY_INFO(data, locale)] });
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-02-27 17:47:13 +07:00
|
|
|
query = args.join(' ');
|
2022-06-29 15:27:42 +07:00
|
|
|
} else if (data.isApplicationCommand()) {
|
|
|
|
|
query = data.getSlashCommand().options.get('query', true).value?.toString();
|
2022-01-30 20:10:44 +07:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
if (!query) return;
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (!member.voice.channel)
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(data, {
|
|
|
|
|
embeds: [EMBEDS.USER_NOT_IN_VOICECHANNEL(data, locale)]
|
|
|
|
|
});
|
2022-01-30 22:42:00 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
const voiceChannel = member.voice.channel;
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (!DiscordMusicPlayer.isGuildInstanceExists(guild.id)) {
|
2022-06-06 21:33:02 +07:00
|
|
|
await joinVoiceChannelProcedure(data, null, voiceChannel);
|
2022-01-30 20:10:44 +07:00
|
|
|
}
|
|
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
let instance = DiscordMusicPlayer.getGuildInstance(guild.id);
|
2022-07-01 22:04:54 +07:00
|
|
|
if (!instance) return;
|
2022-01-30 20:10:44 +07:00
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
if (instance!.voiceChannel.id !== member.voice.channel.id)
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(
|
|
|
|
|
data,
|
|
|
|
|
{ embeds: [EMBEDS.USER_NOT_IN_SAME_VOICECHANNEL(data, locale)] },
|
|
|
|
|
true
|
|
|
|
|
);
|
2022-01-30 22:42:00 +07:00
|
|
|
|
2022-01-30 20:10:44 +07:00
|
|
|
if (!instance!.isConnected()) {
|
2022-06-06 21:33:02 +07:00
|
|
|
await joinVoiceChannelProcedure(data, instance!, voiceChannel);
|
2022-03-03 20:56:49 +07:00
|
|
|
instance = DiscordMusicPlayer.getGuildInstance(guild.id);
|
2022-01-30 20:10:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!instance) return;
|
|
|
|
|
|
|
|
|
|
if (DiscordMusicPlayer.isYouTubeLink(query)) {
|
2022-02-27 21:07:08 +07:00
|
|
|
let linkData = DiscordMusicPlayer.parseYouTubeLink(query);
|
2022-02-27 21:40:59 +07:00
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
if (linkData.videoId === '' && linkData.list) {
|
2022-02-27 21:40:59 +07:00
|
|
|
let playlist = await DiscordMusicPlayer.getYouTubeSongsInPlayList(query);
|
|
|
|
|
const songs = await playlist.all_videos();
|
|
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
for (let song of songs) {
|
2022-02-27 21:40:59 +07:00
|
|
|
instance.addTrackToQueue(song);
|
|
|
|
|
}
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(
|
|
|
|
|
data,
|
|
|
|
|
{ embeds: [EMBEDS.ADDED_SONGS_QUEUE(data, locale, songs)] },
|
|
|
|
|
true
|
|
|
|
|
);
|
2022-02-27 21:40:59 +07:00
|
|
|
}
|
|
|
|
|
|
2022-03-03 22:37:41 +07:00
|
|
|
let result = await DiscordMusicPlayer.searchYouTubeByYouTubeLink(linkData).catch((err) => {
|
2022-06-07 03:08:27 +07:00
|
|
|
sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.LOOKUP_ERROR(data, locale, err)] }, true);
|
2022-03-03 22:37:41 +07:00
|
|
|
return null;
|
|
|
|
|
});
|
2022-01-30 20:10:44 +07:00
|
|
|
|
|
|
|
|
if (!result) return;
|
|
|
|
|
|
2023-03-05 14:46:25 +07:00
|
|
|
if (
|
|
|
|
|
data.isMessage() &&
|
|
|
|
|
data.getMessage().embeds.length > 0 &&
|
|
|
|
|
(await checkBotPermissionsInChannel({
|
|
|
|
|
guild,
|
|
|
|
|
channel,
|
|
|
|
|
permissions: [PermissionsBitField.Flags.ManageMessages]
|
|
|
|
|
}))
|
|
|
|
|
) {
|
2022-03-03 20:56:49 +07:00
|
|
|
data.getMessage().suppressEmbeds(true);
|
2022-01-30 20:10:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
instance.addTrackToQueue(result);
|
2022-02-27 21:07:08 +07:00
|
|
|
|
|
|
|
|
if (linkData.list) {
|
2022-07-15 15:41:29 +07:00
|
|
|
const row = new ActionRowBuilder<ButtonBuilder>().addComponents([
|
2022-06-29 15:27:42 +07:00
|
|
|
new ButtonBuilder()
|
2022-06-07 03:08:27 +07:00
|
|
|
.setEmoji('✅')
|
|
|
|
|
.setCustomId(
|
|
|
|
|
JSON.stringify({
|
2022-02-27 21:07:08 +07:00
|
|
|
m: 'MP_P',
|
|
|
|
|
a: 'arp', // Add remaining playlist
|
2022-02-27 21:16:23 +07:00
|
|
|
d: `${voiceChannel.id}$${linkData.videoId}$${linkData.list}`
|
2022-06-07 03:08:27 +07:00
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.setLabel(' Add the remaining songs in the playlist')
|
2022-06-29 15:27:42 +07:00
|
|
|
.setStyle(ButtonStyle.Primary)
|
2023-03-05 14:46:25 +07:00
|
|
|
]);
|
2022-02-27 21:16:23 +07:00
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(data, {
|
2022-07-01 22:04:54 +07:00
|
|
|
embeds: [await EMBEDS.ADDED_QUEUE(data, locale, result)],
|
2022-06-07 03:08:27 +07:00
|
|
|
components: [row]
|
|
|
|
|
});
|
|
|
|
|
} else
|
|
|
|
|
return await sendHybridInteractionMessageResponse(data, {
|
2022-07-01 22:04:54 +07:00
|
|
|
embeds: [await EMBEDS.ADDED_QUEUE(data, locale, result)]
|
2022-06-07 03:08:27 +07:00
|
|
|
});
|
2022-07-01 22:04:54 +07:00
|
|
|
} else if (DiscordMusicPlayer.isSpotifyLink(query)) {
|
|
|
|
|
let linkData = DiscordMusicPlayer.parseSpotifyLink(query);
|
|
|
|
|
|
|
|
|
|
if (linkData.type === 'playlist' || linkData.type === 'album') {
|
2022-07-15 14:57:40 +07:00
|
|
|
let playlist = await DiscordMusicPlayer.getSpotifySongsInPlayList(query).catch((err) => {
|
2023-03-05 14:46:25 +07:00
|
|
|
sendHybridInteractionMessageResponse(
|
|
|
|
|
data,
|
|
|
|
|
{ embeds: [EMBEDS.LOOKUP_ERROR(data, locale, err)] },
|
|
|
|
|
true
|
|
|
|
|
);
|
2022-07-15 14:57:40 +07:00
|
|
|
return null;
|
|
|
|
|
});
|
2023-03-05 14:46:25 +07:00
|
|
|
|
|
|
|
|
if (!playlist) return;
|
2022-07-01 22:04:54 +07:00
|
|
|
const songs = await playlist.all_tracks();
|
|
|
|
|
|
|
|
|
|
for (const song of songs) {
|
|
|
|
|
instance.addTrackToQueue(song);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await sendHybridInteractionMessageResponse(
|
|
|
|
|
data,
|
|
|
|
|
{ embeds: [EMBEDS.ADDED_SONGS_QUEUE(data, locale, songs)] },
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let result = await DiscordMusicPlayer.searchSpotifyBySpotifyLink(linkData).catch((err) => {
|
|
|
|
|
sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.LOOKUP_ERROR(data, locale, err)] }, true);
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!result) return;
|
|
|
|
|
|
2023-03-05 14:46:25 +07:00
|
|
|
if (
|
|
|
|
|
data.isMessage() &&
|
|
|
|
|
data.getMessage().embeds.length > 0 &&
|
|
|
|
|
(await checkBotPermissionsInChannel({
|
|
|
|
|
guild,
|
|
|
|
|
channel,
|
|
|
|
|
permissions: [PermissionsBitField.Flags.ManageMessages]
|
|
|
|
|
}))
|
|
|
|
|
) {
|
2022-07-01 22:04:54 +07:00
|
|
|
data.getMessage().suppressEmbeds(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
instance.addTrackToQueue(result);
|
|
|
|
|
|
|
|
|
|
return await sendHybridInteractionMessageResponse(data, {
|
|
|
|
|
embeds: [await EMBEDS.ADDED_QUEUE(data, locale, result)]
|
|
|
|
|
});
|
2022-01-30 20:10:44 +07:00
|
|
|
} else {
|
|
|
|
|
let result = await DiscordMusicPlayer.searchYouTubeByQuery(query);
|
|
|
|
|
|
2022-07-12 12:30:04 +07:00
|
|
|
if (!result) {
|
|
|
|
|
// TODO: Send not found embed
|
|
|
|
|
await sendHybridInteractionMessageResponse(
|
|
|
|
|
data,
|
|
|
|
|
{ embeds: [EMBEDS.LOOKUP_ERROR(data, locale, new Error('Cannot find that song'))] },
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-30 20:10:44 +07:00
|
|
|
instance.addTrackToQueue(result[0]);
|
|
|
|
|
|
2024-06-16 00:34:31 +07:00
|
|
|
// Max 1 hour
|
|
|
|
|
if (result[0].durationInSec > 3600)
|
|
|
|
|
return await sendHybridInteractionMessageResponse(data, {
|
|
|
|
|
embeds: [EMBEDS.TOO_LONG(data, locale)]
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-27 21:07:08 +07:00
|
|
|
if (result.length > 1) {
|
2022-02-27 20:06:14 +07:00
|
|
|
// TODO: Find a better logic than this
|
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
|
2022-02-27 20:06:14 +07:00
|
|
|
// 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;
|
2022-02-27 19:21:50 +07:00
|
|
|
|
|
|
|
|
// The query length is too long to fit in json
|
2022-02-27 21:07:08 +07:00
|
|
|
if (query.length > 100 - 51) return;
|
2022-02-27 19:21:50 +07:00
|
|
|
|
2022-07-15 15:41:29 +07:00
|
|
|
const row = new ActionRowBuilder<ButtonBuilder>().addComponents([
|
2022-06-29 15:27:42 +07:00
|
|
|
new ButtonBuilder()
|
2022-06-07 03:08:27 +07:00
|
|
|
.setEmoji('🔎')
|
|
|
|
|
.setCustomId(
|
|
|
|
|
JSON.stringify({
|
2022-02-27 21:07:08 +07:00
|
|
|
m: 'MP_S',
|
|
|
|
|
a: 'search',
|
|
|
|
|
d: {
|
|
|
|
|
q: query
|
|
|
|
|
}
|
2022-06-07 03:08:27 +07:00
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.setLabel(' Not this? Search!')
|
2022-06-29 15:27:42 +07:00
|
|
|
.setStyle(ButtonStyle.Primary)
|
2023-03-05 14:46:25 +07:00
|
|
|
]);
|
2022-02-27 19:21:50 +07:00
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(data, {
|
2022-07-01 22:04:54 +07:00
|
|
|
embeds: [await EMBEDS.ADDED_QUEUE(data, locale, result[0])],
|
2022-06-07 03:08:27 +07:00
|
|
|
components: [row]
|
|
|
|
|
});
|
2022-02-27 17:47:13 +07:00
|
|
|
}
|
2022-02-27 21:07:08 +07:00
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
return await sendHybridInteractionMessageResponse(data, {
|
2022-07-01 22:04:54 +07:00
|
|
|
embeds: [await EMBEDS.ADDED_QUEUE(data, locale, result[0])]
|
2022-06-07 03:08:27 +07:00
|
|
|
});
|
2022-01-30 20:10:44 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
isJSONValid(jsonString: string) {
|
2022-01-30 20:10:44 +07:00
|
|
|
try {
|
|
|
|
|
let o = JSON.parse(jsonString);
|
2022-06-07 03:08:27 +07:00
|
|
|
if (o && typeof o === 'object') {
|
2022-01-30 20:10:44 +07:00
|
|
|
return true;
|
|
|
|
|
}
|
2022-06-07 03:08:27 +07:00
|
|
|
} catch (e) {}
|
2022-01-30 20:10:44 +07:00
|
|
|
return false;
|
|
|
|
|
}
|
2022-06-07 03:08:27 +07:00
|
|
|
}
|