Skip to content

Commit

Permalink
[Fix] fixed queue and lyrics crashing the bot if the text in embe…
Browse files Browse the repository at this point in the history
…d was bigger than 4096. Added docs for `play`
  • Loading branch information
naseif committed Nov 23, 2021
1 parent b3956fb commit 4ba11c7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
31 changes: 24 additions & 7 deletions commands/Music/lyrics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { embedMessage } = require("../../modules/embedSimple");
const { getLyrics, getSong } = require("genius-lyrics-api");
const { getSong } = require("genius-lyrics-api");
const { geniusApiKey } = require("../../config.json");
const { chunkSubstr } = require("../../modules/chunkString");

module.exports = {
name: "lyrics",
Expand Down Expand Up @@ -83,9 +84,17 @@ module.exports = {
timestamp: new Date(),
};

return await message.channel.send({
embeds: [lyricsEmbed],
});
if (lyrics.lyrics.length > 4096) {
const chunkLyrics = chunkSubstr(lyrics.lyrics, 4096);
chunkLyrics.forEach(async (str) => {
lyricsEmbed.description = str;
return await message.channel.send({ embeds: [lyricsEmbed] });
});
} else {
return await message.channel.send({
embeds: [lyricsEmbed],
});
}
} catch (error) {
await message.channel.send({
embeds: [
Expand Down Expand Up @@ -179,9 +188,17 @@ module.exports = {
timestamp: new Date(),
};

return await interaction.followUp({
embeds: [lyricsEmbed],
});
if (lyrics.lyrics.length > 4096) {
const chunkLyrics = chunkSubstr(lyrics.lyrics, 4096);
chunkLyrics.forEach(async (str) => {
lyricsEmbed.description = str;
return await interaction.followUp({ embeds: [lyricsEmbed] });
});
} else {
return await interaction.followUp({
embeds: [lyricsEmbed],
});
}
} catch (error) {
await interaction.followUp({
embeds: [
Expand Down
22 changes: 20 additions & 2 deletions commands/Music/queue.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { chunkSubstr } = require("../../modules/chunkString");
const { embedMessage } = require("../../modules/embedSimple");

module.exports = {
Expand All @@ -20,6 +21,7 @@ module.exports = {
(track, index) =>
`[${index + 1}] - ${track.title} - <@${track.requestedBy.id}>`
);

const queueEmbed = {
color: "#9dcc37",
title: `Queue contains ${queue?.tracks.length} songs!`,
Expand All @@ -37,7 +39,15 @@ module.exports = {
},
};

return await message.channel.send({ embeds: [queueEmbed] });
if (tracks.join("\n").length > 4096) {
const chunked = chunkSubstr(tracks.join("\n"), 4096);
chunked.forEach(async (str) => {
queueEmbed.description = str;
return await message.channel.send({ embeds: [queueEmbed] });
});
} else {
return await message.channel.send({ embeds: [queueEmbed] });
}
},
data: new SlashCommandBuilder()
.setName("queue")
Expand Down Expand Up @@ -70,6 +80,14 @@ module.exports = {
},
};

return await interaction.followUp({ embeds: [queueEmbed] });
if (tracks.join("\n").length > 4096) {
const chunked = chunkSubstr(tracks.join("\n"), 4096);
chunked.forEach(async (str) => {
queueEmbed.description = str;
return await interaction.followUp({ embeds: [queueEmbed] });
});
} else {
return await interaction.followUp({ embeds: [queueEmbed] });
}
},
};
8 changes: 8 additions & 0 deletions modules/Music.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const { QueryType } = require("discord-player");
const { logger } = require("./logger.js");

class Music {
/**
* plays music from youtube and various platforms!
* @param {String} query
* @param {Message} command
* @param {Client} client
* @param {User} user
* @returns
*/
async play(query, command, client, user = null) {
if (!query) throw new Error("No search Query Provided!");

Expand Down
17 changes: 17 additions & 0 deletions modules/chunkString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Chunks a single string into multiple multiple strings
* @param {String} str
* @param {Number} size
* @returns Array containing the chunked strings
*/

module.exports.chunkSubstr = (str, size) => {
const numChunks = Math.ceil(str.length / size);
const chunks = new Array(numChunks);

for (let i = 0, o = 0; i < numChunks; ++i, o += size) {
chunks[i] = str.substr(o, size);
}

return chunks;
};

0 comments on commit 4ba11c7

Please sign in to comment.