Skip to content

Commit

Permalink
bot: v3.3.2
Browse files Browse the repository at this point in the history
* playlist: Removed unnecessary similar code for the playlist message

* music(internals): Added one check in the inactivity leave

* handlers(command): Fix crash when replying to a already replied and/or deferred interaction

* lyrics: Fix possible crash
  • Loading branch information
nikosszzz committed May 24, 2024
1 parent 94b16e7 commit c9d5f23
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 66 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "music-bot",
"version": "3.3.1",
"version": "3.3.2",
"description": "A general music Discord bot.",
"main": "./src/bot.ts",
"type": "module",
Expand Down
7 changes: 4 additions & 3 deletions src/commands/info/changelogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ export default {
.setDescription("Displays information about the latest Music Bot update."),
async execute(interaction) {

const bugfixes = `- None`;
const bugfixes = `- handlers(command): Fix crash when replying to a already replied and/or deferred interaction
- lyrics: Fix possible crash`;

const whatsnew = `- bot: Updated and upgraded
- codebase: Update to ESM format
- lyrics: New package used for lyrics`;
- playlist: Removed unnecessary similar code for the playlist message
- music(internals): Added one check in the inactivity leave`;

const UpdateEmbed = new EmbedBuilder()
.setColor("NotQuiteBlack")
Expand Down
22 changes: 11 additions & 11 deletions src/commands/music/lyrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ export default {
await interaction.reply({ embeds: [lyricsFetch], ephemeral: true });

try {
lyricsSearch = await lyrics.default(queue.songs[0].title);
if (!lyricsSearch.lyrics) return interaction.editReply({ embeds: [lyricsNotFound] });
lyricsSearch = await lyrics.default(title);
if (!lyricsSearch?.lyrics) return interaction.editReply({ embeds: [lyricsNotFound] });

const lyricsEmbed = new EmbedBuilder()
.setColor("NotQuiteBlack")
.setTitle(`${queue.songs[0].title} - Lyrics`)
.setDescription(lyricsSearch.lyrics);

if ((lyricsEmbed.data.description?.length as number) >= 2048)
lyricsEmbed.data.description = lyricsEmbed.data.description?.substring(0, 2045) + "...";
return interaction.editReply({ embeds: [lyricsEmbed] });
} catch (err: any) {
Logger.error({ type: "MUSICCMDS", err: err });
return interaction.editReply({ embeds: [lyricsNotFound] });
}

const lyricsEmbed = new EmbedBuilder()
.setColor("NotQuiteBlack")
.setTitle(`${queue.songs[0].title} - Lyrics`)
.setDescription(lyricsSearch.lyrics);

if ((lyricsEmbed.data.description?.length as number) >= 2048)
lyricsEmbed.data.description = lyricsEmbed.data.description?.substr(0, 2045) + "..." as string;
return interaction.editReply({ embeds: [lyricsEmbed] });
},
} as Command;
6 changes: 3 additions & 3 deletions src/commands/music/nowplaying.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ export default {
nowPlaying.addFields(
{
name: "\u200b",
value: new Date(seek * 1000).toISOString().substr(11, 8) +
value: new Date(seek * 1000).toISOString().substring(11, 8) +
"** **[" +
splitBar(song.duration == 0 ? seek : song.duration, seek, 20)[0] +
"]** **" +
(song.duration == 0 ? " ◉ LIVE" : new Date(song.duration * 1000).toISOString().substr(11, 8)),
(song.duration == 0 ? " ◉ LIVE" : new Date(song.duration * 1000).toISOString().substring(11, 8)),
inline: false
});

nowPlaying.setFooter({ text: "Time Remaining: " + new Date(left * 1000).toISOString().substr(11, 8) });
nowPlaying.setFooter({ text: "Time Remaining: " + new Date(left * 1000).toISOString().substring(11, 8) });
}

return interaction.reply({ embeds: [nowPlaying] });
Expand Down
3 changes: 2 additions & 1 deletion src/commands/music/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default {
}

await interaction.reply({ content: "⏳ Loading..." });

let song: Song;
try {
song = await Song.from({ search: url, interaction });
Expand Down Expand Up @@ -83,6 +83,7 @@ export default {
});

interaction.client.queues.set(interaction.guild?.id as string, newQueue);
await interaction.deleteReply();

return await newQueue.enqueue({ songs: [song] });
}
Expand Down
39 changes: 7 additions & 32 deletions src/commands/music/playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default {
),
async execute(interaction) {
const { channel } = (interaction.member as GuildMember).voice;
const queue = interaction.client.queues.get(interaction.guild?.id as string);
let queue = interaction.client.queues.get(interaction.guild?.id as string);
const url = interaction.options.getString("query") as string;

/* Embeds for music */
Expand Down Expand Up @@ -54,35 +54,12 @@ export default {

if (queue) {
if (!queue.songs.length) {
const playlistEmbed = new EmbedBuilder()
.setColor("NotQuiteBlack")
.setTitle("Track Player")
.setDescription("The following playlist is now playing:")
.addFields(
{
name: playlist.data instanceof SpotifyPlaylist || playlist.data instanceof SoundCloudPlaylist ? playlist.data.name : playlist.data.title!, value: "** **"
})
.setURL(playlist.data.url as string);

interaction.editReply({ embeds: [playlistEmbed] }).catch((err: Error) => Logger.error({ type: "MUSICCMDS", err }));
return queue.enqueue({ songs: playlist.videos });
} else {
queue.enqueue({ songs: playlist.videos });
} else {
queue.songs.push(...playlist.videos);

const playlistEmbed = new EmbedBuilder()
.setColor("NotQuiteBlack")
.setTitle("Track Player")
.setDescription("The following playlist has been added to the queue:")
.addFields(
{
name: playlist.data instanceof SpotifyPlaylist || playlist.data instanceof SoundCloudPlaylist ? playlist.data.name : playlist.data.title!, value: "** **"
})
.setURL(playlist.data.url as string);

return interaction.editReply({ embeds: [playlistEmbed] }).catch((err: Error) => Logger.error({ type: "MUSICCMDS", err }));
}
} else {
const newQueue = new MusicQueue({
queue = new MusicQueue({
options: {
interaction,
connection: joinVoiceChannel({
Expand All @@ -92,16 +69,14 @@ export default {
})
}
});
interaction.client.queues.set(interaction.guild?.id as string, queue);

interaction.client.queues.set(interaction.guild?.id as string, newQueue);

await newQueue.enqueue({ songs: playlist.videos });
await queue.enqueue({ songs: playlist.videos });
}

const playlistEmbed = new EmbedBuilder()
.setColor("NotQuiteBlack")
.setTitle("Track Player")
.setDescription("The following playlist is now playing:")
.setDescription("The following playlist has been queued")
.addFields(
{
name: playlist.data instanceof SpotifyPlaylist || playlist.data instanceof SoundCloudPlaylist ? playlist.data.name : playlist.data.title!, value: "** **"
Expand Down
1 change: 1 addition & 0 deletions src/commands/music/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
if (!canModifyQueue({ member: interaction.member as GuildMember })) return interaction.reply({ embeds: [notInBotChannel], ephemeral: true });
if (!queue) return interaction.reply({ embeds: [nothingPlaying], ephemeral: true });

queue.songs = [];
queue.player.stop(true);

const stopEmbed = new EmbedBuilder()
Expand Down
2 changes: 1 addition & 1 deletion src/components/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Bot extends Client {
public commands = new Collection<string, Command>();
public queues = new Collection<string, MusicQueue>();
public readonly debug: boolean = false;
public readonly version: string = "3.3.1";
public readonly version: string = "3.3.2";
public readonly branch: string;

constructor(options: ClientOptions) {
Expand Down
20 changes: 10 additions & 10 deletions src/components/MusicQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
VoiceConnectionStatus,
type AudioPlayerPlayingState
} from "@discordjs/voice";
import type { CacheType, ChatInputCommandInteraction, Message, TextChannel } from "discord.js";
import type { CacheType, ChatInputCommandInteraction, GuildTextBasedChannel } from "discord.js";
import { promisify } from "node:util";
import type { QueueOptions } from "@common";
import { config } from "@components/config";
Expand All @@ -31,7 +31,7 @@ export class MusicQueue {
public readonly interaction!: ChatInputCommandInteraction<CacheType>;
public readonly connection!: VoiceConnection;
public readonly player: AudioPlayer;
public readonly textChannel: TextChannel;
public readonly textChannel: GuildTextBasedChannel;
public readonly bot: Bot;

public resource!: AudioResource;
Expand All @@ -47,7 +47,7 @@ export class MusicQueue {
Object.assign(this, options);

this.bot = options.interaction.client;
this.textChannel = options.interaction.channel as TextChannel;
this.textChannel = options.interaction.channel as GuildTextBasedChannel;
this.player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Play } });

options.connection.subscribe(this.player);
Expand Down Expand Up @@ -139,6 +139,8 @@ export class MusicQueue {
} catch { }
}

if (this.player.state.status == AudioPlayerStatus.Playing || this.songs.length) return;

this.bot.queues.delete(this.interaction.guild?.id as string);
this.textChannel.send("Left channel due to inactivity.");
}, config.STAY_TIME * 1000);
Expand Down Expand Up @@ -173,16 +175,14 @@ export class MusicQueue {
}

private async sendPlayingMessage({ newState }: { newState: AudioPlayerPlayingState; }): Promise<void> {
let playingMessage: Message<boolean>;

try {
playingMessage = this.interaction.replied || this.interaction.deferred ? await this.interaction.editReply((newState.resource as AudioResource<Song>).metadata.startMessage()) : await this.textChannel.send((newState.resource as AudioResource<Song>).metadata.startMessage());
const playingMessage = await this.textChannel.send((newState.resource as AudioResource<Song>).metadata.startMessage());

if (config.PRUNING) setTimeout((): void => {
playingMessage.delete().catch();
}, 3000);
} catch (err: any | Error) {
Logger.error({ type: "INTERNAL:MUSIC", err: err });
}

if (config.PRUNING) setTimeout((): void => {
playingMessage.delete().catch();
}, 3000);
}
}
2 changes: 1 addition & 1 deletion src/components/Playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class Playlist {

pl = { data: playlist, tracks: await playlist.all_tracks() };
} else {
const playlist = await playlist_info(search as string, { incomplete: true });
const playlist = await playlist_info((await playSearch(search, { source: { youtube: "playlist" } }))[0].url!, { incomplete: true });

pl = { data: playlist, tracks: await playlist.all_videos() };
}
Expand Down
4 changes: 2 additions & 2 deletions src/manager/modules/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export async function commands(client: Bot): Promise<void> {
try {
await command.execute(interaction);
} catch (err: any) {
Logger.error({ type: "INTERNALS/CMDHANDLER", err: err.stack });
await interaction.reply({ content: "An error occurred while executing this command.", ephemeral: true });
Logger.error({ type: "INTERNALS/CMDHANDLER", err: err });
interaction.replied || interaction.deferred ? await interaction.editReply({ content: "An error occurred while executing this command.", embeds: [] }) : await interaction.reply({ content: "An error occurred while executing this command.", embeds: [], ephemeral: true });
}
});
}
Expand Down

0 comments on commit c9d5f23

Please sign in to comment.