Fix skip loop crash and repeating spam

This commit is contained in:
2022-05-01 16:56:37 +07:00
parent db3e8f56d0
commit 7826bceffd
2 changed files with 47 additions and 7 deletions
+31 -4
View File
@@ -3,7 +3,7 @@ import DiscordModule, { HybridInteractionMessage } from "../../utils/DiscordModu
import { Message, CommandInteraction, Interaction, VoiceChannel, Permissions, GuildMember, DMChannel, StageChannel, MessageActionRow, MessageButton, TextChannel } from "discord.js";
import { makeSuccessEmbed, makeErrorEmbed, sendMessage, sendMessageOrInteractionResponse, sendHybridInteractionMessageResponse, makeInfoEmbed } from "../../utils/DiscordMessage";
import DiscordProvider from "../../providers/Discord";
import DiscordMusicPlayer, { PlayerPlayingEvent, PlayerErrorEvent, VoiceDisconnectedEvent, ValidTracks, DiscordMusicPlayerInstance } from "../../providers/DiscordMusicPlayer";
import DiscordMusicPlayer, { PlayerPlayingEvent, PlayerErrorEvent, VoiceDisconnectedEvent, ValidTracks, DiscordMusicPlayerInstance, DiscordMusicPlayerLoopMode } from "../../providers/DiscordMusicPlayer";
const EMBEDS = {
VOICECHANNEL_JOINED: (data: Message | Interaction) => {
@@ -63,6 +63,21 @@ const EMBEDS = {
const highestResolutionThumbnail = track.thumbnails.reduce((prev, current) => (prev.height * prev.width > current.height * current.width) ? prev : current)
if(highestResolutionThumbnail)
embed.setImage(highestResolutionThumbnail.url);
return embed;
},
NOW_REPEATING: (data: Message | Interaction, track: ValidTracks) => {
const embed = makeInfoEmbed({
title: 'Now playing (Repeating)',
icon: '🎵',
description: `${track.title}`,
user: DiscordProvider.client.user
});
const highestResolutionThumbnail = track.thumbnails.reduce((prev, current) => (prev.height * prev.width > current.height * current.width) ? prev : current)
if(highestResolutionThumbnail)
embed.setImage(highestResolutionThumbnail.url);
@@ -89,7 +104,7 @@ export async function joinVoiceChannelProcedure (data: Interaction | Message, in
// If already in VoiceChannel
if (DiscordProvider.client.guilds.cache.get(data.guildId!)!.me!.voice.channelId) {
if (DiscordProvider.client.guilds.cache.get(data.guildId)!.me!.voice.channelId) {
// But, no music instance yet (The bot might just restarted)
if (!instance) {
// User is in different VoiceChannel
@@ -151,12 +166,18 @@ export async function joinVoiceChannelProcedure (data: Interaction | Message, in
if (!instance) return;
let nowPlayingMessage: any;
let previousTrack: ValidTracks | undefined;
let isLoopMessageSent = false;
// Register Event Listeners
instance.events.on('playing', async (event: PlayerPlayingEvent) => {
if(!event.instance.queue || !event.instance.queue.track || event.instance.queue.track.length === 0) return;
previousTrack = event.instance.getPreviousTrack();
if(event.instance.queue.track[0] !== previousTrack)
isLoopMessageSent = false;
else if(event.instance.queue.track[0] === previousTrack && isLoopMessageSent) return;
const row = new MessageActionRow()
.addComponents(
@@ -168,7 +189,13 @@ export async function joinVoiceChannelProcedure (data: Interaction | Message, in
)
if (event.instance.textChannel) {
await sendMessage(event.instance.textChannel, undefined, { embeds: [EMBEDS.NOW_PLAYING(data, event.instance.queue.track[0])], components: [row] });
if(event.instance.getLoopMode() === DiscordMusicPlayerLoopMode.Current) {
isLoopMessageSent = true;
await sendMessage(event.instance.textChannel, undefined, { embeds: [EMBEDS.NOW_REPEATING(data, event.instance.queue.track[0])], components: [row] });
}
else {
await sendMessage(event.instance.textChannel, undefined, { embeds: [EMBEDS.NOW_PLAYING(data, event.instance.queue.track[0])], components: [row] });
}
}
});
+16 -3
View File
@@ -65,6 +65,7 @@ export class DiscordMusicPlayerInstance {
public textChannel?: TextChannel;
public voiceChannel: (VoiceChannel | StageChannel);
public voiceConnection?: VoiceConnection;
public previousTrack?: ValidTracks;
public loopMode: DiscordMusicPlayerLoopMode = DiscordMusicPlayerLoopMode.None;
@@ -87,13 +88,19 @@ export class DiscordMusicPlayerInstance {
// Loop mode is set to current song
if (this.loopMode === DiscordMusicPlayerLoopMode.Current) {
this.playTrack(this.queue.track[0]);
if (this.queue.track.length !== 0) {
this.previousTrack = this.queue.track[0];
this.playTrack(this.queue.track[0]);
}
return;
}
// There are more songs in the queue, remove finished song and play the next one
if (this.queue.track.length !== 0) {
this.queue.track.shift();
let previousTrack = this.queue.track.shift();
if(previousTrack)
this.previousTrack = previousTrack;
if (this.queue.track.length > 0) {
this.playTrack(this.queue.track[0]);
}
@@ -203,10 +210,12 @@ export class DiscordMusicPlayerInstance {
if (!this.voiceConnection) throw new Error("No voice connection");
if (this.queue.track.length > 1) {
this.previousTrack = this.queue.track[0];
this.queue.track.shift();
this.playTrack(this.queue.track[0]);
}
else {
this.previousTrack = this.queue.track[0];
this.queue.track.shift();
this.player.stop();
}
@@ -220,6 +229,10 @@ export class DiscordMusicPlayerInstance {
return this.loopMode;
}
public getPreviousTrack(): ValidTracks | undefined {
return this.previousTrack;
}
public async destroy() {
await this.leaveVoiceChannel();
@@ -324,6 +337,7 @@ class DiscordMusicPlayer {
channel: yt_info.video_details.channel,
likes: yt_info.video_details.likes,
live: yt_info.video_details.live,
liveAt: yt_info.video_details.liveAt,
private: yt_info.video_details.private,
tags: yt_info.video_details.tags,
discretionAdvised: yt_info.video_details.discretionAdvised,
@@ -331,7 +345,6 @@ class DiscordMusicPlayer {
});
}
return null;
}