Skip to content

Commit 09aa8bc

Browse files
committed
Refactor suggest listener: improve button validation and adjust type annotations
- Added validation for invalid button interactions with user-friendly ephemeral replies. - Updated `SUGGESTION_BUTTON_MAP` type annotations for better type safety and error handling. - Reformatted imports for consistency and readability.
1 parent 49ca3e0 commit 09aa8bc

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

src/modules/suggest/suggest.listener.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ import {
3030
upsertVote,
3131
} from "./suggest.js";
3232

33-
const SUGGESTION_BUTTON_MAP: Record<string, SuggestionVoteType> = {
33+
const SUGGESTION_BUTTON_MAP: {
34+
[SUGGESTION_NO_ID]: SuggestionVoteType;
35+
[SUGGESTION_YES_ID]: SuggestionVoteType;
36+
[key: string]: SuggestionVoteType | undefined;
37+
} = {
3438
[SUGGESTION_NO_ID]: -1,
3539
[SUGGESTION_YES_ID]: 1,
3640
};
@@ -47,10 +51,17 @@ async function handleVoteButtonInteraction(
4751
});
4852
return;
4953
}
54+
const votingValue = SUGGESTION_BUTTON_MAP[interaction.customId];
55+
if (!votingValue) {
56+
await interaction.reply({
57+
content: "This is not a valid Button!",
58+
flags: MessageFlags.Ephemeral,
59+
});
60+
return;
61+
}
5062

5163
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
5264

53-
const votingValue = SUGGESTION_BUTTON_MAP[interaction.customId];
5465
const suggestion = await getSuggestionByMessageIdOrRecoverFromMessage(
5566
interaction.message,
5667
);

0 commit comments

Comments
 (0)