Skip to content

Commit

Permalink
update: code styling and debug log for reactions / buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhuh committed May 29, 2024
1 parent 2dbb1ba commit e5b9024
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 98 deletions.
59 changes: 18 additions & 41 deletions src/services/buttonHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,53 +28,37 @@ export class ButtonHandler {
* This can happen if users swap types and leave old messages up.
*/
if (config?.reactType !== GuildReactType.button) {
return interaction.editReply(
`Hey! The server doesn't use button roles! The button you clicked must be out of date.`,
);
this.log.debug(`User pressed button when guild config isn't button.`, guildId);
return interaction.editReply(`Hey! The server doesn't use button roles! The button you clicked must be out of date.`);
}

const [reactRoleId, categoryId] = args;

if (isNaN(Number(reactRoleId)) || isNaN(Number(categoryId))) {
return interaction.editReply(
`Hey! Something went wrong with processing that button press! reactRoleId: ${reactRoleId} | categoryId: ${categoryId}`,
);
this.log.error(`Invalid reactRoleId[${reactRoleId}] or categoryId[${categoryId}] for button press.`, guildId);
return interaction.editReply(`Hey! Something went wrong with processing that button press! reactRoleId: ${reactRoleId} | categoryId: ${categoryId}`);
}

const reactRole = await GET_REACT_ROLE_BY_ID(Number(reactRoleId));
const category = await GET_CATEGORY_BY_ID(Number(categoryId));

if (!reactRole || !category) {
return interaction.editReply(
`Hey! I failed to find the react role or category related to this button.`,
);
this.log.debug(`ReactRole[${reactRole}] of Category[${categoryId}] could not be found`, guildId);
return interaction.editReply(`Hey! I failed to find the react role or category related to this button.`);
}

const member = await interaction.guild?.members
.fetch(interaction.user.id)
.catch((e) =>
this.log.error(
`Fetching user[${interaction.user.id}] threw an error.\n${e}`,
),
);
.catch(e => this.log.error(`Fetching user[${interaction.user.id}] threw an error.\n${e}`));

if (!member) {
this.log.debug(
`Failed to grab member object from interaction.`,
interaction.guildId,
);
this.log.debug(`Failed to grab member object from interaction.`, interaction.guildId);

return interaction.editReply(
`Heyo! I had some issues finding _you_ here.`,
);
return interaction.editReply(`Heyo! I had some issues finding _you_ here.`);
}

// If the category limits who can react and the user doesn't have said role ignore request.
if (
category.requiredRoleId &&
!member.roles.cache.has(category.requiredRoleId)
) {
// Remove reaction as to not confuse the user that they succeeded.
if (category.requiredRoleId && !member.roles.cache.has(category.requiredRoleId)) {
return interaction.editReply(
`Hey! You lack the required role to get anything from this category. ${RolePing(
category.requiredRoleId,
Expand All @@ -83,10 +67,7 @@ export class ButtonHandler {
}

// If the category has an excluded role, and the user has said excluded role, ignore request.
if (
category.excludedRoleId &&
member.roles.cache.has(category.excludedRoleId)
) {
if (category.excludedRoleId && member.roles.cache.has(category.excludedRoleId)) {
return interaction.editReply(
`Heyo! You have a role that prevents you from obtaining anything from this category. ${RolePing(
category.excludedRoleId,
Expand All @@ -110,6 +91,8 @@ export class ButtonHandler {
);
};

this.log.debug(`User[${member.id}] pressed button for ReactRole[${reactRoleId}] Category[${categoryId}]`, guildId);

// Remove the role if the user has it. Should catch mutually exclusive removes too.
if (member.roles.cache.has(reactRole.roleId)) {
member.roles
Expand Down Expand Up @@ -158,33 +141,27 @@ export class ButtonHandler {
);

await member.roles.remove(rolesToRemove).catch((e) => {
this.log.error(
`Failed to remove roles[${rolesToRemove}] from user[${member.id}]\n${e}`,
);
this.log.error(`Failed to remove roles[${rolesToRemove}] from user[${member.id}]\n${e}`);

return interaction.editReply(
`Hey! I couldn't remove some mutually exclusive roles from you. Do I have the manage role permission?`,
);
return interaction.editReply(`Hey! I couldn't remove some mutually exclusive roles from you. Do I have the manage role permission?`);
});

await member.roles.add(role.roleId).catch((e) => {
this.log.error(
`Failed to add role[${role.roleId}] from user[${member.id}]\n${e}`,
);
this.log.error(`Failed to add role[${role.roleId}] from user[${member.id}]\n${e}`);

return interaction.editReply(
`Hey! I couldn't add the role ${RolePing(
role.roleId,
)}. Do I the manage roles permission?`,
);
});

const removedRoles = ` and removed ${rolesToRemove.map((r) => RolePing(r.id)).join(' ')}`;

return interaction.editReply(
`Hey! I gave you the ${RolePing(
role.roleId,
)} role${ rolesToRemove.size ? removedRoles : '.'}`,
)} role${rolesToRemove.size ? removedRoles : '.'}`,
);
};
}
71 changes: 14 additions & 57 deletions src/services/reactionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ export class ReactionHandler {
const emojiId = emoji.id || emoji.name;

if (!emojiId) {
return this.log.debug(
`Emoji doesn't exist on message[${message.id}] reaction`,
guild.id,
);
return this.log.debug(`Emoji doesn't exist on message[${message.id}] reaction`, guild.id);
}

const reactMessage = await GET_REACT_MESSAGE_BY_MSGID_AND_EMOJI_ID(
Expand All @@ -40,38 +37,24 @@ export class ReactionHandler {
this.log.error(`Failed to query for react message.\n${e}`, guild.id),
);

if (!reactMessage) return;

if (!reactMessage.categoryId) {
return this.log.error(
`React role[${reactMessage.id}] in guild[${guild.id}] does NOT have a category set.`,
guild.id,
);
if (!reactMessage) {
return;
}

this.log.debug(`User[${user.id}] reacted with[${emojiId}]`, guild.id);

const member = await guild.members
.fetch(user.id)
.catch((e) =>
this.log.error(
`Fetching user[${user.id}] threw an error.\n${e}`,
guild.id,
),
);
.catch((e) => this.log.error(`Fetching user[${user.id}] threw an error.\n${e}`, guild.id));

if (!member) {
return this.log.debug(
`Failed to fetch member with user[${user.id}] for reaction[${type}]`,
guild.id,
);
return this.log.debug(`Failed to fetch member with user[${user.id}] for reaction[${type}]`, guild.id);
}

const category = await GET_CATEGORY_BY_ID(reactMessage.categoryId);

if (!category) {
return this.log.error(
`Category[${reactMessage.categoryId}] does not exist`,
guild.id,
);
return this.log.error(`Category[${reactMessage.categoryId}] does not exist`, guild.id);
}

// If the category limits who can react and the user doesn't have said role ignore request.
Expand Down Expand Up @@ -103,22 +86,12 @@ export class ReactionHandler {
case 'add':
member.roles
.add(reactMessage.roleId)
.catch((e) =>
this.log.debug(
`Cannot give role[${reactMessage.roleId}] to user[${member?.id}]\n${e}`,
guild.id,
),
);
.catch((e) => this.log.debug(`Cannot give role[${reactMessage.roleId}] to user[${member?.id}]\n${e}`, guild.id));
break;
case 'remove':
member.roles
.remove(reactMessage.roleId)
.catch((e) =>
this.log.debug(
`Cannot remove role[${reactMessage.roleId}] from user[${member?.id}]\n${e}`,
guild.id,
),
);
.catch((e) => this.log.debug(`Cannot remove role[${reactMessage.roleId}] from user[${member?.id}]\n${e}`, guild.id));
}
};

Expand All @@ -138,19 +111,11 @@ export class ReactionHandler {
if (type === 'remove') {
return member.roles
.remove(reactMessage.roleId)
.catch((e) =>
this.log.error(
`Failed to remove role[${reactMessage.roleId}] from user[${member.id}]\n${e}`,
guild.id,
),
);
.catch(e => this.log.error(`Failed to remove role[${reactMessage.roleId}] from user[${member.id}]\n${e}`, guild.id));
}

if (!reactMessage.categoryId) {
return this.log.error(
`React role[${reactMessage.id}] category is undefined.`,
guild.id,
);
return this.log.error(`React role[${reactMessage.id}] category is undefined.`, guild.id);
}

const categoryRoles = (
Expand All @@ -168,18 +133,10 @@ export class ReactionHandler {
// Fetch the role we want to ADD to the user.
const role = await guild.roles
.fetch(reactMessage.roleId)
.catch((e) =>
this.log.error(
`Failed to fetch role[${reactMessage.roleId}]\n${e}`,
guild.id,
),
);
.catch((e) => this.log.error(`Failed to fetch role[${reactMessage.roleId}]\n${e}`, guild.id));

if (!role) {
return this.log.debug(
`Role[${reactMessage.roleId}] could not be found`,
guild.id,
);
return this.log.debug(`Role[${reactMessage.roleId}] could not be found`, guild.id);
}

// Because this is the role we want to give the user we need to set it.
Expand Down

0 comments on commit e5b9024

Please sign in to comment.