2022-06-06 21:40:35 +07:00
|
|
|
|
import {
|
|
|
|
|
|
Message,
|
|
|
|
|
|
CommandInteraction,
|
|
|
|
|
|
Interaction,
|
|
|
|
|
|
VoiceChannel,
|
|
|
|
|
|
Permissions,
|
|
|
|
|
|
GuildMember,
|
|
|
|
|
|
DMChannel,
|
|
|
|
|
|
StageChannel,
|
|
|
|
|
|
MessageActionRow,
|
|
|
|
|
|
MessageButton,
|
|
|
|
|
|
TextChannel,
|
|
|
|
|
|
VoiceBasedChannel
|
|
|
|
|
|
} from 'discord.js';
|
2022-06-07 03:08:27 +07:00
|
|
|
|
import { I18n } from 'i18n';
|
|
|
|
|
|
|
2022-06-06 21:40:35 +07:00
|
|
|
|
import {
|
|
|
|
|
|
makeSuccessEmbed,
|
|
|
|
|
|
makeErrorEmbed,
|
|
|
|
|
|
sendMessage,
|
|
|
|
|
|
sendHybridInteractionMessageResponse,
|
|
|
|
|
|
makeInfoEmbed
|
|
|
|
|
|
} from '../../utils/DiscordMessage';
|
|
|
|
|
|
import DiscordProvider from '../../providers/Discord';
|
|
|
|
|
|
import DiscordMusicPlayer, {
|
|
|
|
|
|
PlayerPlayingEvent,
|
|
|
|
|
|
PlayerErrorEvent,
|
|
|
|
|
|
VoiceDisconnectedEvent,
|
|
|
|
|
|
ValidTracks,
|
|
|
|
|
|
DiscordMusicPlayerInstance,
|
|
|
|
|
|
DiscordMusicPlayerLoopMode
|
|
|
|
|
|
} from '../../providers/DiscordMusicPlayer';
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
|
import DiscordModule, { HybridInteractionMessage } from '../../utils/DiscordModule';
|
|
|
|
|
|
import Locale from '../../services/Locale';
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
|
const EMBEDS = {
|
|
|
|
|
|
VOICECHANNEL_JOINED: (data: HybridInteractionMessage, locale: I18n) => {
|
|
|
|
|
|
return makeSuccessEmbed({
|
|
|
|
|
|
title: locale.__('musicplayer_join.success'),
|
|
|
|
|
|
user: data.getUser()
|
|
|
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
VOICECHANNEL_DISCONNECTED: (data: HybridInteractionMessage, locale: I18n) => {
|
|
|
|
|
|
return makeSuccessEmbed({
|
|
|
|
|
|
title: locale.__('musicplayer_join.disconnected'),
|
|
|
|
|
|
description: locale.__('musicplayer_join.disconnected_reason_disconnected'),
|
|
|
|
|
|
user: data.getUser()
|
|
|
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
VOICECHANNEL_INUSE: (data: HybridInteractionMessage, locale: I18n, haveForceMove: boolean) => {
|
2022-01-30 20:10:52 +07:00
|
|
|
|
return makeErrorEmbed({
|
2022-06-07 03:08:27 +07:00
|
|
|
|
title: locale.__('musicplayer_join.error'),
|
|
|
|
|
|
description: `${locale.__('musicplayer_join.in_use')}\n\n${locale.__('musicplayer_join.in_use_wait')}${
|
|
|
|
|
|
haveForceMove ? locale.__('musicplayer_join.can_force_move') : ''
|
2022-06-06 21:40:35 +07:00
|
|
|
|
}`,
|
2022-06-07 03:08:27 +07:00
|
|
|
|
user: data.getUser()
|
2022-01-30 20:10:52 +07:00
|
|
|
|
});
|
|
|
|
|
|
},
|
2022-06-07 03:08:27 +07:00
|
|
|
|
VOICECHANNEL_ALREADY_JOINED: (data: HybridInteractionMessage, locale: I18n) => {
|
2022-01-30 20:10:52 +07:00
|
|
|
|
return makeInfoEmbed({
|
2022-06-07 03:08:27 +07:00
|
|
|
|
title: locale.__('musicplayer_join.already_joined_title'),
|
|
|
|
|
|
description: locale.__('musicplayer_join.already_joined_description'),
|
|
|
|
|
|
user: data.getUser()
|
2022-01-30 20:10:52 +07:00
|
|
|
|
});
|
|
|
|
|
|
},
|
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
|
|
|
|
MUSIC_ERROR: (data: HybridInteractionMessage, locale: I18n, error: Error) => {
|
2022-02-27 17:15:20 +07:00
|
|
|
|
return makeErrorEmbed({
|
2022-06-07 03:08:27 +07:00
|
|
|
|
title: locale.__('musicplayer.track_error'),
|
|
|
|
|
|
description: error.message,
|
|
|
|
|
|
user: data.getUser()
|
2022-02-27 17:15:20 +07:00
|
|
|
|
});
|
|
|
|
|
|
},
|
2022-06-07 03:08:27 +07:00
|
|
|
|
NOW_PLAYING: (data: HybridInteractionMessage, locale: I18n, track: ValidTracks) => {
|
2022-01-30 20:10:52 +07:00
|
|
|
|
const embed = makeInfoEmbed({
|
2022-06-07 03:08:27 +07:00
|
|
|
|
title: locale.__('musicplayer.now_playing'),
|
|
|
|
|
|
icon: '🎵 ',
|
|
|
|
|
|
description: track.title,
|
2022-01-30 20:10:52 +07:00
|
|
|
|
user: DiscordProvider.client.user
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-06-06 21:40:35 +07:00
|
|
|
|
const highestResolutionThumbnail = track.thumbnails.reduce((prev, current) =>
|
|
|
|
|
|
prev.height * prev.width > current.height * current.width ? prev : current
|
|
|
|
|
|
);
|
2022-03-04 00:29:50 +07:00
|
|
|
|
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (highestResolutionThumbnail) embed.setImage(highestResolutionThumbnail.url);
|
2022-05-01 16:56:37 +07:00
|
|
|
|
|
|
|
|
|
|
return embed;
|
|
|
|
|
|
},
|
2022-06-07 03:08:27 +07:00
|
|
|
|
NOW_REPEATING: (data: HybridInteractionMessage, locale: I18n, track: ValidTracks) => {
|
2022-05-01 16:56:37 +07:00
|
|
|
|
const embed = makeInfoEmbed({
|
2022-06-07 03:08:27 +07:00
|
|
|
|
title: locale.__('musicplayer.now_playing_repeating'),
|
|
|
|
|
|
icon: '🎵 ',
|
2022-05-01 16:56:37 +07:00
|
|
|
|
description: `${track.title}`,
|
|
|
|
|
|
user: DiscordProvider.client.user
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-06-06 21:40:35 +07:00
|
|
|
|
const highestResolutionThumbnail = track.thumbnails.reduce((prev, current) =>
|
|
|
|
|
|
prev.height * prev.width > current.height * current.width ? prev : current
|
|
|
|
|
|
);
|
2022-05-01 16:56:37 +07:00
|
|
|
|
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (highestResolutionThumbnail) embed.setImage(highestResolutionThumbnail.url);
|
2022-03-04 00:29:50 +07:00
|
|
|
|
|
2022-01-30 20:10:52 +07:00
|
|
|
|
return embed;
|
|
|
|
|
|
}
|
2022-06-06 21:40:35 +07:00
|
|
|
|
};
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-06-06 21:40:35 +07:00
|
|
|
|
export async function joinVoiceChannelProcedure(
|
|
|
|
|
|
data: HybridInteractionMessage,
|
|
|
|
|
|
instance: DiscordMusicPlayerInstance | null,
|
|
|
|
|
|
voiceChannel: VoiceChannel | StageChannel
|
|
|
|
|
|
) {
|
2022-06-06 21:33:02 +07:00
|
|
|
|
const isSlashCommand = data.isSlashCommand();
|
|
|
|
|
|
const isAcceptableInteraction = data.isSelectMenu() || data.isButton();
|
|
|
|
|
|
const isMessage = data.isMessage();
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (!isSlashCommand && !isAcceptableInteraction && !isMessage) return;
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-06-06 21:33:02 +07:00
|
|
|
|
const member = data.getMember();
|
|
|
|
|
|
const channel = data.getChannel();
|
|
|
|
|
|
const guild = data.getGuild();
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (!member || !channel || !guild) return;
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-06-06 21:33:02 +07:00
|
|
|
|
if (channel instanceof DMChannel) return;
|
|
|
|
|
|
if (!(channel instanceof TextChannel)) return;
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-06-06 21:40:35 +07:00
|
|
|
|
const memberVoiceChannel = member.voice.channel; //isMessage ? member.voice.channel : DiscordProvider.client.guilds.cache.get(guild.id)!.members.cache.get((data as Interaction).user.id)?.voice.channel;
|
2022-06-06 21:33:02 +07:00
|
|
|
|
if (!memberVoiceChannel) return;
|
|
|
|
|
|
|
|
|
|
|
|
const bot = guild.me;
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (!bot) return;
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-06-07 03:08:27 +07:00
|
|
|
|
const locale = await Locale.getGuildLocale(guild.id);
|
|
|
|
|
|
|
2022-01-30 20:10:52 +07:00
|
|
|
|
// If already in VoiceChannel
|
2022-06-06 21:33:02 +07:00
|
|
|
|
if (bot.voice.channelId) {
|
2022-01-30 20:10:52 +07:00
|
|
|
|
// But, no music instance yet (The bot might just restarted)
|
|
|
|
|
|
if (!instance) {
|
|
|
|
|
|
// User is in different VoiceChannel
|
2022-06-06 21:33:02 +07:00
|
|
|
|
if (memberVoiceChannel.id !== bot.voice.channelId) {
|
2022-01-30 20:10:52 +07:00
|
|
|
|
//Disconnect it
|
2022-06-06 21:33:02 +07:00
|
|
|
|
await bot.voice.disconnect();
|
2022-01-30 20:10:52 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Create instance for a new one
|
2022-06-06 21:33:02 +07:00
|
|
|
|
DiscordMusicPlayer.createGuildInstance(guild.id, voiceChannel);
|
|
|
|
|
|
instance = DiscordMusicPlayer.getGuildInstance(guild.id);
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-06-06 21:33:02 +07:00
|
|
|
|
instance!.joinVoiceChannel(voiceChannel, channel);
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (!isAcceptableInteraction)
|
|
|
|
|
|
await sendHybridInteractionMessageResponse(data, {
|
2022-06-07 03:08:27 +07:00
|
|
|
|
embeds: [EMBEDS.VOICECHANNEL_JOINED(data, locale)]
|
2022-06-06 21:40:35 +07:00
|
|
|
|
});
|
2022-01-30 20:10:52 +07:00
|
|
|
|
}
|
|
|
|
|
|
// And, already have instance on the guild
|
|
|
|
|
|
else {
|
|
|
|
|
|
// And, User VoiceChannel is same as the instance
|
|
|
|
|
|
if (channel.id === instance.voiceChannel.id) {
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (!isAcceptableInteraction)
|
|
|
|
|
|
return await sendHybridInteractionMessageResponse(data, {
|
2022-06-07 03:08:27 +07:00
|
|
|
|
embeds: [EMBEDS.VOICECHANNEL_ALREADY_JOINED(data, locale)]
|
2022-06-06 21:40:35 +07:00
|
|
|
|
});
|
2022-01-30 20:10:52 +07:00
|
|
|
|
else return;
|
|
|
|
|
|
}
|
|
|
|
|
|
// But, not the same VoiceChannel
|
|
|
|
|
|
else {
|
2022-06-06 21:40:35 +07:00
|
|
|
|
let activeMembers = instance.voiceChannel.members.filter((member) => member.user.bot === false);
|
2022-01-30 20:10:52 +07:00
|
|
|
|
// And someone else is using the bot
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (activeMembers.size > 0) {
|
|
|
|
|
|
if (!isAcceptableInteraction)
|
|
|
|
|
|
return await sendHybridInteractionMessageResponse(data, {
|
2022-06-07 03:08:27 +07:00
|
|
|
|
embeds: [
|
|
|
|
|
|
EMBEDS.VOICECHANNEL_INUSE(
|
|
|
|
|
|
data,
|
|
|
|
|
|
locale,
|
|
|
|
|
|
member.permissions.has([Permissions.FLAGS.MOVE_MEMBERS])
|
|
|
|
|
|
)
|
|
|
|
|
|
]
|
2022-06-06 21:40:35 +07:00
|
|
|
|
});
|
2022-01-30 20:10:52 +07:00
|
|
|
|
else return;
|
|
|
|
|
|
}
|
|
|
|
|
|
// And alone in the VoiceChannel
|
|
|
|
|
|
else {
|
|
|
|
|
|
// Move to the new voice channel
|
2022-06-06 21:33:02 +07:00
|
|
|
|
await bot.voice.setChannel(voiceChannel);
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (!isAcceptableInteraction)
|
|
|
|
|
|
return await sendHybridInteractionMessageResponse(data, {
|
2022-06-07 03:08:27 +07:00
|
|
|
|
embeds: [EMBEDS.VOICECHANNEL_JOINED(data, locale)]
|
2022-06-06 21:40:35 +07:00
|
|
|
|
});
|
2022-01-30 20:10:52 +07:00
|
|
|
|
else return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Not in any VoiceChannel
|
|
|
|
|
|
else {
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (instance) await DiscordMusicPlayer.destoryGuildInstance(guild.id);
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-06-06 21:33:02 +07:00
|
|
|
|
DiscordMusicPlayer.createGuildInstance(guild.id, voiceChannel);
|
|
|
|
|
|
instance = DiscordMusicPlayer.getGuildInstance(guild.id);
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-06-06 21:33:02 +07:00
|
|
|
|
instance!.joinVoiceChannel(voiceChannel, channel);
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (!isAcceptableInteraction)
|
2022-06-07 03:08:27 +07:00
|
|
|
|
await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.VOICECHANNEL_JOINED(data, locale)] });
|
2022-01-30 20:10:52 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!instance) return;
|
|
|
|
|
|
|
2022-05-01 16:56:37 +07:00
|
|
|
|
let previousTrack: ValidTracks | undefined;
|
|
|
|
|
|
let isLoopMessageSent = false;
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
|
|
|
|
|
// Register Event Listeners
|
|
|
|
|
|
instance.events.on('playing', async (event: PlayerPlayingEvent) => {
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (!event.instance.queue || !event.instance.queue.track || event.instance.queue.track.length === 0) return;
|
2022-05-01 16:56:37 +07:00
|
|
|
|
previousTrack = event.instance.getPreviousTrack();
|
|
|
|
|
|
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (event.instance.queue.track[0] !== previousTrack) isLoopMessageSent = false;
|
|
|
|
|
|
else if (event.instance.queue.track[0] === previousTrack && isLoopMessageSent) return;
|
2022-03-03 22:18:09 +07:00
|
|
|
|
|
2022-06-06 21:40:35 +07:00
|
|
|
|
const row = new MessageActionRow().addComponents(
|
|
|
|
|
|
new MessageButton()
|
|
|
|
|
|
.setEmoji('▶️')
|
|
|
|
|
|
.setLabel(' Open on YouTube')
|
|
|
|
|
|
.setURL(encodeURI(`https://www.youtube.com/watch?v=${event.instance.queue.track[0].id}`))
|
|
|
|
|
|
.setStyle('LINK')
|
|
|
|
|
|
);
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
|
|
|
|
|
if (event.instance.textChannel) {
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (event.instance.getLoopMode() === DiscordMusicPlayerLoopMode.Current) {
|
2022-05-01 16:56:37 +07:00
|
|
|
|
isLoopMessageSent = true;
|
2022-06-06 21:40:35 +07:00
|
|
|
|
await sendMessage(event.instance.textChannel, undefined, {
|
2022-06-07 03:08:27 +07:00
|
|
|
|
embeds: [EMBEDS.NOW_REPEATING(data, locale, event.instance.queue.track[0])],
|
2022-06-06 21:40:35 +07:00
|
|
|
|
components: [row]
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
await sendMessage(event.instance.textChannel, undefined, {
|
2022-06-07 03:08:27 +07:00
|
|
|
|
embeds: [EMBEDS.NOW_PLAYING(data, locale, event.instance.queue.track[0])],
|
2022-06-06 21:40:35 +07:00
|
|
|
|
components: [row]
|
|
|
|
|
|
});
|
2022-05-01 16:56:37 +07:00
|
|
|
|
}
|
2022-01-30 20:10:52 +07:00
|
|
|
|
}
|
2022-02-27 17:15:20 +07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
instance.events.on('error', async (event: PlayerErrorEvent) => {
|
|
|
|
|
|
if (event.instance.textChannel) {
|
2022-06-06 21:40:35 +07:00
|
|
|
|
await sendMessage(event.instance.textChannel, undefined, {
|
2022-06-07 03:08:27 +07:00
|
|
|
|
embeds: [EMBEDS.MUSIC_ERROR(data, locale, event.error)]
|
2022-06-06 21:40:35 +07:00
|
|
|
|
});
|
2022-02-27 17:15:20 +07:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
instance.events.on('disconnect', async (event: VoiceDisconnectedEvent) => {
|
|
|
|
|
|
if (event.instance.textChannel) {
|
2022-06-06 21:40:35 +07:00
|
|
|
|
await sendMessage(event.instance.textChannel, undefined, {
|
2022-06-07 03:08:27 +07:00
|
|
|
|
embeds: [EMBEDS.VOICECHANNEL_DISCONNECTED(data, locale)]
|
2022-06-06 21:40:35 +07:00
|
|
|
|
});
|
2022-02-27 17:15:20 +07:00
|
|
|
|
}
|
2022-03-03 22:48:23 +07:00
|
|
|
|
|
|
|
|
|
|
DiscordMusicPlayer.destoryGuildInstance(event.instance.voiceChannel.guildId);
|
2022-02-27 17:15:20 +07:00
|
|
|
|
});
|
2022-01-30 20:10:52 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
|
export default class Join extends DiscordModule {
|
2022-06-06 21:40:35 +07:00
|
|
|
|
public id = 'Discord_MusicPlayer_Join';
|
|
|
|
|
|
public commands = ['join'];
|
|
|
|
|
|
public commandInteractionName = 'join';
|
2022-01-30 20:10:52 +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:52 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-06 21:40:35 +07:00
|
|
|
|
async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) {
|
2022-03-03 20:56:49 +07:00
|
|
|
|
await this.run(new HybridInteractionMessage(interaction), interaction.options);
|
2022-01-30 20:10:52 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
|
async run(data: HybridInteractionMessage, args: any) {
|
|
|
|
|
|
const guild = data.getGuild();
|
|
|
|
|
|
const member = data.getMember();
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-06-06 21:40:35 +07:00
|
|
|
|
if (!guild || !member) return;
|
2022-01-30 20:10:52 +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
|
|
|
|
const voiceChannel = member.voice.channel;
|
2022-01-30 20:10:52 +07:00
|
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
|
if (!voiceChannel)
|
2022-06-06 21:40:35 +07:00
|
|
|
|
return await sendHybridInteractionMessageResponse(data, {
|
2022-06-07 03:08:27 +07:00
|
|
|
|
embeds: [EMBEDS.USER_NOT_IN_VOICECHANNEL(data, locale)]
|
2022-06-06 21:40:35 +07:00
|
|
|
|
});
|
|
|
|
|
|
|
2022-03-03 20:56:49 +07:00
|
|
|
|
const instance = DiscordMusicPlayer.getGuildInstance(guild.id);
|
2022-06-06 21:33:02 +07:00
|
|
|
|
await joinVoiceChannelProcedure(data, instance, voiceChannel);
|
2022-01-30 20:10:52 +07:00
|
|
|
|
}
|
2022-06-06 21:40:35 +07:00
|
|
|
|
}
|