Skip to content

Commit

Permalink
Prevent API requests if user cannot receive DMs (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
rorro authored Oct 30, 2023
1 parent 15ef3ee commit 31f65d2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
22 changes: 13 additions & 9 deletions src/commands/instances/moderation/CreateAPIKeyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ class CreateAPIKeyCommand extends Command {

if (!requester) throw new Error();

const key = await createAPIKey(project, requester.user.username);
const sentDM = await requester.send(`Generating API key...`).catch(() => {
throw new CommandError(
`Failed to send DM to <@${requesterId}>. Please go into Privacy Settings and enable Direct Messages.`
);
});

// DM the user back with the new API key
await requester
.send(
`Wise Old Man API key for "${project}":\n\`${key.id}\`\n\n<https://docs.wiseoldman.net/#rate-limits--api-keys>`
)
.catch(() => {
throw new CommandError(`Failed to send DM to <@${requesterId}>.`);
});
const key = await createAPIKey(project, requester.user.username).catch(e => {
sentDM.edit('Failed to generate API key.');
throw e;
});

sentDM.edit(
`Wise Old Man API key for "${project}":\n\`${key.id}\`\n\n<https://docs.wiseoldman.net/#rate-limits--api-keys>`
);

// Respond on the WOM discord chat with a success status
const response = new MessageEmbed()
Expand Down
15 changes: 11 additions & 4 deletions src/commands/instances/moderation/ResetCompetitionCodeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { resetCompetitionCode } from '../../../services/wiseoldman';
import config from '../../../config';
import { Command, CommandConfig, CommandError, sendModLog } from '../../../utils';

const DM_MESSAGE = (code: string) =>
`Hey! Here's your new verification code: \n\`${code}\`\n\nPlease save it somewhere safe and be mindful of who you choose to share it with.`;
const DM_MESSAGE = (code: string, competitionId: number) =>
`Hey! Here's your new verification code for competition ${competitionId}: \n\`${code}\`\n\nPlease save it somewhere safe and be mindful of who you choose to share it with.`;

const CHAT_MESSAGE = (userId: string) =>
`Verification code successfully reset. A DM has been sent to <@${userId}>.`;
Expand Down Expand Up @@ -45,15 +45,22 @@ class ResetCompetitionCodeCommand extends Command {
throw new CommandError("Couldn't find that user.");
}

const sentDM = await user.send('Resetting competition code...').catch(() => {
throw new CommandError(
`Failed to send DM to ${user}. Please go into Privacy Settings and enable Direct Messages.`
);
});

const { newCode } = await resetCompetitionCode(competitionId).catch(e => {
sentDM.edit('Failed to generate a new verification code.');
if (e.statusCode === 400) throw new CommandError(e.message);
if (e.statusCode === 404) throw new CommandError('Competition not found.');
if (e.statusCode === 404) throw new CommandError(`Competition '${competitionId}' not found.`);

throw e;
});

// DM the user back with the new verification code
await user.send(DM_MESSAGE(newCode));
await sentDM.edit(DM_MESSAGE(newCode, competitionId));

// Respond on the WOM discord chat with a success status
const response = new MessageEmbed()
Expand Down
15 changes: 11 additions & 4 deletions src/commands/instances/moderation/ResetGroupCodeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { resetGroupCode } from '../../../services/wiseoldman';
import config from '../../../config';
import { Command, CommandConfig, CommandError, sendModLog } from '../../../utils';

const DM_MESSAGE = (code: string) =>
`Hey! Here's your new verification code: \n\`${code}\`\n\nPlease save it somewhere safe and be mindful of who you choose to share it with.`;
const DM_MESSAGE = (code: string, groupId: number) =>
`Hey! Here's your new verification code for group ${groupId}: \n\`${code}\`\n\nPlease save it somewhere safe and be mindful of who you choose to share it with.`;

const CHAT_MESSAGE = (userId: string) =>
`Verification code successfully reset. A DM has been sent to <@${userId}>.`;
Expand Down Expand Up @@ -45,13 +45,20 @@ class ResetGroupCodeCommand extends Command {
throw new CommandError("Couldn't find that user.");
}

const sentDM = await user.send('Resetting group code...').catch(() => {
throw new CommandError(
`Failed to send DM to ${user}. Please go into Privacy Settings and enable Direct Messages.`
);
});

const { newCode } = await resetGroupCode(groupId).catch(e => {
if (e.statusCode === 404) throw new CommandError('Competition not found.');
sentDM.edit('Failed to generate a new verification code.');
if (e.statusCode === 404) throw new CommandError(`Group '${groupId}' not found.`);
throw e;
});

// DM the user back with the new verification code
await user.send(DM_MESSAGE(newCode));
await sentDM.edit(DM_MESSAGE(newCode, groupId));

// Respond on the WOM discord chat with a success status
const response = new MessageEmbed()
Expand Down

0 comments on commit 31f65d2

Please sign in to comment.