Skip to content

Commit ac7add1

Browse files
display a message everytime user runs mention-each command
1 parent b54d9c3 commit ac7add1

File tree

5 files changed

+79
-22
lines changed

5 files changed

+79
-22
lines changed

src/constants/commands.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export const MENTION_EACH = {
4444
type: 3,
4545
require: false,
4646
},
47+
{
48+
name: "dev",
49+
description: "want to tag them individually?",
50+
type: 5,
51+
require: false,
52+
},
4753
],
4854
};
4955

src/controllers/mentionEachUser.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { env } from "../typeDefinitions/default.types";
66
import {
77
UserArray,
88
MentionEachUserOptions,
9+
DevFlag,
910
} from "../typeDefinitions/filterUsersByRole";
1011
import { mentionEachUserInMessage } from "../utils/guildRole";
1112
import { checkDisplayType } from "../utils/checkDisplayType";
@@ -15,43 +16,41 @@ export async function mentionEachUser(
1516
roleToBeTaggedObj: MentionEachUserOptions;
1617
displayMessageObj?: MentionEachUserOptions;
1718
channelId: number;
19+
dev?: DevFlag;
1820
},
1921
env: env,
2022
ctx: ExecutionContext
2123
) {
2224
const getMembersInServerResponse = await getMembersInServer(env);
2325
const roleId = transformedArgument.roleToBeTaggedObj.value;
2426
const msgToBeSent = transformedArgument?.displayMessageObj?.value;
27+
const dev = transformedArgument?.dev?.value || false;
2528

2629
const usersWithMatchingRole = filterUserByRoles(
2730
getMembersInServerResponse as UserArray[],
2831
roleId
2932
);
30-
3133
const payload = {
3234
channelId: transformedArgument.channelId,
3335
roleId: roleId,
3436
message: msgToBeSent,
3537
usersWithMatchingRole,
3638
};
3739

38-
if (usersWithMatchingRole.length === 0) {
40+
if (!dev || usersWithMatchingRole.length === 0) {
3941
const responseData = checkDisplayType({
4042
usersWithMatchingRole,
4143
msgToBeSent,
44+
roleId,
4245
});
4346
return discordTextResponse(responseData);
4447
} else {
45-
ctx.waitUntil(
46-
mentionEachUserInMessage({
47-
message: payload.message,
48-
userIds: payload.usersWithMatchingRole,
49-
channelId: payload.channelId,
50-
env,
51-
})
52-
);
53-
return discordTextResponse(
54-
`Found ${usersWithMatchingRole.length} users with matched role, mentioning them shortly...`
55-
);
48+
let responseMessage = "";
49+
if (usersWithMatchingRole.length === 1) {
50+
responseMessage = `The user with <@&${roleId}> role is: ${payload.usersWithMatchingRole}`;
51+
} else {
52+
responseMessage = `The users with <@&${roleId}> role are: ${payload.usersWithMatchingRole} `;
53+
}
54+
return discordTextResponse(responseMessage);
5655
}
5756
}

src/typeDefinitions/filterUsersByRole.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ export type MentionEachUserOptions = {
1010
type: number;
1111
value: string;
1212
};
13+
export type DevFlag = {
14+
name: string;
15+
type: number;
16+
value: boolean;
17+
};

src/utils/checkDisplayType.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
export function checkDisplayType({
55
usersWithMatchingRole,
66
msgToBeSent,
7+
roleId,
78
}: {
89
msgToBeSent?: string;
910
usersWithMatchingRole: string[];
11+
roleId?: string;
1012
}) {
1113
if (usersWithMatchingRole.length > 0) {
1214
const returnString = msgToBeSent ? msgToBeSent : "";
1315
return `${returnString} ${usersWithMatchingRole}`;
1416
} else {
15-
return `Sorry no user found under this role.`;
17+
return `Sorry no user found with <@&${roleId ?? "undefined"}> role.`;
1618
}
1719
}

tests/unit/handlers/mentionEachUser.test.ts

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ describe("Test mention each function", () => {
1919
expect(response).toBeInstanceOf(Promise);
2020
});
2121

22-
it("should run without displayMessageObj argument", async () => {
22+
it("should run without displayMessageObj argument in dev mode", async () => {
2323
const env = {
2424
BOT_PUBLIC_KEY: "xyz",
2525
DISCORD_GUILD_ID: "123",
2626
DISCORD_TOKEN: "abc",
2727
};
28+
const roleId = "1118201414078976192";
2829
const response = mentionEachUser(
2930
{
3031
...onlyRoleToBeTagged,
32+
dev: {
33+
name: "dev",
34+
type: 4,
35+
value: true,
36+
},
3137
},
3238
env,
3339
ctx
@@ -37,7 +43,7 @@ describe("Test mention each function", () => {
3743
(res) => res.json()
3844
);
3945
expect(textMessage.data.content).toBe(
40-
"Sorry no user found under this role."
46+
`Sorry no user found with <@&${roleId}> role.`
4147
);
4248
});
4349

@@ -47,14 +53,14 @@ describe("Test mention each function", () => {
4753
DISCORD_GUILD_ID: "123",
4854
DISCORD_TOKEN: "abc",
4955
};
50-
56+
const roleId = "1118201414078976192";
5157
const response = mentionEachUser(onlyRoleToBeTagged, env, ctx);
5258
expect(response).toBeInstanceOf(Promise);
5359
const textMessage: { data: { content: string } } = await response.then(
5460
(res) => res.json()
5561
);
5662
expect(textMessage.data.content).toBe(
57-
"Sorry no user found under this role."
63+
`Sorry no user found with <@&${roleId}> role.`
5864
);
5965
});
6066

@@ -102,11 +108,16 @@ describe("Test mention each function", () => {
102108
expect(response).toBe(expectedResponse);
103109
});
104110

105-
it("should return default string ", () => {
106-
const usersWithMatchingRole = [] as string[];
111+
it("should return default string", () => {
112+
const roleId = "1118201414078976192";
113+
const usersWithMatchingRole: string[] = [];
107114
const msgToBeSent = "hello";
108-
const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent });
109-
const expectedResponse = `Sorry no user found under this role.`;
115+
const response = checkDisplayType({
116+
usersWithMatchingRole,
117+
msgToBeSent,
118+
roleId,
119+
});
120+
const expectedResponse = `Sorry no user found with <@&${roleId}> role.`;
110121
expect(response).toBe(expectedResponse);
111122
});
112123

@@ -121,4 +132,38 @@ describe("Test mention each function", () => {
121132
const expectedResponse = `${returnString} ${usersWithMatchingRole}`;
122133
expect(response).toBe(expectedResponse);
123134
});
135+
136+
describe("checkDisplayType", () => {
137+
it("should handle message with no matching users", () => {
138+
const usersWithMatchingRole: string[] = [];
139+
const roleId = "1118201414078976192";
140+
const msgToBeSent = "No users found:";
141+
const response = checkDisplayType({
142+
usersWithMatchingRole,
143+
msgToBeSent,
144+
roleId,
145+
});
146+
expect(response).toBe(`Sorry no user found with <@&${roleId}> role.`);
147+
});
148+
});
149+
it("should handle case when only one user found", () => {
150+
const roleId = "860900892193456149";
151+
const optionsArray = [
152+
{
153+
roles: [
154+
"890520255934377985",
155+
"860900892193456149",
156+
"845302148878565406",
157+
],
158+
user: {
159+
id: "282859044593598464",
160+
},
161+
},
162+
];
163+
const response = filterUserByRoles(optionsArray, roleId);
164+
const message = `The user with <@&${roleId}> role is: ${response}`;
165+
expect(message).toBe(
166+
`The user with <@&${roleId}> role is: <@${optionsArray[0].user.id}>`
167+
);
168+
});
124169
});

0 commit comments

Comments
 (0)