Skip to content

Commit

Permalink
Added searching within top recordings and albums
Browse files Browse the repository at this point in the history
  • Loading branch information
coopw1 committed Jan 9, 2024
1 parent 02c6cbd commit 54854ea
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
52 changes: 46 additions & 6 deletions src/commands/general/top.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ module.exports = {
type: ApplicationCommandOptionType.String,
required: false,
},
{
name: "artist",
description: "Filter your top tracks by artist!",
type: ApplicationCommandOptionType.String,
required: false,
},
{
name: "timeperiod",
description: "Time period",
Expand Down Expand Up @@ -192,6 +198,12 @@ module.exports = {
type: ApplicationCommandOptionType.String,
required: false,
},
{
name: "artist",
description: "Filter your top tracks by artist!",
type: ApplicationCommandOptionType.String,
required: false,
},
{
name: "timeperiod",
description: "Time period",
Expand Down Expand Up @@ -289,7 +301,7 @@ module.exports = {
.slice(0, -1)} has no listeners!`
)
.setColor("ba0000");
interaction.reply({ embeds: [embed], ephemeral: true });
interaction.editReply({ embeds: [embed], ephemeral: true });
return;
}
let embed;
Expand All @@ -311,15 +323,19 @@ module.exports = {
.setColor(0x353070);
}

interaction.reply({ embeds: [embed], ephemeral: false });
interaction.editReply({ embeds: [embed], ephemeral: false });
return;
}

const count = interaction.options.get("artist") ? 100 : 100;
const topStatistics = await getTopStatistics(
listenBrainzToken,
brainzUsername,
searchType,
interaction.options.get("timeperiod")?.value || "week"
interaction.options.get("timeperiod")?.value || "week",
false,
null,
count
);

// Check if there are no recently played tracks
Expand All @@ -329,10 +345,34 @@ module.exports = {
`❌ You have no top ${interaction.options.getSubcommand()} stats!`
)
.setColor("ba0000");
interaction.reply({ embeds: [embed], ephemeral: true });
interaction.editReply({ embeds: [embed], ephemeral: true });
return;
}
const maxPages = Math.ceil(topStatistics.count / 10);

let footer;
if (interaction.options.get("artist")) {
topStatistics[searchType] = topStatistics[searchType].filter((item) => {
return item.artist_name
.toLowerCase()
.includes(interaction.options.get("artist").value.toLowerCase());
});
if (topStatistics[searchType].length === 0) {
const embed = new EmbedBuilder()
.setDescription(
`❌ You have no top listens to ${
interaction.options.get("artist").value
}!`
)
.setColor("ba0000");
interaction.editReply({ embeds: [embed], ephemeral: true });
return;
}
footer = ` • ${topStatistics[searchType].length} results for ${
interaction.options.get("artist").value
}`;
}

const maxPages = Math.ceil(topStatistics[searchType].length / 10);
let descriptions = [];

for (let i = 0; i < maxPages; i++) {
Expand Down Expand Up @@ -419,6 +459,6 @@ module.exports = {
);
}

pagination(interaction, embeds, maxPages);
pagination(interaction, embeds, maxPages, footer);
},
};
6 changes: 4 additions & 2 deletions src/commands/general/util/getTopStatistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const axios = require("axios").default;
* @param {string} timePeriod - The time period for which to retrieve statistics.
* @param {boolean} [getListeners=false] - Optional parameter to retrieve listeners for a specific MBID.
* @param {string} [MBID] - Optional parameter for the MBID of an artist or release.
* @param {number} [count=100] - Optional parameter for the number of top statistics to retrieve.
* @return {Promise<Object>} A promise that resolves to the top statistics retrieved from the ListenBrainz API.
*/
module.exports = async (
Expand All @@ -17,7 +18,8 @@ module.exports = async (
searchType,
timePeriod,
getListeners = false,
MBID
MBID,
count = 100
) => {
try {
const AUTH_HEADER = {
Expand All @@ -40,7 +42,7 @@ module.exports = async (
PARAMS = {
params: {
range: timePeriod,
count: 100,
count: count,
},
headers: AUTH_HEADER,
};
Expand Down

0 comments on commit 54854ea

Please sign in to comment.