3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

Better errors for reaction roles

This commit is contained in:
Dragory 2020-08-28 01:30:25 +03:00
parent fca8a8dcce
commit b53acd6b84
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
2 changed files with 57 additions and 17 deletions

View file

@ -102,9 +102,19 @@ export const InitReactionRolesCmd = reactionRolesCmd({
// Apply the reactions themselves // Apply the reactions themselves
const reactionRoles = await pluginData.state.reactionRoles.getForMessage(targetMessage.id); 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); (await progressMessage).delete().catch(noop);
}, },
}); });

View file

@ -9,15 +9,21 @@ import { LogType } from "../../../data/LogType";
const CLEAR_ROLES_EMOJI = "❌"; const CLEAR_ROLES_EMOJI = "❌";
/**
* @return Errors encountered while applying reaction roles, if any
*/
export async function applyReactionRoleReactionsToMessage( export async function applyReactionRoleReactionsToMessage(
pluginData: PluginData<ReactionRolesPluginType>, pluginData: PluginData<ReactionRolesPluginType>,
channelId: string, channelId: string,
messageId: string, messageId: string,
reactionRoles: ReactionRole[], reactionRoles: ReactionRole[],
) { ): Promise<string[]> {
const channel = pluginData.guild.channels.get(channelId) as TextChannel; const channel = pluginData.guild.channels.get(channelId) as TextChannel;
if (!channel) return; if (!channel) return;
const errors = [];
const logs = pluginData.getPlugin(LogsPlugin);
let targetMessage; let targetMessage;
try { try {
targetMessage = await channel.getMessage(messageId); targetMessage = await channel.getMessage(messageId);
@ -25,24 +31,38 @@ export async function applyReactionRoleReactionsToMessage(
if (isDiscordRESTError(e)) { if (isDiscordRESTError(e)) {
if (e.code === 10008) { if (e.code === 10008) {
// Unknown message, remove reaction roles from the message // Unknown message, remove reaction roles from the message
logger.warn( logs.log(LogType.BOT_ALERT, {
`Removed reaction roles from unknown message ${channelId}/${messageId} in guild ${pluginData.guild.name} (${pluginData.guild.id})`, body: `Removed reaction roles from unknown message ${channelId}/${messageId} (${pluginData.guild.id})`,
); });
await pluginData.state.reactionRoles.removeFromMessage(messageId); await pluginData.state.reactionRoles.removeFromMessage(messageId);
} else { } else {
logger.warn( logs.log(LogType.BOT_ALERT, {
`Error when applying reaction roles to message ${channelId}/${messageId} in guild ${pluginData.guild.name} (${pluginData.guild.id}), error code ${e.code}`, 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 { } else {
throw e; throw e;
} }
} }
// Remove old reactions, if any // 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); await sleep(1500);
// Add reaction role reactions // Add reaction role reactions
@ -54,12 +74,20 @@ export async function applyReactionRoleReactionsToMessage(
await sleep(1250); // Make sure we don't hit rate limits await sleep(1250); // Make sure we don't hit rate limits
} catch (e) { } catch (e) {
if (isDiscordRESTError(e) && e.code === 10014) { if (isDiscordRESTError(e) && e.code === 10014) {
pluginData.state.reactionRoles.removeFromMessage(messageId, rr.emoji); if (e.code === 10014) {
const logs = pluginData.getPlugin(LogsPlugin); pluginData.state.reactionRoles.removeFromMessage(messageId, rr.emoji);
logs.log(LogType.BOT_ALERT, { errors.push(`Unknown emoji: ${emoji}`);
body: `Could not add unknown reaction role emoji ${emoji} to message ${channelId}/${messageId}`, logs.log(LogType.BOT_ALERT, {
}); body: `Could not add unknown reaction role emoji ${emoji} to message ${channelId}/${messageId}`,
continue; });
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; throw e;
@ -68,4 +96,6 @@ export async function applyReactionRoleReactionsToMessage(
// Add the "clear reactions" button // Add the "clear reactions" button
await targetMessage.addReaction(CLEAR_ROLES_EMOJI); await targetMessage.addReaction(CLEAR_ROLES_EMOJI);
return errors;
} }