Implement error handling for rolemenu

This commit is contained in:
Skylar Grant 2023-06-03 14:42:44 -04:00
parent 3c01d37bd5
commit e075a3a5d6
3 changed files with 46 additions and 17 deletions

View File

@ -54,7 +54,12 @@
"invalidSubcommand": "Invalid subcommand detected.",
"noTreeMessage": "</tree:0> - Make sure you've sent or refreshed a Tree recently.",
"noLeaderboardMessage": "</top trees:0> - Make sure you've sent or refreshed the Tallest Trees leaderboard recently.",
"noCompareMessage": "</compare:0> - This is awkward, I've lost my own comparison message!"
"noCompareMessage": "</compare:0> - 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 </setupinfo:1065413032374706196> to verify. Run </compare:1065346941166297128> to get started!",

10
main.js
View File

@ -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 => {

View File

@ -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: {