Better errors for reaction roles
This commit is contained in:
parent
fca8a8dcce
commit
b53acd6b84
2 changed files with 57 additions and 17 deletions
|
@ -102,9 +102,19 @@ export const InitReactionRolesCmd = reactionRolesCmd({
|
|||
|
||||
// Apply the reactions themselves
|
||||
const reactionRoles = await pluginData.state.reactionRoles.getForMessage(targetMessage.id);
|
||||
await applyReactionRoleReactionsToMessage(pluginData, targetMessage.channel.id, targetMessage.id, reactionRoles);
|
||||
const errors = await applyReactionRoleReactionsToMessage(
|
||||
pluginData,
|
||||
targetMessage.channel.id,
|
||||
targetMessage.id,
|
||||
reactionRoles,
|
||||
);
|
||||
|
||||
if (errors.length) {
|
||||
sendErrorMessage(pluginData, msg.channel, `Errors while adding reaction roles:\n${errors.join("\n")}`);
|
||||
} else {
|
||||
sendSuccessMessage(pluginData, msg.channel, "Reaction roles added");
|
||||
}
|
||||
|
||||
sendSuccessMessage(pluginData, msg.channel, "Reaction roles added");
|
||||
(await progressMessage).delete().catch(noop);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -9,15 +9,21 @@ import { LogType } from "../../../data/LogType";
|
|||
|
||||
const CLEAR_ROLES_EMOJI = "❌";
|
||||
|
||||
/**
|
||||
* @return Errors encountered while applying reaction roles, if any
|
||||
*/
|
||||
export async function applyReactionRoleReactionsToMessage(
|
||||
pluginData: PluginData<ReactionRolesPluginType>,
|
||||
channelId: string,
|
||||
messageId: string,
|
||||
reactionRoles: ReactionRole[],
|
||||
) {
|
||||
): Promise<string[]> {
|
||||
const channel = pluginData.guild.channels.get(channelId) as TextChannel;
|
||||
if (!channel) return;
|
||||
|
||||
const errors = [];
|
||||
const logs = pluginData.getPlugin(LogsPlugin);
|
||||
|
||||
let targetMessage;
|
||||
try {
|
||||
targetMessage = await channel.getMessage(messageId);
|
||||
|
@ -25,24 +31,38 @@ export async function applyReactionRoleReactionsToMessage(
|
|||
if (isDiscordRESTError(e)) {
|
||||
if (e.code === 10008) {
|
||||
// Unknown message, remove reaction roles from the message
|
||||
logger.warn(
|
||||
`Removed reaction roles from unknown message ${channelId}/${messageId} in guild ${pluginData.guild.name} (${pluginData.guild.id})`,
|
||||
);
|
||||
logs.log(LogType.BOT_ALERT, {
|
||||
body: `Removed reaction roles from unknown message ${channelId}/${messageId} (${pluginData.guild.id})`,
|
||||
});
|
||||
await pluginData.state.reactionRoles.removeFromMessage(messageId);
|
||||
} else {
|
||||
logger.warn(
|
||||
`Error when applying reaction roles to message ${channelId}/${messageId} in guild ${pluginData.guild.name} (${pluginData.guild.id}), error code ${e.code}`,
|
||||
);
|
||||
logs.log(LogType.BOT_ALERT, {
|
||||
body: `Error ${e.code} when applying reaction roles to message ${channelId}/${messageId}: ${e.message}`,
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
errors.push(`Error ${e.code} while fetching reaction role message: ${e.message}`);
|
||||
return errors;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove old reactions, if any
|
||||
await targetMessage.removeReactions();
|
||||
try {
|
||||
await targetMessage.removeReactions();
|
||||
} catch (e) {
|
||||
if (isDiscordRESTError(e)) {
|
||||
errors.push(`Error ${e.code} while removing old reactions: ${e.message}`);
|
||||
logs.log(LogType.BOT_ALERT, {
|
||||
body: `Error ${e.code} while removing old reaction role reactions from message ${channelId}/${messageId}: ${e.message}`,
|
||||
});
|
||||
return errors;
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
|
||||
await sleep(1500);
|
||||
|
||||
// Add reaction role reactions
|
||||
|
@ -54,12 +74,20 @@ export async function applyReactionRoleReactionsToMessage(
|
|||
await sleep(1250); // Make sure we don't hit rate limits
|
||||
} catch (e) {
|
||||
if (isDiscordRESTError(e) && e.code === 10014) {
|
||||
pluginData.state.reactionRoles.removeFromMessage(messageId, rr.emoji);
|
||||
const logs = pluginData.getPlugin(LogsPlugin);
|
||||
logs.log(LogType.BOT_ALERT, {
|
||||
body: `Could not add unknown reaction role emoji ${emoji} to message ${channelId}/${messageId}`,
|
||||
});
|
||||
continue;
|
||||
if (e.code === 10014) {
|
||||
pluginData.state.reactionRoles.removeFromMessage(messageId, rr.emoji);
|
||||
errors.push(`Unknown emoji: ${emoji}`);
|
||||
logs.log(LogType.BOT_ALERT, {
|
||||
body: `Could not add unknown reaction role emoji ${emoji} to message ${channelId}/${messageId}`,
|
||||
});
|
||||
continue;
|
||||
} else if (e.code === 50013) {
|
||||
errors.push(`Missing permissions to apply reactions`);
|
||||
logs.log(LogType.BOT_ALERT, {
|
||||
body: `Error ${e.code} while applying reaction role reactions to ${channelId}/${messageId}: ${e.message}`,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
throw e;
|
||||
|
@ -68,4 +96,6 @@ export async function applyReactionRoleReactionsToMessage(
|
|||
|
||||
// Add the "clear reactions" button
|
||||
await targetMessage.addReaction(CLEAR_ROLES_EMOJI);
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue