From d084c3f3c4d1399640e023f225e825399854074e Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Sat, 6 Mar 2021 14:44:31 -0500 Subject: [PATCH] fix(handlers/channel): handle max_age and max_uses limit in createInvite() (#597) * fix: handle invalid invite create limits * refactor: cleanup conditonals * chore: clarification Co-authored-by: ayntee --- src/api/handlers/channel.ts | 14 ++++++++++++++ src/types/channel.ts | 9 +++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/api/handlers/channel.ts b/src/api/handlers/channel.ts index e22b410ca..5e1b772d1 100644 --- a/src/api/handlers/channel.ts +++ b/src/api/handlers/channel.ts @@ -321,6 +321,20 @@ export async function createInvite( throw new Error(Errors.MISSING_CREATE_INSTANT_INVITE); } + if (options.max_age && (options.max_age > 604800 || options.max_age < 0)) { + console.log( + `The max age for invite created in ${channelID} was not between 0-604800. Using default values instead.`, + ); + options.max_age = undefined; + } + + if (options.max_uses && (options.max_uses > 100 || options.max_uses < 0)) { + console.log( + `The max uses for invite created in ${channelID} was not between 0-100. Using default values instead.`, + ); + options.max_uses = undefined; + } + const result = await RequestManager.post( endpoints.CHANNEL_INVITES(channelID), options, diff --git a/src/types/channel.ts b/src/types/channel.ts index ad3202a95..3743c2ebc 100644 --- a/src/types/channel.ts +++ b/src/types/channel.ts @@ -154,11 +154,12 @@ export interface GetMessagesAround extends GetMessages { around: string; } +// TODO: v11 change to camelcase export interface CreateInviteOptions { - /** Duration of invite in seconds before expiry, or 0 for never. Defaults to 86400 (24 hours) */ - "max_age": number; - /** Max number of uses or 0 for unlimited. Default 0 */ - "max_uses": number; + /** Duration of invite in seconds before expiry, or 0 for never. Between 0-604800 (7 days). Defaults to 86400 (24 hours). */ + "max_age"?: number; + /** Max number of uses or 0 for unlimited. Between 0-100. Default 0 */ + "max_uses"?: number; /** Whether this invite only grants temporary membership. */ temporary: boolean; /** If true, don't try to reuse a similar invite (useful for creating many unique one time use invites.) */