Skip to content

Commit

Permalink
fix(*): Reduce more spammy messages (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazmi35 committed Feb 5, 2021
1 parent 70e9385 commit aa9f4bb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 20 deletions.
49 changes: 35 additions & 14 deletions src/commands/PlayCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ export class PlayCommand extends BaseCommand {
const playlist = await this.client.youtube.getPlaylist(id);
const videos = await playlist.getVideos();
let skippedVideos = 0;
message.channel.send(createEmbed("info", `Adding all videos in playlist: **[${playlist.title}](${playlist.url})**, hang on...`))
.catch(e => this.client.logger.error("PLAY_CMD_ERR:", e));
const addingPlaylistVideoMessage = await message.channel.send(
createEmbed("info", `Adding all videos in playlist: **[${playlist.title}](${playlist.url})**, hang on...`)
.setThumbnail(playlist.thumbnailURL)
);
for (const video of Object.values(videos)) {
if (video.status.privacyStatus === "private") {
skippedVideos++;
Expand All @@ -58,8 +60,18 @@ export class PlayCommand extends BaseCommand {
createEmbed("warn", `${skippedVideos} ${skippedVideos >= 2 ? `videos` : `video`} are skipped because it's a private video`)
).catch(e => this.client.logger.error("PLAY_CMD_ERR:", e));
}
if (skippedVideos === playlist.itemCount) return message.channel.send(createEmbed("error", `Failed to load playlist **[${playlist.title}](${playlist.url})** because all of the items are private videos`));
return message.channel.send(createEmbed("info", `All videos in playlist: **[${playlist.title}](${playlist.url})**, has been added to the queue!`));
message.channel.messages.fetch(addingPlaylistVideoMessage.id, false).then(m => m.delete()).catch(e => this.client.logger.error("YT_PLAYLIST_ERR:", e));
if (skippedVideos === playlist.itemCount) {
return message.channel.send(
createEmbed("error", `Failed to load playlist **[${playlist.title}](${playlist.url})** because all of the items are private videos`)
.setThumbnail(playlist.thumbnailURL)
);
}
return message.channel.send(
createEmbed("info", `All videos in playlist: **[${playlist.title}](${playlist.url})**, has been added to the queue!`)
.setThumbnail(playlist.thumbnailURL)

);
} catch (e) {
this.client.logger.error("YT_PLAYLIST_ERR:", new Error(e.message));
return message.channel.send(createEmbed("error", `I could not load the playlist!\nError: \`${e.message}\``));
Expand Down Expand Up @@ -130,12 +142,17 @@ export class PlayCommand extends BaseCommand {
);
}
message.guild.queue.songs.addSong(song);
if (playlist) return;
message.channel.send(createEmbed("info", `✅ Music **[${song.title}](${song.url})** has been added to the queue`).setThumbnail(song.thumbnail))
.catch(e => this.client.logger.error("PLAY_CMD_ERR:", e));
if (!playlist) {
message.channel.send(createEmbed("info", `✅ Music **[${song.title}](${song.url})** has been added to the queue`).setThumbnail(song.thumbnail))
.catch(e => this.client.logger.error("PLAY_CMD_ERR:", e));
}
} else {
message.guild!.queue = new ServerQueue(message.channel as ITextChannel, voiceChannel);
message.guild?.queue.songs.addSong(song);
if (!playlist) {
message.channel.send(createEmbed("info", `✅ Music **[${song.title}](${song.url})** has been added to the queue`).setThumbnail(song.thumbnail))
.catch(e => this.client.logger.error("PLAY_CMD_ERR:", e));
}
try {
const connection = await message.guild!.queue.voiceChannel!.join();
message.guild!.queue.connection = connection;
Expand All @@ -160,6 +177,8 @@ export class PlayCommand extends BaseCommand {
const serverQueue = guild.queue!;
const song = serverQueue.songs.first();
if (!song) {
if (serverQueue.lastMusicMessageID !== null) serverQueue.textChannel?.messages.fetch(serverQueue.lastMusicMessageID, false).then(m => m.delete()).catch(e => this.client.logger.error("PLAY_ERR:", e));
if (serverQueue.lastVoiceStateUpdateMessageID !== null) serverQueue.textChannel?.messages.fetch(serverQueue.lastVoiceStateUpdateMessageID, false).then(m => m.delete()).catch(e => this.client.logger.error("PLAY_ERR:", e));
serverQueue.textChannel?.send(
createEmbed("info", `⏹ Queue is finished! Use "${guild.client.config.prefix}play" to play more music`)
).catch(e => this.client.logger.error("PLAY_ERR:", e));
Expand Down Expand Up @@ -191,13 +210,15 @@ export class PlayCommand extends BaseCommand {
if (serverQueue.lastMusicMessageID !== null) serverQueue.textChannel?.messages.fetch(serverQueue.lastMusicMessageID, false).then(m => m.delete()).catch(e => this.client.logger.error("PLAY_ERR:", e));
serverQueue.textChannel?.send(createEmbed("info", `⏹ Stop playing: **[${song.title}](${song.url})**`).setThumbnail(song.thumbnail))
.then(m => serverQueue.lastMusicMessageID = m.id)
.catch(e => this.client.logger.error("PLAY_ERR:", e));
this.play(guild).catch(e => {
serverQueue.textChannel?.send(createEmbed("error", `Error while trying to play music\nReason: \`${e}\``))
.catch(e => this.client.logger.error("PLAY_ERR:", e));
serverQueue.connection?.dispatcher.end();
return this.client.logger.error("PLAY_ERR:", e);
});
.catch(e => this.client.logger.error("PLAY_ERR:", e))
.finally(() => {
this.play(guild).catch(e => {
serverQueue.textChannel?.send(createEmbed("error", `Error while trying to play music\nReason: \`${e}\``))
.catch(e => this.client.logger.error("PLAY_ERR:", e));
serverQueue.connection?.dispatcher.end();
return this.client.logger.error("PLAY_ERR:", e);
});
});
})
.on("error", (err: Error) => {
serverQueue.textChannel?.send(createEmbed("error", `Error while playing music\nReason: \`${err.message}\``))
Expand Down
3 changes: 1 addition & 2 deletions src/commands/SkipCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export class SkipCommand extends BaseCommand {
message.channel.send(
createEmbed("info", `⏭ Skipped **[${song!.title}](${song!.url}})**`)
.setThumbnail(song?.thumbnail as string)
)
.catch(e => this.client.logger.error("SKIP_CMD_ERR:", e));
).catch(e => this.client.logger.error("SKIP_CMD_ERR:", e));
}
}
13 changes: 9 additions & 4 deletions src/listeners/VoiceStateUpdateEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class VoiceStateUpdateEvent extends BaseListener {
// Handle when bot gets kicked from the voice channel
if (oldMember?.id === botID && oldID === queueVC.id && newID === undefined) {
try {
if (queue.lastMusicMessageID !== null) queue.textChannel?.messages.fetch(queue.lastMusicMessageID, false).then(m => m.delete()).catch(e => this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e));
this.client.logger.info(`${this.client.shard ? `[Shard #${this.client.shard.ids[0]}]` : ""} Disconnected from the voice channel at ${newState.guild.name}, the queue was deleted.`);
queue.textChannel?.send(createEmbed("warn", "I was disconnected from the voice channel, the queue will be deleted"))
.catch(e => this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e));
Expand Down Expand Up @@ -62,19 +63,22 @@ export class VoiceStateUpdateEvent extends BaseListener {
queue.connection?.dispatcher.pause();
const timeout = this.client.config.deleteQueueTimeout;
const duration = formatMS(timeout);
if (queue.lastVoiceStateUpdateMessageID !== null) queue.textChannel?.messages.fetch(queue.lastVoiceStateUpdateMessageID, false).then(m => m.delete()).catch(e => this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e));
newState.guild.queue!.timeout = setTimeout(() => {
queue.voiceChannel?.leave();
newState.guild.queue = null;
if (queue.lastMusicMessageID !== null) queue.textChannel?.messages.fetch(queue.lastMusicMessageID, false).then(m => m.delete()).catch(e => this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e));
if (queue.lastVoiceStateUpdateMessageID !== null) queue.textChannel?.messages.fetch(queue.lastVoiceStateUpdateMessageID, false).then(m => m.delete()).catch(e => this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e));
queue.textChannel?.send(
createEmbed("error", `\`${duration}\` have passed and there is no one who joins my voice channel, the queue was deleted.`)
createEmbed("error", `**${duration}** have passed and there is no one who joins my voice channel, the queue was deleted.`)
.setTitle("⏹ Queue deleted.")
).catch(e => this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e));
}, timeout);
queue.textChannel?.send(
createEmbed("warn", "Everyone has left from my voice channel, to save resources, the queue was paused. " +
`If there's no one who joins my voice channel in the next ${duration}, the queue will be deleted.`)
`If there's no one who joins my voice channel in the next **${duration}**, the queue will be deleted.`)
.setTitle("⏸ Queue paused.")
).catch(e => this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e));
).then(m => queue.lastVoiceStateUpdateMessageID = m.id).catch(e => this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e));
} catch (e) { this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e); }
}

Expand All @@ -85,11 +89,12 @@ export class VoiceStateUpdateEvent extends BaseListener {
clearTimeout(queue.timeout!);
newState.guild.queue!.timeout = null;
const song = queue.songs.first();
if (queue.lastVoiceStateUpdateMessageID !== null) queue.textChannel?.messages.fetch(queue.lastVoiceStateUpdateMessageID, false).then(m => m.delete()).catch(e => this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e));
queue.textChannel?.send(
createEmbed("info", `Someone joins the voice channel. Enjoy the music 🎶\nNow Playing: **[${song!.title}](${song!.url})**`)
.setThumbnail(song!.thumbnail)
.setTitle("▶ Queue resumed")
).catch(e => this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e));
).then(m => queue.lastVoiceStateUpdateMessageID = m.id).catch(e => this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e));
newState.guild.queue!.playing = true;
newState.guild.queue?.connection?.dispatcher.resume();
} catch (e) { this.client.logger.error("VOICE_STATE_UPDATE_EVENT_ERR:", e); }
Expand Down
9 changes: 9 additions & 0 deletions src/structures/ServerQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class ServerQueue {
public loopMode: 0 | 1 | 2 = 0;
public timeout: NodeJS.Timeout | null = null;
private _lastMusicMessageID: Snowflake | null = null;
private _lastvoiceStateUpdateMessageID: Snowflake | null = null;
public constructor(public textChannel: ITextChannel | null = null, public voiceChannel: VoiceChannel | null = null) {
this.volume = textChannel!.client.config.defaultVolume;
Object.defineProperty(this, "_lastMusicMessageID", {
Expand All @@ -25,4 +26,12 @@ export class ServerQueue {
public set lastMusicMessageID(id: Snowflake | null) {
this._lastMusicMessageID = id;
}

public get lastVoiceStateUpdateMessageID(): Snowflake | null {
return this._lastvoiceStateUpdateMessageID;
}

public set lastVoiceStateUpdateMessageID(id: Snowflake | null) {
this._lastvoiceStateUpdateMessageID = id;
}
}
4 changes: 4 additions & 0 deletions src/utils/YoutubeAPI/structures/Playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ export class Playlist implements IPlaylist {
const videos = await this.yt.makePaginatedRequest("playlistItems", { maxResults: 50, playlistId: this.id }, this.itemCount);
return videos.map((i: any) => new Video(this.yt, i, "playlistItem"));
}

public get thumbnailURL(): string {
return (this.thumbnails.maxres ?? this.thumbnails.high ?? this.thumbnails.medium ?? this.thumbnails.standard ?? this.thumbnails.default).url;
}
}

0 comments on commit aa9f4bb

Please sign in to comment.