Files
Yumi/src/discord/MusicPlayer/Join.ts
T

320 lines
12 KiB
TypeScript
Raw Normal View History

2022-06-06 21:40:35 +07:00
import {
Message,
CommandInteraction,
VoiceChannel,
DMChannel,
StageChannel,
2022-06-29 15:27:42 +07:00
ActionRowBuilder,
ButtonBuilder,
PermissionsBitField,
2022-06-29 18:59:06 +07:00
ButtonStyle,
BaseGuildTextChannel,
BaseGuildVoiceChannel
2022-06-06 21:40:35 +07:00
} 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,
2022-07-01 22:04:54 +07:00
DiscordMusicPlayerLoopMode,
TrackUtils
2022-06-06 21:40:35 +07:00
} 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-07-01 22:04:54 +07:00
import { SpotifyTrack, YouTubeVideo } from 'play-dl';
2022-07-05 20:40:16 +07:00
import { checkBotPermissionsInChannel } from '../../utils/DiscordPermission';
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'),
2022-06-28 14:47:45 +07:00
user: DiscordProvider.client.user
2022-06-07 03:08:27 +07:00
});
},
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-07-01 22:04:54 +07:00
NOW_PLAYING: async (data: HybridInteractionMessage, locale: I18n, track: ValidTracks) => {
const title = TrackUtils.getTitle(track);
const thumbnails = await TrackUtils.getThumbnails(track);
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: '🎵 ',
2022-07-01 22:04:54 +07:00
description: title,
2022-01-30 20:10:52 +07:00
user: DiscordProvider.client.user
});
2022-07-01 22:04:54 +07:00
if (TrackUtils.getHighestResolutionThumbnail(thumbnails))
embed.setImage(TrackUtils.getHighestResolutionThumbnail(thumbnails).url);
2022-05-01 16:56:37 +07:00
return embed;
},
2022-07-01 22:04:54 +07:00
NOW_REPEATING: async (data: HybridInteractionMessage, locale: I18n, track: ValidTracks) => {
const title = TrackUtils.getTitle(track);
const thumbnails = await TrackUtils.getThumbnails(track);
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-07-01 22:04:54 +07:00
description: title,
2022-05-01 16:56:37 +07:00
user: DiscordProvider.client.user
});
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: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-29 15:27:42 +07:00
const isSlashCommand = data.isApplicationCommand();
2022-06-06 21:33:02 +07:00
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;
2022-06-29 18:59:06 +07:00
if (!(channel instanceof BaseGuildTextChannel || channel instanceof BaseGuildVoiceChannel)) return;
2022-01-30 20:10:52 +07:00
2022-07-04 21:27:36 +07:00
const memberVoiceChannel = member.voice.channel;
2022-06-06 21:33:02 +07:00
if (!memberVoiceChannel) return;
2022-06-29 15:27:42 +07:00
const bot = guild.members.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-07-05 20:38:07 +07:00
if(!await checkBotPermissionsInChannel({ guild, data, locale, channel: memberVoiceChannel, permissions: [PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.Connect]})) return;
2022-06-07 03:08:27 +07:00
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
2022-06-27 16:25:57 +07:00
if (memberVoiceChannel.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,
2022-06-29 15:27:42 +07:00
member.permissions.has([PermissionsBitField.Flags.MoveMembers])
2022-06-07 03:08:27 +07:00
)
]
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-07-02 00:04:34 +07:00
const locale = await Locale.getGuildLocale(guild.id);
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-07-01 22:04:54 +07:00
const row = new ActionRowBuilder<ButtonBuilder>();
if(event.instance.queue.track[0] instanceof SpotifyTrack)
row.addComponents(
new ButtonBuilder()
.setEmoji('🟢')
2022-07-01 23:08:33 +07:00
.setLabel('Open in Spotify')
2022-07-01 22:04:54 +07:00
.setURL(encodeURI(`https://open.spotify.com/track/${event.instance.queue.track[0].id}`))
.setStyle(ButtonStyle.Link),
)
if(event.instance.queue.track[0] instanceof YouTubeVideo || event.instance.queue.track[0] instanceof SpotifyTrack) {
const actualPlaybackURL = event.instance.getActualPlaybackURL();
if(event.instance.queue.track[0] instanceof SpotifyTrack && !actualPlaybackURL) return;
row.addComponents(
new ButtonBuilder()
.setEmoji('🔴')
2022-07-01 23:08:33 +07:00
.setLabel('Open in YouTube')
2022-07-01 22:04:54 +07:00
.setURL(event.instance.queue.track[0] instanceof YouTubeVideo ? encodeURI(`https://www.youtube.com/watch?v=${event.instance.queue.track[0].id}`) : encodeURI(event.instance.getActualPlaybackURL()!))
.setStyle(ButtonStyle.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-07-01 22:04:54 +07:00
embeds: [await 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-07-01 22:04:54 +07:00
embeds: [await 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
}
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';
2022-06-28 14:48:00 +07:00
public commands = ['join', 'summon'];
2022-06-06 21:40:35 +07:00
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
}