Skip to content

Commit

Permalink
update: on react remove update user when reactions failed to remove, …
Browse files Browse the repository at this point in the history
…also extra logging
  • Loading branch information
Uhuh committed May 28, 2024
1 parent 2f8e74b commit 2dbb1ba
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions utilities/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { GET_CATEGORY_BY_ID, GET_ROLES_BY_CATEGORY_ID } from '../src/database/qu
import {
CREATE_REACT_MESSAGE,
DELETE_REACT_MESSAGES_BY_MESSAGE_ID,
GET_REACT_MESSAGE_BY_CATEGORY_ID,
GET_GUILD_REACT_MESSAGE_BY_CATEGORY_ID,
} from '../src/database/queries/reactMessage.query';
import { LogService } from '../src/services/logService';
import { CLIENT_ID } from '../src/vars';
Expand All @@ -31,10 +31,13 @@ export const reactToMessage = async (
isCustomMessage: boolean,
log: LogService,
) => {
log.debug(`Creating react message for message[${message.id}] for ${categoryRoles.length} category roles.`, guildId);

for (const role of categoryRoles) {
try {
await message.react(role.emojiId);

log.debug(`Creating react message with emoji[${role.emojiId}], role[${role.roleId}] and category[${categoryId}]`, guildId);
await CREATE_REACT_MESSAGE({
messageId: message.id,
emojiId: role.emojiId,
Expand Down Expand Up @@ -70,30 +73,40 @@ export const updateReactMessages = async (
type: ReactMessageUpdate,
) => {
try {
const reactMessage = await GET_REACT_MESSAGE_BY_CATEGORY_ID(categoryId);
const { guildId } = interaction;

if (!guildId) {
throw Error('GuildId was missing on interaction');
}

const reactMessage = await GET_GUILD_REACT_MESSAGE_BY_CATEGORY_ID(guildId, categoryId);

if (!reactMessage) {
return log.debug(`No react messages exist with category[${categoryId}]`);
}

const { guildId, channelId, messageId } = reactMessage;
/**
* Sometimes servers fail to give RoleBot MANAGE_MESSAGE perms which affects RoleBot's ability to remove reactions.
* Use this to alert the user that somewhere failed to update.
*/
let failedToRemoveReactions = false;

const { channelId, messageId, isCustomMessage } = reactMessage;

const channel = await interaction.guild?.channels
.fetch(channelId)
.catch(() => log.info(`Failed to fetch channel[${channelId}]`, guildId));

if (!(channel?.type === ChannelType.GuildText)) {
return log.debug(
`Guild[${guildId}] apparently does not have channel[${channelId}]`,
);
if (channel?.type !== ChannelType.GuildText) {
return log.info(`Channel[${channelId}] is not of GuildText type.`, guildId);
}

const message = await channel.messages
.fetch(messageId)
.catch(() => log.info(`Failed to fetch message[${messageId}]`, guildId));

if (!message) {
return log.debug(
return log.info(
`Could not find message[${messageId}] in channel[${channelId}] in guild[${guildId}]`,
);
}
Expand All @@ -110,13 +123,14 @@ export const updateReactMessages = async (
categoryId,
category.displayOrder,
);

const embed = reactRoleEmbed(categoryRoles, category);

/**
* The /react message command allows users to use their own message instead of RoleBots
* Make sure this is not their message. We cannot edit user messages.
*/
if (!reactMessage.isCustomMessage) {
if (!isCustomMessage) {
await message
.edit({
embeds: [embed],
Expand All @@ -133,22 +147,35 @@ export const updateReactMessages = async (
/**
* Clear all reactions first. If RoleBot is missing permission then we don't want to delete react message by ID.
*/
await message.reactions.removeAll();
await message.reactions.removeAll().catch(() => {
failedToRemoveReactions = true;
log.error(`Failed to remove all reactions for message[${messageId}]`, guildId);
});

// Remove all react messages since they are created and depend on RoleBots reactions
log.info(`Purging existing react messages by message id[${messageId}]`, guildId);
await DELETE_REACT_MESSAGES_BY_MESSAGE_ID(messageId);

// Re-react to the message with the updated react role list.
log.info(`Reacting to message[${messageId}] for category[${categoryId}]`, guildId);
await reactToMessage(
message,
guildId,
categoryRoles,
channel.id,
categoryId,
reactMessage.isCustomMessage,
isCustomMessage,
log,
);
}

if (failedToRemoveReactions) {
const messageLink = `https://discord.com/channels/${guildId}/${channelId}/${messageId}`;
log.debug(`Informing user RoleBot failed to update the message properly.`, guildId);

await interaction.followUp({
content: `I failed to update the messages reactions. This is usually caused by not giving me "MANAGE_MESSAGE" permissions in a channel.\n\nThe message I tried updating: ${messageLink}`,
ephemeral: true,
});
}
} catch (e) {
log.error(
`Caught an error updating reaction messages.\n${e}`,
Expand Down

0 comments on commit 2dbb1ba

Please sign in to comment.