Skip to content

Commit ab993f5

Browse files
feat: rework on existing mention-each command (#76)
* feat: changing format of command * refactor: formattingd * feat: changed input argument * feat: rework on existing code based on input * fix:output for changing the input * refactor: rework on test :( * refactor:test constants to fixtures * chore: linting * feat:added comments * chore: linting
1 parent c6eae6b commit ab993f5

File tree

6 files changed

+91
-125
lines changed

6 files changed

+91
-125
lines changed

src/constants/commands.ts

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,20 @@ export const VERIFY = {
1010
};
1111

1212
export const MENTION_EACH = {
13-
name: "mention",
13+
name: "mention-each",
1414
description: "mention each user with this role",
1515
options: [
1616
{
17-
name: "list",
18-
description: "get user in list",
19-
type: 2, // 2 is type SUB_COMMAND_GROUP
20-
options: [
21-
{
22-
name: "get",
23-
description: "get me the role ",
24-
type: 1, // 1 is type SUB_COMMAND
25-
options: [
26-
{
27-
name: "user",
28-
description: "The user to get",
29-
type: 9, // 6 is type USER
30-
required: true,
31-
},
32-
{
33-
name: "message",
34-
description: "What message to send to user ?",
35-
type: 3, // 7 is type CHANNEL
36-
required: true,
37-
},
38-
],
39-
},
40-
],
17+
name: "role",
18+
description: "to be tagged in the message",
19+
type: 8, // 2 is type SUB_COMMAND_GROUP
20+
required: true,
4121
},
4222
{
43-
name: "series",
44-
description: "get user in series",
45-
type: 2,
46-
options: [
47-
{
48-
name: "get",
49-
description: "get me the role",
50-
type: 1,
51-
options: [
52-
{
53-
name: "user",
54-
description: "The user to get",
55-
type: 9, // 8 is type ROLE
56-
required: true,
57-
},
58-
{
59-
name: "message",
60-
description:
61-
"The channel permissions to get. If omitted, the guild permissions will be returned",
62-
type: 3,
63-
required: true,
64-
},
65-
],
66-
},
67-
],
23+
name: "message",
24+
description: "any message for them?",
25+
type: 3,
26+
require: false,
6827
},
6928
],
7029
};

src/controllers/baseHandler.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ export async function baseHandler(
3636
}
3737
case getCommandName(MENTION_EACH): {
3838
const data = message.data?.options as Array<messageRequestDataOptions>;
39+
// data[0] is role obj
40+
// data[1] is message obj
41+
const transformedArgument = {
42+
roleToBeTaggedObj: data[0],
43+
displayMessageObj: data[1] ?? {},
44+
};
3945

40-
return await mentionEachUser(
41-
{
42-
displayType: data[0].name,
43-
options: data[0].options[0].options,
44-
},
45-
env
46-
);
46+
return await mentionEachUser(transformedArgument, env);
4747
}
4848
default: {
4949
return commandNotFound();

src/controllers/mentionEachUser.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,25 @@ import {
1010
import { checkDisplayType } from "../utils/checkDisplayType";
1111

1212
export async function mentionEachUser(
13-
message: { displayType: string; options: Array<MentionEachUserOptions> },
13+
transformedArgument: {
14+
roleToBeTaggedObj: MentionEachUserOptions;
15+
displayMessageObj?: MentionEachUserOptions;
16+
},
1417
env: env
1518
) {
1619
const getMembersInServerResponse = await getMembersInServer(env);
17-
18-
// displaytype is list or series & options is first level of options array on desctructure
19-
const { displayType, options } = message;
20-
const [roleToBeTaggedObj, displayMessageObj] = options;
21-
22-
const roleId = roleToBeTaggedObj.value;
23-
const msgToBeSent = displayMessageObj.value;
20+
const roleId = transformedArgument.roleToBeTaggedObj.value;
21+
// optional chaining here only because display message obj is optional argument
22+
const msgToBeSent = transformedArgument?.displayMessageObj?.value;
2423

2524
const usersWithMatchingRole = filterUserByRoles(
2625
getMembersInServerResponse as UserArray[],
2726
roleId
2827
);
2928

3029
const responseData = checkDisplayType({
31-
displayType,
32-
msgToBeSent,
3330
usersWithMatchingRole,
31+
msgToBeSent,
3432
});
3533
return discordTextResponse(responseData);
3634
}

src/utils/checkDisplayType.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1+
// const excludeThis = [undefined, null]
2+
// put a check for the above things later
3+
14
export function checkDisplayType({
2-
displayType,
35
msgToBeSent,
46
usersWithMatchingRole,
57
}: {
6-
displayType: string;
7-
msgToBeSent: string;
8+
msgToBeSent?: string;
89
usersWithMatchingRole: string[];
910
}) {
10-
if (displayType === "list") {
11-
return `Coming soon. We are working on this feature. We feel sorry for not serving you what you expect this command to do for now.(T_T)`;
11+
if (usersWithMatchingRole.length > 0) {
12+
const returnString = msgToBeSent ? msgToBeSent : "";
13+
14+
return `${returnString} ${usersWithMatchingRole} \n \`Disclaimer: Very soon all the users will be tagged individually in a separate(new) message!\``;
1215
} else {
13-
if (usersWithMatchingRole.length > 0) {
14-
return `${displayType && msgToBeSent} ${usersWithMatchingRole}`;
15-
} else {
16-
return `Sorry no user found under this role.`;
17-
}
16+
return `Sorry no user found under this role.`;
1817
}
1918
}

tests/fixtures/fixture.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,20 @@ export const dummyGuildMemberDetails = {
7878
mute: false,
7979
deaf: false,
8080
};
81+
82+
export const transformedArgument = {
83+
roleToBeTaggedObj: {
84+
name: "role",
85+
type: 8,
86+
value: "1118201414078976192",
87+
},
88+
displayMessageObj: { name: "message", type: 3, value: "hello" },
89+
};
90+
91+
export const onlyRoleToBeTagged = {
92+
roleToBeTaggedObj: {
93+
name: "role",
94+
type: 8,
95+
value: "1118201414078976192",
96+
},
97+
};
Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { mentionEachUser } from "../../../src/controllers/mentionEachUser";
22
import { filterUserByRoles } from "../../../src/utils/filterUsersByRole";
33
import { checkDisplayType } from "../../../src/utils/checkDisplayType";
4+
import {
5+
onlyRoleToBeTagged,
6+
transformedArgument,
7+
} from "../../fixtures/fixture";
48

59
describe("Test mention each function", () => {
610
it("Should be an instance of JSONResponse", () => {
@@ -9,27 +13,23 @@ describe("Test mention each function", () => {
913
DISCORD_GUILD_ID: "123",
1014
DISCORD_TOKEN: "abc",
1115
};
12-
const optionsArray = [
13-
{
14-
name: "user",
15-
type: 9,
16-
value: "860900892193456149",
17-
},
18-
{
19-
name: "message",
20-
type: 3,
21-
value: "hello",
22-
},
23-
];
2416

25-
const response = mentionEachUser(
26-
{ displayType: "series", options: optionsArray },
27-
env
28-
);
17+
const response = mentionEachUser(transformedArgument, env);
2918
expect(response).toBeInstanceOf(Promise);
3019
});
3120

32-
it("should filter users based on role", () => {
21+
it("should run without displayMessageObj argument", () => {
22+
const env = {
23+
BOT_PUBLIC_KEY: "xyz",
24+
DISCORD_GUILD_ID: "123",
25+
DISCORD_TOKEN: "abc",
26+
};
27+
28+
const response = mentionEachUser(onlyRoleToBeTagged, env);
29+
expect(response).toBeInstanceOf(Promise);
30+
});
31+
32+
it("should return users with matching roles", () => {
3333
const roleId = "860900892193456149";
3434
const optionsArray = [
3535
{
@@ -56,47 +56,40 @@ describe("Test mention each function", () => {
5656
},
5757
];
5858
const response = filterUserByRoles(optionsArray, roleId);
59-
const expectedResponse = ["<@282859044593598464>", "<@725745030706364447>"];
60-
expect(response).toStrictEqual(expectedResponse);
59+
expect(response).toStrictEqual([
60+
"<@282859044593598464>",
61+
"<@725745030706364447>",
62+
]);
6163
});
6264

63-
it("should return a message if users are not found (usersWithMatchingRole array is empty)", () => {
65+
it("should return message based on the input", () => {
66+
const usersWithMatchingRole = [
67+
"<@282859044593598464>",
68+
"<@725745030706364447>",
69+
];
6470
const msgToBeSent = "hello";
65-
66-
const response = checkDisplayType({
67-
displayType: "series",
68-
msgToBeSent,
69-
usersWithMatchingRole: [],
70-
});
71-
const expectedResponse = `Sorry no user found under this role.`;
72-
71+
const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent });
72+
const expectedResponse = `${msgToBeSent} ${usersWithMatchingRole} \n \`Disclaimer: Very soon all the users will be tagged individually in a separate(new) message!\``;
7373
expect(response).toBe(expectedResponse);
7474
});
7575

76-
it("should return message if displayType is list", () => {
76+
it("should return default string ", () => {
77+
const usersWithMatchingRole = [] as string[];
7778
const msgToBeSent = "hello";
78-
79-
const response = checkDisplayType({
80-
displayType: "list",
81-
msgToBeSent,
82-
usersWithMatchingRole: [],
83-
});
84-
const expectedResponse = `Coming soon. We are working on this feature. We feel sorry for not serving you what you expect this command to do for now.(T_T)`;
85-
79+
const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent });
80+
const expectedResponse = `Sorry no user found under this role.`;
8681
expect(response).toBe(expectedResponse);
8782
});
8883

89-
it("should return a message if users are found (usersWithMatchingRole array is not empty)", () => {
90-
const msgToBeSent = "hello";
91-
92-
const response = checkDisplayType({
93-
displayType: "series",
94-
msgToBeSent,
95-
usersWithMatchingRole: ["<@282859044593598464>", "<@725745030706364447>"],
96-
});
97-
const expectedResponse =
98-
"hello <@282859044593598464>,<@725745030706364447>";
99-
84+
it("should return default string ", () => {
85+
const usersWithMatchingRole = [
86+
"<@282859044593598464>",
87+
"<@725745030706364447>",
88+
] as string[];
89+
const msgToBeSent = undefined;
90+
const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent });
91+
const returnString = msgToBeSent ? msgToBeSent : "";
92+
const expectedResponse = `${returnString} ${usersWithMatchingRole} \n \`Disclaimer: Very soon all the users will be tagged individually in a separate(new) message!\``;
10093
expect(response).toBe(expectedResponse);
10194
});
10295
});

0 commit comments

Comments
 (0)