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 @@ -44,6 +44,12 @@ export const MENTION_EACH = {
type: 3,
require: false,
},
{
name: "dev",
description: "want to tag them individually?",
type: 5,
require: false,
},
],
};

Expand Down
25 changes: 12 additions & 13 deletions src/controllers/mentionEachUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { env } from "../typeDefinitions/default.types";
import {
UserArray,
MentionEachUserOptions,
DevFlag,
} from "../typeDefinitions/filterUsersByRole";
import { mentionEachUserInMessage } from "../utils/guildRole";
import { checkDisplayType } from "../utils/checkDisplayType";
Expand All @@ -15,43 +16,41 @@ export async function mentionEachUser(
roleToBeTaggedObj: MentionEachUserOptions;
displayMessageObj?: MentionEachUserOptions;
channelId: number;
dev?: DevFlag;
},
env: env,
ctx: ExecutionContext
) {
const getMembersInServerResponse = await getMembersInServer(env);
const roleId = transformedArgument.roleToBeTaggedObj.value;
const msgToBeSent = transformedArgument?.displayMessageObj?.value;
const dev = transformedArgument?.dev?.value || false;

const usersWithMatchingRole = filterUserByRoles(
getMembersInServerResponse as UserArray[],
roleId
);

const payload = {
channelId: transformedArgument.channelId,
roleId: roleId,
message: msgToBeSent,
usersWithMatchingRole,
};

if (usersWithMatchingRole.length === 0) {
if (!dev || usersWithMatchingRole.length === 0) {
const responseData = checkDisplayType({
usersWithMatchingRole,
msgToBeSent,
roleId,
});
return discordTextResponse(responseData);
} else {
ctx.waitUntil(
mentionEachUserInMessage({
message: payload.message,
userIds: payload.usersWithMatchingRole,
channelId: payload.channelId,
env,
})
);
return discordTextResponse(
`Found ${usersWithMatchingRole.length} users with matched role, mentioning them shortly...`
);
let responseMessage = "";
if (usersWithMatchingRole.length === 1) {
responseMessage = `The user with <@&${roleId}> role is: ${payload.usersWithMatchingRole}`;
} else {
responseMessage = `The users with <@&${roleId}> role are: ${payload.usersWithMatchingRole} `;
}
return discordTextResponse(responseMessage);
}
}
5 changes: 5 additions & 0 deletions src/typeDefinitions/filterUsersByRole.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ export type MentionEachUserOptions = {
type: number;
value: string;
};
export type DevFlag = {
name: string;
type: number;
value: boolean;
};
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.`;
}
}
61 changes: 53 additions & 8 deletions tests/unit/handlers/mentionEachUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ describe("Test mention each function", () => {
expect(response).toBeInstanceOf(Promise);
});

it("should run without displayMessageObj argument", async () => {
it("should run without displayMessageObj argument in dev mode", async () => {
const env = {
BOT_PUBLIC_KEY: "xyz",
DISCORD_GUILD_ID: "123",
DISCORD_TOKEN: "abc",
};
const roleId = "1118201414078976192";
const response = mentionEachUser(
{
...onlyRoleToBeTagged,
dev: {
name: "dev",
type: 4,
value: true,
},
},
env,
ctx
Expand All @@ -37,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 @@ -47,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 @@ -102,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 @@ -121,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