From 09d9a28b19447ef309240f9ac53062758d2679b3 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Mon, 4 Mar 2024 15:05:12 +0100 Subject: [PATCH 01/20] Rename displayCurrentScrobble to getCurrentScrobble --- commands/misc/fm.js | 4 ++-- .../{displayCurrentScrobble.js => getCurrentScrobble.js} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename util/lastfm/{displayCurrentScrobble.js => getCurrentScrobble.js} (100%) diff --git a/commands/misc/fm.js b/commands/misc/fm.js index 9b0d0f4..284cca3 100644 --- a/commands/misc/fm.js +++ b/commands/misc/fm.js @@ -1,5 +1,5 @@ const fmlogin = require("../../util/lastfm/fmlogin"); -const displayCurrentScrobble = require("../../util/lastfm/displayCurrentScrobble"); +const getCurrentScrobble = require("../../util/lastfm/getCurrentScrobble"); module.exports = { name: 'fm', @@ -18,7 +18,7 @@ module.exports = { break; } if(args.length < 1){ - sendText = await displayCurrentScrobble(message.author.id); + sendText = await getCurrentScrobble(message.author.id); } message.channel.send(sendText); } diff --git a/util/lastfm/displayCurrentScrobble.js b/util/lastfm/getCurrentScrobble.js similarity index 100% rename from util/lastfm/displayCurrentScrobble.js rename to util/lastfm/getCurrentScrobble.js From 53358531e5867e49c8354f609d8da3e633fbc28a Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 10:47:35 +0100 Subject: [PATCH 02/20] Split getFmUsername into own function This also fixes the bug that happens when a user tries the default fm command and it crashes the bot. --- util/lastfm/getCurrentScrobble.js | 23 +++-------------------- util/lastfm/getFmUsername.js | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 20 deletions(-) create mode 100644 util/lastfm/getFmUsername.js diff --git a/util/lastfm/getCurrentScrobble.js b/util/lastfm/getCurrentScrobble.js index 456a512..dbc7030 100644 --- a/util/lastfm/getCurrentScrobble.js +++ b/util/lastfm/getCurrentScrobble.js @@ -1,28 +1,11 @@ +const getFmUsername = require("./getFmUsername"); + require("dotenv").config(); module.exports = async function(userID) { let sendText = ""; let scrobble = {}; const apiKey = process.env.LAST_FM_API_KEY; - let lastfmUsername = await new Promise((resolve, reject)=>{ - const sqlite3 = require('sqlite3').verbose(); - const db = new sqlite3.Database('data/database.db'); - db.get( - `SELECT * FROM lastfm WHERE userID = ?`, - [userID], - (error, row) => { - if (error) { - console.error(error); - reject(error); - } else { - if (row == undefined) { - resolve(undefined); - } - resolve(row.lastfmUsername); - } - db.close(); - } - ); - }); + let lastfmUsername = await getFmUsername(userID); if(lastfmUsername != undefined){ scrobble = await new Promise ((resolve, reject) => { fetch(`https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${lastfmUsername}&api_key=${apiKey}&format=json`) diff --git a/util/lastfm/getFmUsername.js b/util/lastfm/getFmUsername.js new file mode 100644 index 0000000..30dab1a --- /dev/null +++ b/util/lastfm/getFmUsername.js @@ -0,0 +1,24 @@ +module.exports = async function(userID) { + let lastfmUsername = await new Promise((resolve, reject)=>{ + const sqlite3 = require('sqlite3').verbose(); + const db = new sqlite3.Database('data/database.db'); + db.get( + `SELECT * FROM lastfm WHERE userID = ?`, + [userID], + (error, row) => { + if (error) { + console.error(error); + reject(error); + } else { + if (row == undefined) { + resolve(undefined); + }else{ + resolve(row.lastfmUsername); + } + } + db.close(); + } + ); + }); + return lastfmUsername; +} \ No newline at end of file From b539f41d5176eec61d8cfb0c8b6e2d6832a9ddf4 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 10:53:52 +0100 Subject: [PATCH 03/20] Move checks before other things No need to do other things before this check. --- server/message.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server/message.js b/server/message.js index 5065a5f..270f814 100644 --- a/server/message.js +++ b/server/message.js @@ -7,6 +7,9 @@ module.exports = function(client, owners, message, globalPrefix){ if (serverPrefix) { prefix = serverPrefix; } + + if (!message.guild || message.author.bot || !message.content.startsWith(prefix)) return; + if(message.content.startsWith(`<@${client.user.id}>`)){ let regex = new RegExp("(<@" + client.user.id + ">) *") message.content = message.content.replace(regex, prefix); @@ -16,7 +19,6 @@ module.exports = function(client, owners, message, globalPrefix){ const commandName = args.shift().toLowerCase(); const command = client.commands.get(commandName); const netModule = client.netmodules.get(commandName); - if (!message.guild || message.author.bot || !message.content.startsWith(prefix)) return; if (!command){ if (netModule){ try { From 5b202ae9d34e72c732f0087a6e3683edeba6c7e2 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 11:03:01 +0100 Subject: [PATCH 04/20] Add better syntax for the fm command --- server/message.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/message.js b/server/message.js index 270f814..e629779 100644 --- a/server/message.js +++ b/server/message.js @@ -15,6 +15,10 @@ module.exports = function(client, owners, message, globalPrefix){ message.content = message.content.replace(regex, prefix); } let args = message.content.slice(prefix.length).split(" ") + if(args[0] !== "fm" && args[0].startsWith("fm")){ + let firstElement = args[0]; + args.splice(0, 1, firstElement.substring(0, 2), firstElement.substring(2)); + } const commandName = args.shift().toLowerCase(); const command = client.commands.get(commandName); From 51e810264c9cf4b3cd5151fda32e287dc0e21095 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 11:18:21 +0100 Subject: [PATCH 05/20] FM: Add a toptracks feature --- commands/misc/fm.js | 5 ++++- util/lastfm/getTopTracks.js | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 util/lastfm/getTopTracks.js diff --git a/commands/misc/fm.js b/commands/misc/fm.js index 284cca3..d789ae9 100644 --- a/commands/misc/fm.js +++ b/commands/misc/fm.js @@ -1,6 +1,6 @@ const fmlogin = require("../../util/lastfm/fmlogin"); const getCurrentScrobble = require("../../util/lastfm/getCurrentScrobble"); - +const getTopTracks = require("../../util/lastfm/getTopTracks"); module.exports = { name: 'fm', description: 'Last fm commands. See `help fm` for more info.', @@ -14,6 +14,9 @@ module.exports = { case "set": sendText = await fmlogin(message.author.id, args[1]); break; + case "toptracks": + case "tt": + sendText = await getTopTracks(message.author.id, args); default: break; } diff --git a/util/lastfm/getTopTracks.js b/util/lastfm/getTopTracks.js new file mode 100644 index 0000000..997dafb --- /dev/null +++ b/util/lastfm/getTopTracks.js @@ -0,0 +1,38 @@ +// http://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=username&api_key=YOUR_API_KEY&format=json + +const getFmUsername = require("./getFmUsername") + +module.exports = async function (userID, options) { + let lastfmUsername = await getFmUsername(userID); + let sendText = ""; + let tracks = []; + const apiKey = process.env.LAST_FM_API_KEY; + if(lastfmUsername != undefined){ + tracks = await new Promise ((resolve, reject) => { + fetch(`https://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=${lastfmUsername}&api_key=${apiKey}&format=json`) + .then(response => response.json()) + .then(data => { + for(let i = 0; i < 10; i++){ + let track = {} + let currentTrack = data.toptracks.track[i]; + track.artist = currentTrack.artist.name; + track.song = currentTrack.name; + track.playcount = currentTrack.playcount; + tracks.push(track); + } + resolve(tracks); + }) + .catch(error => { + console.error(error); + reject(error); + }); + }); + sendText = `Your top 10 tracks are:\n`; + for(let i = 0; i < tracks.length; i++){ + sendText += `${i}. ${tracks[i].artist} - ${tracks[i].song} (${tracks[i].playcount} plays)\n`; + } + } else { + sendText = "You haven't set your last.fm username yet. Use `fm set ` to set it."; + } + return sendText; +} \ No newline at end of file From 0871b659a16ac257e3e4d62da28ab71fd522614f Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 11:38:28 +0100 Subject: [PATCH 06/20] Fix fmhelp command crashing the bot Was trying to send an array. Didn't think... --- commands/misc/fm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/misc/fm.js b/commands/misc/fm.js index d789ae9..847d506 100644 --- a/commands/misc/fm.js +++ b/commands/misc/fm.js @@ -9,7 +9,7 @@ module.exports = { let sendText = "Something went wrong."; switch (args[0]) { case "help": - sendText = this.moreHelp; + sendText = this.moreHelp.join("\n"); break; case "set": sendText = await fmlogin(message.author.id, args[1]); From efcf081e80fbae5c9f3818d2e293df52ff4023b4 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 11:47:57 +0100 Subject: [PATCH 07/20] Replace with prefix in fmhelp --- commands/misc/fm.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/misc/fm.js b/commands/misc/fm.js index 847d506..83e0720 100644 --- a/commands/misc/fm.js +++ b/commands/misc/fm.js @@ -5,11 +5,11 @@ module.exports = { name: 'fm', description: 'Last fm commands. See `help fm` for more info.', moreHelp: ["Set username: `fm set `",], - async execute({ message, args }) { + async execute({ message, args, prefix }) { let sendText = "Something went wrong."; switch (args[0]) { case "help": - sendText = this.moreHelp.join("\n"); + sendText = this.moreHelp.join("\n").replace("", prefix); break; case "set": sendText = await fmlogin(message.author.id, args[1]); From 02b60c31b4b610706781730fc99c336a4e806b9a Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 11:48:45 +0100 Subject: [PATCH 08/20] Remove some logging --- util/lastfm/fmlogin.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/util/lastfm/fmlogin.js b/util/lastfm/fmlogin.js index 9700bb4..b2767b4 100644 --- a/util/lastfm/fmlogin.js +++ b/util/lastfm/fmlogin.js @@ -33,7 +33,6 @@ module.exports = async function(userID, lastfmUsername) { reject(sendText); } else { let sendText = `Your last.fm username has been set to '${lastfmUsername}'.`; - console.log(sendText); resolve(sendText); } db.close(); @@ -53,7 +52,6 @@ module.exports = async function(userID, lastfmUsername) { reject(sendText); } else { let sendText = `Your last.fm username has been updated to '${lastfmUsername}'.`; - console.log(sendText); resolve(sendText); } db.close(); From 653f9d0339ad706eb6228720a4a76d7bef67db5a Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 11:53:23 +0100 Subject: [PATCH 09/20] FM: Update moreHelp with up to date information --- commands/misc/fm.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/commands/misc/fm.js b/commands/misc/fm.js index 83e0720..c240c6b 100644 --- a/commands/misc/fm.js +++ b/commands/misc/fm.js @@ -4,7 +4,12 @@ const getTopTracks = require("../../util/lastfm/getTopTracks"); module.exports = { name: 'fm', description: 'Last fm commands. See `help fm` for more info.', - moreHelp: ["Set username: `fm set `",], + moreHelp: ["Info: Having a space between fm and the subcommand makes no difference.", + "They behave the same (for example: `fmtt` and `fm tt`)", + "Set username: `fmset `", + "Get current scrobble: `fm`", + "Get top tracks: `fmtt`" + ], async execute({ message, args, prefix }) { let sendText = "Something went wrong."; switch (args[0]) { From e5862a323be9952ecc97b5bf55b09e400f5ea4ba Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 11:58:01 +0100 Subject: [PATCH 10/20] FM: Replace all with prefix --- commands/misc/fm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/misc/fm.js b/commands/misc/fm.js index c240c6b..82dab5c 100644 --- a/commands/misc/fm.js +++ b/commands/misc/fm.js @@ -14,7 +14,7 @@ module.exports = { let sendText = "Something went wrong."; switch (args[0]) { case "help": - sendText = this.moreHelp.join("\n").replace("", prefix); + sendText = this.moreHelp.join("\n").replace(//g, prefix); break; case "set": sendText = await fmlogin(message.author.id, args[1]); From 3c7d0a6c28367ed4744f76831e795fa1002d92ef Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 12:35:58 +0100 Subject: [PATCH 11/20] fm: Add the possibility to specify period --- commands/misc/fm.js | 2 ++ util/lastfm/getTopTracks.js | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/commands/misc/fm.js b/commands/misc/fm.js index 82dab5c..faaeb3b 100644 --- a/commands/misc/fm.js +++ b/commands/misc/fm.js @@ -21,7 +21,9 @@ module.exports = { break; case "toptracks": case "tt": + args.shift(); sendText = await getTopTracks(message.author.id, args); + break; default: break; } diff --git a/util/lastfm/getTopTracks.js b/util/lastfm/getTopTracks.js index 997dafb..fad0ce3 100644 --- a/util/lastfm/getTopTracks.js +++ b/util/lastfm/getTopTracks.js @@ -2,14 +2,30 @@ const getFmUsername = require("./getFmUsername") -module.exports = async function (userID, options) { +module.exports = async function (userID, option) { let lastfmUsername = await getFmUsername(userID); let sendText = ""; let tracks = []; + const options = { + "alltime": "overall", + "weekly": "7day", + "monthly": "1month", + "quarterly": "3month", + "halfyear": "6month", + "yearly": "12month", + undefined: "7day" + } + if(option.length === 0){ + option[0] = "weekly" + }else { + option[0] = options[option[0]]; + if(option[0] === undefined) + option[0] = options[option[0]]; + } const apiKey = process.env.LAST_FM_API_KEY; if(lastfmUsername != undefined){ tracks = await new Promise ((resolve, reject) => { - fetch(`https://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=${lastfmUsername}&api_key=${apiKey}&format=json`) + fetch(`https://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=${lastfmUsername}&period${option[0]}&api_key=${apiKey}&format=json`) .then(response => response.json()) .then(data => { for(let i = 0; i < 10; i++){ From 3ed47144579ecc8b4e53f4dc2650b825e448aab2 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 12:40:56 +0100 Subject: [PATCH 12/20] fmtt: fix time period not working --- util/lastfm/getTopTracks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/lastfm/getTopTracks.js b/util/lastfm/getTopTracks.js index fad0ce3..c707d1d 100644 --- a/util/lastfm/getTopTracks.js +++ b/util/lastfm/getTopTracks.js @@ -25,7 +25,7 @@ module.exports = async function (userID, option) { const apiKey = process.env.LAST_FM_API_KEY; if(lastfmUsername != undefined){ tracks = await new Promise ((resolve, reject) => { - fetch(`https://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=${lastfmUsername}&period${option[0]}&api_key=${apiKey}&format=json`) + fetch(`https://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=${lastfmUsername}&period=${option[0]}&api_key=${apiKey}&format=json`) .then(response => response.json()) .then(data => { for(let i = 0; i < 10; i++){ From 4a67153fb9118074aea4b7c63a80801f200c4c67 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 12:43:34 +0100 Subject: [PATCH 13/20] fmtt: Set weekly as the default time period --- util/lastfm/getTopTracks.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/util/lastfm/getTopTracks.js b/util/lastfm/getTopTracks.js index c707d1d..f38a78d 100644 --- a/util/lastfm/getTopTracks.js +++ b/util/lastfm/getTopTracks.js @@ -17,12 +17,13 @@ module.exports = async function (userID, option) { } if(option.length === 0){ option[0] = "weekly" - }else { - option[0] = options[option[0]]; - if(option[0] === undefined) - option[0] = options[option[0]]; } - const apiKey = process.env.LAST_FM_API_KEY; + + option[0] = options[option[0]]; + if(option[0] === undefined) + option[0] = options[option[0]]; + + const apiKey = process.env.LAST_FM_API_KEY; if(lastfmUsername != undefined){ tracks = await new Promise ((resolve, reject) => { fetch(`https://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=${lastfmUsername}&period=${option[0]}&api_key=${apiKey}&format=json`) From 27ec24b7d40c2a9f00600995f18e8a47aed11d57 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 7 Mar 2024 12:57:08 +0100 Subject: [PATCH 14/20] fm: Show which last.fm username the top tracks are from --- util/lastfm/getTopTracks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/lastfm/getTopTracks.js b/util/lastfm/getTopTracks.js index f38a78d..42097f0 100644 --- a/util/lastfm/getTopTracks.js +++ b/util/lastfm/getTopTracks.js @@ -44,7 +44,7 @@ module.exports = async function (userID, option) { reject(error); }); }); - sendText = `Your top 10 tracks are:\n`; + sendText = `Top weekly tracks for ${lastfmUsername}:\n`; for(let i = 0; i < tracks.length; i++){ sendText += `${i}. ${tracks[i].artist} - ${tracks[i].song} (${tracks[i].playcount} plays)\n`; } From b9eeeef97b35f41380f70032e36b86ea52076bc2 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Fri, 22 Mar 2024 09:43:07 +0100 Subject: [PATCH 15/20] Add total amount of commands to botinfo --- commands/info/botinfo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/info/botinfo.js b/commands/info/botinfo.js index 400ea95..247c068 100644 --- a/commands/info/botinfo.js +++ b/commands/info/botinfo.js @@ -15,7 +15,7 @@ module.exports = { .setTimestamp() .setAuthor(client.user.username, client.user.avatarURL({ dynamic: true, size: 4096 })) .addFields({ - name: "General info", value: `Name: ${client.user.username}\nPrefix: ${prefix}\nTotal Servers: ${guildCount}\nCreation Date: ${getCreationDate(client)}\nSource: [Click Here](https://github.com/SileNce5k/discord_bot)`, + name: "General info", value: `Name: ${client.user.username}\nPrefix: ${prefix}\nTotal Servers: ${guildCount}\nTotal Commands: ${client.commands.size}\nCreation Date: ${getCreationDate(client)}\nSource: [Click Here](https://github.com/SileNce5k/discord_bot)`, },) message.channel.send({embeds :[embed]}) From e56d8e768cf2478bec7bdcceacfe33aec2758c96 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Fri, 22 Mar 2024 09:48:04 +0100 Subject: [PATCH 16/20] Add pages to help command --- commands/info/help.js | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/commands/info/help.js b/commands/info/help.js index 4b46159..1e1d549 100644 --- a/commands/info/help.js +++ b/commands/info/help.js @@ -6,7 +6,7 @@ const getSubdirFiles = require('../../util/getSubdirFiles'); module.exports = { name: 'help', description: 'List all available commands.', - moreHelp: ["Examples:","`help` will return help with a small description for each command", + moreHelp: ["Examples:","`help [optional_page]` will return help with a small description for each command", "`help ` will return help with a more descriptive description", "The descriptive description isn't available on all commands", "`help netmodules` to display help for netmodules" @@ -15,15 +15,29 @@ module.exports = { let commands = "" let commandFiles = getSubdirFiles('commands/') let x = false; - let fieldName = "General"; + let fieldName = `Page [[page]]/${Math.round(client.commands.size / 10)}`; + let iteration = 0; + let num_in_args = false; + let added_commands = 0; + let page = -1; if (args[0] == "netmodules") { commandFiles = fs.readdirSync('../../netload').filter(file => file.endsWith('.js')); if (commandFiles.length == 0) { message.channel.send("There are no netmodules currently loaded.") x = true; } - + + }else if(!isNaN(parseInt(args[0])) && parseInt(args[0])){ + num_in_args = true; + iteration = ( parseInt(args[0]) - 1) * 10; + page = Math.round(parseInt(args[0])) } + if(page === -1){ + page = 1; + } + fieldName = fieldName.replace("[[page]]", page); + const max_commands = iteration + 10; + if (x) return; const embed = new Discord.MessageEmbed() @@ -31,17 +45,26 @@ module.exports = { .setTitle("Commands") .setTimestamp() .setAuthor(client.user.username, client.user.avatarURL({ dynamic: true, size: 4096 })) - + let start = 0; for (const file of commandFiles) { + if(iteration >= max_commands) break; const command = require(`../../${file}`); + if(command.disabled) continue; if (args[0] == "admin") { if (command.admin) commands = commands + `${prefix}${command.name} | ${command.description}\n` - }else if(!args[0]){ - if (!command.admin) - commands = commands + `${prefix}${command.name} | ${command.description}\n` + }else if(!args[0] || num_in_args){ + if (!command.admin){ + if(start < iteration){ + }else{ + added_commands++ + commands = commands + `${prefix}${command.name} | ${command.description}\n`; + iteration++ + } + start++; + } }else if(args[0] === command.name){ commands = commands + `${prefix}${command.name}\n` embed.setTitle(command.name.charAt(0).toUpperCase() + command.name.slice(1)) @@ -65,6 +88,10 @@ module.exports = { embed.addFields( { name: fieldName, value: commands }, ) + if(embed.fields[0].value.length > 1023){ + message.channel.send(`There are more than 1023 characters`) + }else { message.channel.send({embeds :[embed]}); + } }, }; \ No newline at end of file From 39ca3ee32290e0fa4ce35dda74055de9d05fd02a Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Sun, 24 Mar 2024 21:00:00 +0100 Subject: [PATCH 17/20] Fix mention not working as prefix --- server/message.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/message.js b/server/message.js index e629779..85d488d 100644 --- a/server/message.js +++ b/server/message.js @@ -8,12 +8,12 @@ module.exports = function(client, owners, message, globalPrefix){ prefix = serverPrefix; } - if (!message.guild || message.author.bot || !message.content.startsWith(prefix)) return; if(message.content.startsWith(`<@${client.user.id}>`)){ let regex = new RegExp("(<@" + client.user.id + ">) *") message.content = message.content.replace(regex, prefix); } + if (!message.guild || message.author.bot || !message.content.startsWith(prefix)) return; let args = message.content.slice(prefix.length).split(" ") if(args[0] !== "fm" && args[0].startsWith("fm")){ let firstElement = args[0]; From 7de04136e29e634072f70b2e6184c0e038e40e23 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Mon, 25 Mar 2024 00:45:06 +0100 Subject: [PATCH 18/20] Add invalid subcommand error --- commands/misc/fm.js | 1 + 1 file changed, 1 insertion(+) diff --git a/commands/misc/fm.js b/commands/misc/fm.js index faaeb3b..0524169 100644 --- a/commands/misc/fm.js +++ b/commands/misc/fm.js @@ -25,6 +25,7 @@ module.exports = { sendText = await getTopTracks(message.author.id, args); break; default: + sendText = `${args[0]} is not a valid subcommand.\nSee \`${prefix}help fm\` for more info.`; break; } if(args.length < 1){ From 12ba68e9d66539a1eb2ca57c4a5eb6379012b5b2 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 25 Mar 2024 07:44:58 +0100 Subject: [PATCH 19/20] Bump ip from 2.0.0 to 2.0.1 (#88) Bumps [ip](https://github.com/indutny/node-ip) from 2.0.0 to 2.0.1. - [Commits](https://github.com/indutny/node-ip/compare/v2.0.0...v2.0.1) --- updated-dependencies: - dependency-name: ip dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package-lock.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index cc76593..da4f375 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1052,9 +1052,9 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "node_modules/ip": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.0.tgz", - "integrity": "sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.1.tgz", + "integrity": "sha512-lJUL9imLTNi1ZfXT+DU6rBBdbiKGBuay9B6xGSPVjUeQwaH1RIGqef8RZkUtHioLmSNpPR5M4HVKJGm1j8FWVQ==", "optional": true }, "node_modules/ipaddr.js": { @@ -2959,9 +2959,9 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "ip": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.0.tgz", - "integrity": "sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.1.tgz", + "integrity": "sha512-lJUL9imLTNi1ZfXT+DU6rBBdbiKGBuay9B6xGSPVjUeQwaH1RIGqef8RZkUtHioLmSNpPR5M4HVKJGm1j8FWVQ==", "optional": true }, "ipaddr.js": { From 7bd728a16e910083ea2d0894965607f6423de3d5 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Mon, 8 Apr 2024 11:46:24 +0200 Subject: [PATCH 20/20] Improve default fm command, plus improve help --- commands/misc/fm.js | 23 ++++++++++++--------- util/lastfm/getCurrentScrobble.js | 33 +++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/commands/misc/fm.js b/commands/misc/fm.js index 0524169..aadeec6 100644 --- a/commands/misc/fm.js +++ b/commands/misc/fm.js @@ -1,6 +1,7 @@ const fmlogin = require("../../util/lastfm/fmlogin"); const getCurrentScrobble = require("../../util/lastfm/getCurrentScrobble"); const getTopTracks = require("../../util/lastfm/getTopTracks"); +const help = require("../info/help"); module.exports = { name: 'fm', description: 'Last fm commands. See `help fm` for more info.', @@ -10,27 +11,31 @@ module.exports = { "Get current scrobble: `fm`", "Get top tracks: `fmtt`" ], - async execute({ message, args, prefix }) { - let sendText = "Something went wrong."; + async execute({ message, args, prefix, client }) { + let sendText = {text: "Something went wrong.", embed: null}; switch (args[0]) { case "help": - sendText = this.moreHelp.join("\n").replace(//g, prefix); - break; + help.execute({ message: message, args: ["fm"], prefix: prefix, client: client }); + return; case "set": - sendText = await fmlogin(message.author.id, args[1]); + sendText.text = await fmlogin(message.author.id, args[1]); break; case "toptracks": case "tt": args.shift(); - sendText = await getTopTracks(message.author.id, args); + sendText.text = await getTopTracks(message.author.id, args); break; default: - sendText = `${args[0]} is not a valid subcommand.\nSee \`${prefix}help fm\` for more info.`; + sendText.text = `${args[0]} is not a valid subcommand.\nSee \`${prefix}help fm\` for more info.`; break; } if(args.length < 1){ - sendText = await getCurrentScrobble(message.author.id); + sendText = await getCurrentScrobble(message.author.id, message.guild); + } + if(sendText.embed != null){ + message.channel.send({embeds :[sendText.embed]}) + }else{ + message.channel.send(sendText.text); } - message.channel.send(sendText); } }; \ No newline at end of file diff --git a/util/lastfm/getCurrentScrobble.js b/util/lastfm/getCurrentScrobble.js index dbc7030..2d1f49b 100644 --- a/util/lastfm/getCurrentScrobble.js +++ b/util/lastfm/getCurrentScrobble.js @@ -1,8 +1,18 @@ +const getNickname = require("../getNickname"); +const parseMention = require("../parseMention"); const getFmUsername = require("./getFmUsername"); +const Discord = require('discord.js'); require("dotenv").config(); -module.exports = async function(userID) { - let sendText = ""; +module.exports = async function(userID, guild) { + let parse = parseMention(userID, guild) + let user = guild.members.cache.get(parse); + let nickname = getNickname(user, guild) + if(nickname == null){ + nickname = user.user.username; + } + let isCurrentScrobble = "Current"; + let sendText = {text: "", embed: null} let scrobble = {}; const apiKey = process.env.LAST_FM_API_KEY; let lastfmUsername = await getFmUsername(userID); @@ -16,6 +26,11 @@ module.exports = async function(userID) { scrobble.artist = track.artist["#text"]; scrobble.song = track.name; scrobble.album = track.album["#text"]; + scrobble.cover = track.image[3]["#text"]; + console.log(typeof track['@attr'].nowplaying); + if(track['@attr'].nowplaying === "true"){ + isCurrentScrobble = "Last"; + } resolve(scrobble); }) .catch(error => { @@ -23,9 +38,19 @@ module.exports = async function(userID) { reject(error); }); }); - sendText = `Currently scrobbling:\n${scrobble.artist} - ${scrobble.song}\nAlbum: ${scrobble.album}`; + const embed = new Discord.MessageEmbed() + .setAuthor(`Now playing - ${nickname}`, user.user.avatarURL({ dynamic: true, size: 4096 })) + .setThumbnail(scrobble.cover) + .setColor(15780145) + .addFields({ + name: "Current:", value: `${scrobble.song}\n **${scrobble.artist} • ** ${scrobble.album}` + }, + { + name: "Previous:", value: `**TODO: Make this show the previous scrobble**` + },) + sendText.embed = embed; } else { - sendText = "You haven't set your last.fm username yet. Use `fm set ` to set it."; + sendText.text = "You haven't set your last.fm username yet. Use `fm set ` to set it."; } return sendText; } \ No newline at end of file