Skip to content
Merged
6 changes: 6 additions & 0 deletions src/constants/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export const MENTION_EACH = {
type: 5,
require: false,
},
{
name: "showroles",
description: "want to see extra details?",
type: 5,
require: false,
},
],
};

Expand Down
3 changes: 3 additions & 0 deletions src/controllers/baseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export async function baseHandler(
displayMessageObj: data.find((item) => item.name === "message"),
channelId: message.channel_id,
dev: data.find((item) => item.name === "dev") as unknown as DevFlag,
showroles: data.find(
(item) => item.name === "showroles"
) as unknown as DevFlag,
};
return await mentionEachUser(transformedArgument, env, ctx);
}
Expand Down
18 changes: 17 additions & 1 deletion src/controllers/mentionEachUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function mentionEachUser(
roleToBeTaggedObj: MentionEachUserOptions;
displayMessageObj?: MentionEachUserOptions;
channelId: number;
showroles?: DevFlag;
dev?: DevFlag;
},
env: env,
Expand All @@ -25,6 +26,7 @@ export async function mentionEachUser(
const roleId = transformedArgument.roleToBeTaggedObj.value;
const msgToBeSent = transformedArgument?.displayMessageObj?.value;
const dev = transformedArgument?.dev?.value || false;
const showroles = transformedArgument?.showroles?.value || false;
// optional chaining here only because display message obj is optional argument
const usersWithMatchingRole = filterUserByRoles(
getMembersInServerResponse as UserArray[],
Expand All @@ -36,13 +38,27 @@ export async function mentionEachUser(
message: msgToBeSent,
usersWithMatchingRole,
};
if (!dev || usersWithMatchingRole.length === 0) {
if (transformedArgument.showroles?.value === true) {
let responseMessage = "";
if (usersWithMatchingRole.length === 0) {
responseMessage = `Sorry, no user found with <@&${roleId}> role.`;
} else if (usersWithMatchingRole.length === 1) {
// Mention the single user by ID
responseMessage = `The user with <@&${roleId}> role is ${payload.usersWithMatchingRole}.`;
} else {
// Mention multiple users by their IDs
responseMessage = `The users with <@&${roleId}> role are ${payload.usersWithMatchingRole}.`;
}
return discordTextResponse(responseMessage);
} else if (!dev || usersWithMatchingRole.length === 0) {
const responseData = checkDisplayType({
usersWithMatchingRole,
msgToBeSent,
roleId,
});
return discordTextResponse(responseData);
} else {
// Regular dev flow to mention users
ctx.waitUntil(
mentionEachUserInMessage({
message: payload.message,
Expand Down
4 changes: 3 additions & 1 deletion src/utils/checkDisplayType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
export function checkDisplayType({
usersWithMatchingRole,
msgToBeSent,
roleId,
}: {
msgToBeSent?: string;
usersWithMatchingRole: string[];
roleId?: string;
}) {
if (usersWithMatchingRole.length > 0) {
const returnString = msgToBeSent ? msgToBeSent : "";
return `${returnString} ${usersWithMatchingRole}`;
} else {
return `Sorry no user found under this role.`;
return `Sorry no user found with <@&${roleId ?? "undefined"}> role.`;
}
}
54 changes: 47 additions & 7 deletions tests/unit/handlers/mentionEachUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe("Test mention each function", () => {
DISCORD_GUILD_ID: "123",
DISCORD_TOKEN: "abc",
};
const roleId = "1118201414078976192";
const response = mentionEachUser(
{
...onlyRoleToBeTagged,
Expand All @@ -42,7 +43,7 @@ describe("Test mention each function", () => {
(res) => res.json()
);
expect(textMessage.data.content).toBe(
"Sorry no user found under this role."
`Sorry no user found with <@&${roleId}> role.`
);
});

Expand All @@ -52,14 +53,14 @@ describe("Test mention each function", () => {
DISCORD_GUILD_ID: "123",
DISCORD_TOKEN: "abc",
};

const roleId = "1118201414078976192";
const response = mentionEachUser(onlyRoleToBeTagged, env, ctx);
expect(response).toBeInstanceOf(Promise);
const textMessage: { data: { content: string } } = await response.then(
(res) => res.json()
);
expect(textMessage.data.content).toBe(
"Sorry no user found under this role."
`Sorry no user found with <@&${roleId}> role.`
);
});

Expand Down Expand Up @@ -107,11 +108,16 @@ describe("Test mention each function", () => {
expect(response).toBe(expectedResponse);
});

it("should return default string ", () => {
const usersWithMatchingRole = [] as string[];
it("should return default string", () => {
const roleId = "1118201414078976192";
const usersWithMatchingRole: string[] = [];
const msgToBeSent = "hello";
const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent });
const expectedResponse = `Sorry no user found under this role.`;
const response = checkDisplayType({
usersWithMatchingRole,
msgToBeSent,
roleId,
});
const expectedResponse = `Sorry no user found with <@&${roleId}> role.`;
expect(response).toBe(expectedResponse);
});

Expand All @@ -126,4 +132,38 @@ describe("Test mention each function", () => {
const expectedResponse = `${returnString} ${usersWithMatchingRole}`;
expect(response).toBe(expectedResponse);
});

describe("checkDisplayType", () => {
it("should handle message with no matching users", () => {
const usersWithMatchingRole: string[] = [];
const roleId = "1118201414078976192";
const msgToBeSent = "No users found:";
const response = checkDisplayType({
usersWithMatchingRole,
msgToBeSent,
roleId,
});
expect(response).toBe(`Sorry no user found with <@&${roleId}> role.`);
});
});
it("should handle case when only one user found", () => {
const roleId = "860900892193456149";
const optionsArray = [
{
roles: [
"890520255934377985",
"860900892193456149",
"845302148878565406",
],
user: {
id: "282859044593598464",
},
},
];
const response = filterUserByRoles(optionsArray, roleId);
const message = `The user with <@&${roleId}> role is: ${response}`;
expect(message).toBe(
`The user with <@&${roleId}> role is: <@${optionsArray[0].user.id}>`
);
});
});
Loading