From e075a3a5d63061c30901c8eb30c694910c940706 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Jun 2023 14:42:44 -0400 Subject: [PATCH] Implement error handling for rolemenu --- data/strings.json | 7 ++++++- main.js | 10 +++++++++- modules/functions.js | 46 +++++++++++++++++++++++++++++--------------- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/data/strings.json b/data/strings.json index d93d8fd..11a213f 100755 --- a/data/strings.json +++ b/data/strings.json @@ -54,7 +54,12 @@ "invalidSubcommand": "Invalid subcommand detected.", "noTreeMessage": " - Make sure you've sent or refreshed a Tree recently.", "noLeaderboardMessage": " - Make sure you've sent or refreshed the Tallest Trees leaderboard recently.", - "noCompareMessage": " - This is awkward, I've lost my own comparison message!" + "noCompareMessage": " - This is awkward, I've lost my own comparison message!", + "noFetchRole": "I was unable to find that role, please make sure it exists and I have access to it.", + "noGiveRole": "I was unable to give that role to you, please make sure I have permission to `Manage Roles` and that the role is below my role in the server settings role list.", + "noTakeRole": "I was unable to remove that role from you, please make sure I have permission to `Manage Roles` and that the role is below my role in the server settings role list.", + "yesGiveRole": "Successfully added the role to your profile!", + "yesTakeRole": "Successfully removed the role from your profile!" }, "status": { "treeAndLeaderboard": "Tree and leaderboard messages were both found, setup is complete. Run to verify. Run to get started!", diff --git a/main.js b/main.js index 17609f3..67ee99c 100755 --- a/main.js +++ b/main.js @@ -89,7 +89,15 @@ client.on('interactionCreate', async interaction => { }); client.on('messageUpdate', async (oldMessage, message) => { - await fn.messages.updateHandler(message).catch(e => console.error(e)); + await fn.messages.updateHandler(message).catch(async e => { + switch (e) { + case strings.error.noCompareMessage: + await message.channel.send(strings.error.noCompareMessage); + break; + default: + break; + } + }); }); client.on('messageCreate', async message => { diff --git a/modules/functions.js b/modules/functions.js index 55ed60d..f9aa9de 100755 --- a/modules/functions.js +++ b/modules/functions.js @@ -751,18 +751,24 @@ const functions = { async fruitPing(interaction) { if (interaction.client.guildInfos.has(interaction.guildId)) { let guildInfo = interaction.client.guildInfos.get(interaction.guildId); - const role = await functions.roles.fetchRole(interaction.guild, guildInfo.fruitRoleId); let status = "No Changes Made"; + let errorFlag = false; + const role = await functions.roles.fetchRole(interaction.guild, guildInfo.fruitRoleId).catch(e => { + errorFlag = true; + status = strings.error.noFetchRole; + }); if (interaction.member.roles.cache.some(role => role.id == guildInfo.fruitRoleId)) { - await functions.roles.takeRole(interaction.member, role); - status = "Removed the fruit role."; + await functions.roles.takeRole(interaction.member, role).catch(e => { + errorFlag = true; + status = strings.error.noTakeRole; + }); + if(!errorFlag) status = strings.error.yesTakeRole; } else { await functions.roles.giveRole(interaction.member, role).catch(e => { - const errorId = functions.generateErrorId(); - console.error(errorId + " " + e); - status = `Error adding the fruit role: ${errorId}`; + errorFlag = true; + status = strings.error.noGiveRole; }); - status = "Added the fruit role."; + if (!errorFlag) status = strings.error.yesGiveRole; } return functions.builders.embed(status); } else { @@ -773,13 +779,23 @@ const functions = { if (interaction.client.guildInfos.has(interaction.guildId)) { let guildInfo = interaction.client.guildInfos.get(interaction.guildId); let status = "No Changes Made"; - const role = await functions.roles.fetchRole(interaction.guild, guildInfo.waterRoleId); + let errorFlag = false; + const role = await functions.roles.fetchRole(interaction.guild, guildInfo.waterRoleId).catch(e => { + errorFlag = true; + status = strings.error.noFetchRole; + }); if (interaction.member.roles.cache.some(role => role.id == guildInfo.waterRoleId)) { - await functions.roles.takeRole(interaction.member, role); - status = "Removed the water role."; + await functions.roles.takeRole(interaction.member, role).catch(e => { + errorFlag = true; + status = strings.error.noTakeRole; + }); + if (!errorFlag) status = strings.error.yesTakeRole; } else { - await functions.roles.giveRole(interaction.member, role); - status = "Added the water role."; + await functions.roles.giveRole(interaction.member, role).catch(e => { + errorFlag = true; + status = strings.error.noGiveRole; + }); + if (!errorFlag) status = strings.error.yesGiveRole; } return functions.builders.embed(status); } else { @@ -789,13 +805,13 @@ const functions = { }, roles: { async fetchRole(guild, roleId) { - return await guild.roles.fetch(roleId).catch(err => console.error("Error fetching the role: " + err + "\n" + roleId)); + return await guild.roles.fetch(roleId); }, async giveRole(member, role) { - await member.roles.add(role).catch(err => console.error(`Error giving role: ${err}\nRole Info: ${role.name} (${role.guild}: ${member.guild.name})`)); + await member.roles.add(role); }, async takeRole(member, role) { - await member.roles.remove(role).catch(err => console.error(`Error removing role: ${err}\nRole Info: ${role.name} (${role.guild}: ${member.guild.name})`)); + await member.roles.remove(role); } }, collectors: {