Skip to content

Commit e5b9024

Browse files
committedMay 29, 2024·
update: code styling and debug log for reactions / buttons
1 parent 2dbb1ba commit e5b9024

File tree

2 files changed

+32
-98
lines changed

2 files changed

+32
-98
lines changed
 

‎src/services/buttonHandler.ts

+18-41
Original file line numberDiff line numberDiff line change
@@ -28,53 +28,37 @@ export class ButtonHandler {
2828
* This can happen if users swap types and leave old messages up.
2929
*/
3030
if (config?.reactType !== GuildReactType.button) {
31-
return interaction.editReply(
32-
`Hey! The server doesn't use button roles! The button you clicked must be out of date.`,
33-
);
31+
this.log.debug(`User pressed button when guild config isn't button.`, guildId);
32+
return interaction.editReply(`Hey! The server doesn't use button roles! The button you clicked must be out of date.`);
3433
}
3534

3635
const [reactRoleId, categoryId] = args;
3736

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

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

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

5350
const member = await interaction.guild?.members
5451
.fetch(interaction.user.id)
55-
.catch((e) =>
56-
this.log.error(
57-
`Fetching user[${interaction.user.id}] threw an error.\n${e}`,
58-
),
59-
);
52+
.catch(e => this.log.error(`Fetching user[${interaction.user.id}] threw an error.\n${e}`));
6053

6154
if (!member) {
62-
this.log.debug(
63-
`Failed to grab member object from interaction.`,
64-
interaction.guildId,
65-
);
55+
this.log.debug(`Failed to grab member object from interaction.`, interaction.guildId);
6656

67-
return interaction.editReply(
68-
`Heyo! I had some issues finding _you_ here.`,
69-
);
57+
return interaction.editReply(`Heyo! I had some issues finding _you_ here.`);
7058
}
7159

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

8569
// If the category has an excluded role, and the user has said excluded role, ignore request.
86-
if (
87-
category.excludedRoleId &&
88-
member.roles.cache.has(category.excludedRoleId)
89-
) {
70+
if (category.excludedRoleId && member.roles.cache.has(category.excludedRoleId)) {
9071
return interaction.editReply(
9172
`Heyo! You have a role that prevents you from obtaining anything from this category. ${RolePing(
9273
category.excludedRoleId,
@@ -110,6 +91,8 @@ export class ButtonHandler {
11091
);
11192
};
11293

94+
this.log.debug(`User[${member.id}] pressed button for ReactRole[${reactRoleId}] Category[${categoryId}]`, guildId);
95+
11396
// Remove the role if the user has it. Should catch mutually exclusive removes too.
11497
if (member.roles.cache.has(reactRole.roleId)) {
11598
member.roles
@@ -158,33 +141,27 @@ export class ButtonHandler {
158141
);
159142

160143
await member.roles.remove(rolesToRemove).catch((e) => {
161-
this.log.error(
162-
`Failed to remove roles[${rolesToRemove}] from user[${member.id}]\n${e}`,
163-
);
144+
this.log.error(`Failed to remove roles[${rolesToRemove}] from user[${member.id}]\n${e}`);
164145

165-
return interaction.editReply(
166-
`Hey! I couldn't remove some mutually exclusive roles from you. Do I have the manage role permission?`,
167-
);
146+
return interaction.editReply(`Hey! I couldn't remove some mutually exclusive roles from you. Do I have the manage role permission?`);
168147
});
169148

170149
await member.roles.add(role.roleId).catch((e) => {
171-
this.log.error(
172-
`Failed to add role[${role.roleId}] from user[${member.id}]\n${e}`,
173-
);
150+
this.log.error(`Failed to add role[${role.roleId}] from user[${member.id}]\n${e}`);
174151

175152
return interaction.editReply(
176153
`Hey! I couldn't add the role ${RolePing(
177154
role.roleId,
178155
)}. Do I the manage roles permission?`,
179156
);
180157
});
181-
158+
182159
const removedRoles = ` and removed ${rolesToRemove.map((r) => RolePing(r.id)).join(' ')}`;
183160

184161
return interaction.editReply(
185162
`Hey! I gave you the ${RolePing(
186163
role.roleId,
187-
)} role${ rolesToRemove.size ? removedRoles : '.'}`,
164+
)} role${rolesToRemove.size ? removedRoles : '.'}`,
188165
);
189166
};
190167
}

‎src/services/reactionHandler.ts

+14-57
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ export class ReactionHandler {
2727
const emojiId = emoji.id || emoji.name;
2828

2929
if (!emojiId) {
30-
return this.log.debug(
31-
`Emoji doesn't exist on message[${message.id}] reaction`,
32-
guild.id,
33-
);
30+
return this.log.debug(`Emoji doesn't exist on message[${message.id}] reaction`, guild.id);
3431
}
3532

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

43-
if (!reactMessage) return;
44-
45-
if (!reactMessage.categoryId) {
46-
return this.log.error(
47-
`React role[${reactMessage.id}] in guild[${guild.id}] does NOT have a category set.`,
48-
guild.id,
49-
);
40+
if (!reactMessage) {
41+
return;
5042
}
43+
44+
this.log.debug(`User[${user.id}] reacted with[${emojiId}]`, guild.id);
5145

5246
const member = await guild.members
5347
.fetch(user.id)
54-
.catch((e) =>
55-
this.log.error(
56-
`Fetching user[${user.id}] threw an error.\n${e}`,
57-
guild.id,
58-
),
59-
);
48+
.catch((e) => this.log.error(`Fetching user[${user.id}] threw an error.\n${e}`, guild.id));
6049

6150
if (!member) {
62-
return this.log.debug(
63-
`Failed to fetch member with user[${user.id}] for reaction[${type}]`,
64-
guild.id,
65-
);
51+
return this.log.debug(`Failed to fetch member with user[${user.id}] for reaction[${type}]`, guild.id);
6652
}
6753

6854
const category = await GET_CATEGORY_BY_ID(reactMessage.categoryId);
6955

7056
if (!category) {
71-
return this.log.error(
72-
`Category[${reactMessage.categoryId}] does not exist`,
73-
guild.id,
74-
);
57+
return this.log.error(`Category[${reactMessage.categoryId}] does not exist`, guild.id);
7558
}
7659

7760
// If the category limits who can react and the user doesn't have said role ignore request.
@@ -103,22 +86,12 @@ export class ReactionHandler {
10386
case 'add':
10487
member.roles
10588
.add(reactMessage.roleId)
106-
.catch((e) =>
107-
this.log.debug(
108-
`Cannot give role[${reactMessage.roleId}] to user[${member?.id}]\n${e}`,
109-
guild.id,
110-
),
111-
);
89+
.catch((e) => this.log.debug(`Cannot give role[${reactMessage.roleId}] to user[${member?.id}]\n${e}`, guild.id));
11290
break;
11391
case 'remove':
11492
member.roles
11593
.remove(reactMessage.roleId)
116-
.catch((e) =>
117-
this.log.debug(
118-
`Cannot remove role[${reactMessage.roleId}] from user[${member?.id}]\n${e}`,
119-
guild.id,
120-
),
121-
);
94+
.catch((e) => this.log.debug(`Cannot remove role[${reactMessage.roleId}] from user[${member?.id}]\n${e}`, guild.id));
12295
}
12396
};
12497

@@ -138,19 +111,11 @@ export class ReactionHandler {
138111
if (type === 'remove') {
139112
return member.roles
140113
.remove(reactMessage.roleId)
141-
.catch((e) =>
142-
this.log.error(
143-
`Failed to remove role[${reactMessage.roleId}] from user[${member.id}]\n${e}`,
144-
guild.id,
145-
),
146-
);
114+
.catch(e => this.log.error(`Failed to remove role[${reactMessage.roleId}] from user[${member.id}]\n${e}`, guild.id));
147115
}
148116

149117
if (!reactMessage.categoryId) {
150-
return this.log.error(
151-
`React role[${reactMessage.id}] category is undefined.`,
152-
guild.id,
153-
);
118+
return this.log.error(`React role[${reactMessage.id}] category is undefined.`, guild.id);
154119
}
155120

156121
const categoryRoles = (
@@ -168,18 +133,10 @@ export class ReactionHandler {
168133
// Fetch the role we want to ADD to the user.
169134
const role = await guild.roles
170135
.fetch(reactMessage.roleId)
171-
.catch((e) =>
172-
this.log.error(
173-
`Failed to fetch role[${reactMessage.roleId}]\n${e}`,
174-
guild.id,
175-
),
176-
);
136+
.catch((e) => this.log.error(`Failed to fetch role[${reactMessage.roleId}]\n${e}`, guild.id));
177137

178138
if (!role) {
179-
return this.log.debug(
180-
`Role[${reactMessage.roleId}] could not be found`,
181-
guild.id,
182-
);
139+
return this.log.debug(`Role[${reactMessage.roleId}] could not be found`, guild.id);
183140
}
184141

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

0 commit comments

Comments
 (0)
Please sign in to comment.