Skip to content

Commit c4d8adc

Browse files
FEAT: ephemeral response message for /verify command (#51)
* FEAT: Epheremal text response add * FEAT: retry command error message add * FEAT: replaced discord dm with discord epheremal message function * Fix: remove unused import * Fix: typo epheremal to ephemeral fix * TEST: tests add for discordEphemeralResponse .ts file * REFACTOR: token update to dummy * Fix: remove headers, second parameter for env and typo errors * ENHANCE: responseJson interface update * TEST ENHANCE: discordEphemeralResponse test suits update * REFACTOR: add comments for better code quality
1 parent 21935ec commit c4d8adc

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed

src/constants/responses.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ export const COMMAND_NOT_FOUND = "Command Not Found";
1818

1919
export const INTERNAL_SERVER_ERROR =
2020
"Oops! We have encountered an internal Server Error";
21+
22+
export const RETRY_COMMAND =
23+
"Oops, we didn't catch that! Please use the command again.";

src/controllers/verifyCommand.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import config from "../../config/config";
2+
import { RETRY_COMMAND } from "../constants/responses";
23
import { env } from "../typeDefinitions/default.types";
3-
import { discordTextResponse } from "../utils/discordResponse";
4+
import { discordEphemeralResponse } from "../utils/discordEphemeralResponse";
45
import { generateUniqueToken } from "../utils/generateUniqueToken";
5-
import { sendDiscordDm } from "../utils/sendDiscordDm";
66
import { sendUserDiscordData } from "../utils/sendUserDiscordData";
77

88
export async function verifyCommand(
@@ -22,12 +22,11 @@ export async function verifyCommand(
2222
discriminator,
2323
env
2424
);
25-
if (response?.status === 201) {
25+
if (response?.status === 201 || response?.status === 200) {
2626
const verificationSiteURL = config(env).VERIFICATION_SITE_URL;
2727
const message = `${verificationSiteURL}/discord?token=${token}`;
28-
await sendDiscordDm(userId, env, message);
29-
return discordTextResponse("Please check the DM");
28+
return discordEphemeralResponse(message);
3029
} else {
31-
return discordTextResponse("Error, please use the verify command again. ");
30+
return discordEphemeralResponse(RETRY_COMMAND);
3231
}
3332
}

src/typeDefinitions/default.types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ export interface responseJson {
2020
type: number;
2121
data: {
2222
content: string;
23+
flags?: number;
2324
};
2425
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { InteractionResponseType } from "discord-interactions";
2+
import JSONResponse from "./JsonResponse";
3+
export const discordEphemeralResponse = (reply: string): JSONResponse => {
4+
return new JSONResponse({
5+
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
6+
data: {
7+
content: reply,
8+
flags: 64, // for ephemeral messages, flags value 64 will generate an ephemeral response. "1 << 6" is converted to integer.
9+
},
10+
});
11+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { InteractionResponseType } from "discord-interactions";
2+
import { responseJson } from "../../src/typeDefinitions/default.types";
3+
import JSONResponse from "../../src/utils/JsonResponse";
4+
import { discordEphemeralResponse } from "../../src/utils/discordEphemeralResponse";
5+
6+
describe("Test discordEphemeralResponse function", () => {
7+
it("should return a JSONResponse", () => {
8+
const response = discordEphemeralResponse("Hello");
9+
expect(response).toBeInstanceOf(JSONResponse);
10+
});
11+
it("Should have type as channelMessageWithSource", async () => {
12+
const response: responseJson = await discordEphemeralResponse(
13+
"Hello"
14+
).json();
15+
expect(response?.type).toBe(
16+
InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE
17+
);
18+
});
19+
it("Should contain a content property in data", async () => {
20+
const response: responseJson = await discordEphemeralResponse(
21+
"Hello"
22+
).json();
23+
expect(response?.data).toHaveProperty("content");
24+
});
25+
it("Should have content as hello", async () => {
26+
const response: responseJson = await discordEphemeralResponse(
27+
"Hello"
28+
).json();
29+
expect(response?.data.content).toBe("Hello");
30+
});
31+
it("Should contain a flag property in data", async () => {
32+
const response: responseJson = await discordEphemeralResponse(
33+
"Hello"
34+
).json();
35+
expect(response?.data).toHaveProperty("flags");
36+
});
37+
it("Should have flag as 64", async () => {
38+
const response: responseJson = await discordEphemeralResponse(
39+
"Hello"
40+
).json();
41+
expect(response?.data.flags).toBe(64);
42+
});
43+
});

0 commit comments

Comments
 (0)