Skip to content

Commit 3003b31

Browse files
fix: add type predicate for frequency
1 parent f7aa53d commit 3003b31

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/modules/recurringMessage/recurringMessage.helpers.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ const inMemoryJobList: { id: string; job: CronJob }[] = [];
2323

2424
export type Frequency = keyof typeof cronTime;
2525

26+
export const isFrequency = (frequency: string): frequency is Frequency => {
27+
return Object.keys(cronTime).includes(frequency);
28+
};
29+
2630
export const hasPermission = (interaction: ChatInputCommandInteraction) => {
2731
if (!isModo(interaction.member)) {
2832
interaction.reply('You are not allowed to use this command').catch(console.error);
@@ -45,7 +49,7 @@ export const createRecurringMessage = (
4549
console.error(`Channel ${channelId} not found`);
4650
return;
4751
}
48-
channel.send(message).catch(console.error);
52+
void channel.send(message);
4953
},
5054
null,
5155
true,
@@ -56,18 +60,20 @@ export const createRecurringMessage = (
5660
export const addRecurringMessage = async (interaction: ChatInputCommandInteraction) => {
5761
const jobId = randomUUID();
5862
const channelId = interaction.channelId;
59-
const frequency = interaction.options.getString('frequency', true) as Frequency;
63+
const frequency = interaction.options.getString('frequency', true);
64+
if (!isFrequency(frequency)) {
65+
await interaction.reply(`${frequency} is not a valid frequency`);
66+
return;
67+
}
6068
const message = interaction.options.getString('message', true);
6169

6270
const displayIdInMessage = `\n (id: ${jobId})`;
6371
const jobMessage = message + displayIdInMessage;
6472

6573
if (jobMessage.length > MAX_MESSAGE_LENGTH) {
66-
interaction
67-
.reply(
68-
`Message is too long (max ${MAX_MESSAGE_LENGTH - displayIdInMessage.length} characters)`,
69-
)
70-
.catch(console.error);
74+
await interaction.reply(
75+
`Message is too long (max ${MAX_MESSAGE_LENGTH - displayIdInMessage.length} characters)`,
76+
);
7177
return;
7278
}
7379

@@ -98,7 +104,7 @@ export const removeRecurringMessage = async (interaction: ChatInputCommandIntera
98104

99105
const job = inMemoryJobList.find(({ id }) => id === jobId)?.job;
100106
if (!job) {
101-
interaction.reply('Recurring message not found').catch(console.error);
107+
await interaction.reply('Recurring message not found');
102108
return;
103109
}
104110

@@ -111,7 +117,7 @@ export const listRecurringMessages = async (interaction: ChatInputCommandInterac
111117
const recurringMessages = await cache.get('recurringMessages', []);
112118

113119
if (recurringMessages.length === 0) {
114-
interaction.reply('No recurring message found').catch(console.error);
120+
await interaction.reply('No recurring message found');
115121
return;
116122
}
117123

0 commit comments

Comments
 (0)