fix: remove validations from helpers. Closes #2700

This commit is contained in:
Skillz4Killz
2022-12-18 21:18:33 +00:00
parent d12c618c11
commit 69fa04c326
26 changed files with 184 additions and 189 deletions

View File

@@ -1,4 +1,4 @@
import { Bot } from './deps.js'
import type { Bot } from './deps.js'
import { channels } from './src/channels/index.js'
import { guilds } from './src/guilds/index.js'
import { interactions } from './src/interaction/index.js'
@@ -25,6 +25,27 @@ export function enableValidationsPlugin<B extends Bot> (bot: B): B {
stickers(bot)
webhooks(bot)
// TODO: validations createGuildTemplate
// if (options.name.length < 1 || options.name.length > 100) {
// throw new Error('The name can only be in between 1-100 characters.')
// }
// if (options.description?.length && options.description.length > 120) {
// throw new Error('The description can only be in between 0-120 characters.')
// }
// TODO: validations editGuildTemplate
// if (
// options.name?.length &&
// (options.name.length < 1 || options.name.length > 100)
// ) {
// throw new Error('The name can only be in between 1-100 characters.')
// }
// if (options.description?.length && options.description.length > 120) {
// throw new Error('The description can only be in between 0-120 characters.')
// }
// PLUGINS MUST RETURN THE BOT
return bot
}

View File

@@ -3,4 +3,13 @@ import { threads } from './threads/index.js'
export function channels (bot: Bot) {
threads(bot)
// TODO: validations createChannel
// BITRATE IS IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000
// if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000
// TODO: validations editChannelPositions
// if (channelPositions.length === 0) {
// throw new Error('You must provide at least one channels to be moved.')
// }
}

View File

@@ -4,6 +4,42 @@ export function createScheduledEvent (bot: Bot) {
const createScheduledEvent = bot.helpers.createScheduledEvent
bot.helpers.createScheduledEvent = async function (guildId, options) {
// TODO: validations
// if (!validateLength(options.name, { min: 1, max: 100 })) {
// throw new Error('Name must be between 1-100 characters.')
// }
// if (
// options.description &&
// !validateLength(options.description, { max: 1000 })
// ) {
// throw new Error('Description must be below 1000 characters.')
// }
// if (options.location) {
// if (!validateLength(options.location, { max: 100 })) {
// throw new Error('Location must be below 100 characters.')
// }
// if (options.entityType === ScheduledEventEntityType.Voice) {
// throw new Error('Location can not be provided for a Voice event.')
// }
// }
// if (options.entityType === ScheduledEventEntityType.External) {
// if (!options.scheduledEndTime) {
// throw new Error(
// 'A scheduled end time is required when making an External event.'
// )
// }
// if (!options.location) {
// throw new Error('A location is required when making an External event.')
// }
// }
// if (
// options.scheduledStartTime &&
// options.scheduledEndTime &&
// options.scheduledStartTime > options.scheduledEndTime
// ) {
// throw new Error('Cannot schedule event to end before starting.')
// }
if (options.entityType === ScheduledEventEntityType.StageInstance) {
if (!options.channelId) {
throw new Error(

View File

@@ -3,4 +3,25 @@ import { createScheduledEvent } from './createScheduledEvent.js'
export function events (bot: Bot) {
createScheduledEvent(bot)
// TODO: validations editScheduledEvent
// if (options.name && !validateLength(options.name, { min: 1, max: 100 })) {
// throw new Error('Name must be between 1-100 characters.')
// }
// if (
// options.description &&
// !validateLength(options.description, { max: 1000 })
// ) {
// throw new Error('Description must be below 1000 characters.')
// }
// if (options.location && !validateLength(options.location, { max: 100 })) {
// throw new Error('Location must be below 100 characters.')
// }
// if (
// options.scheduledStartTime &&
// options.scheduledEndTime &&
// options.scheduledStartTime > options.scheduledEndTime
// ) {
// throw new Error('Cannot schedule event to end before starting.')
// }
}

View File

@@ -1,8 +1,40 @@
import { Bot } from '../../deps.js'
import type { Bot } from '../../deps.js'
import { createGuild } from './createGuild.js'
import { events } from './events/index.js'
export function guilds (bot: Bot) {
events(bot)
createGuild(bot)
// TODO: validations createEmoji
// if (options.image && !options.image.startsWith('data:image/')) {
// options.image = await urlToBase64(options.image)
// }
// TODO: validations editGuild
// if (options.icon && !options.icon.startsWith('data:image/')) {
// options.icon = await urlToBase64(options.icon)
// }
// if (options.banner && !options.banner.startsWith('data:image/')) {
// options.banner = await urlToBase64(options.banner)
// }
// if (options.splash && !options.splash.startsWith('data:image/')) {
// options.splash = await urlToBase64(options.splash)
// }
// TODO: validations getAuditLog
// if (options?.limit) {
// options.limit =
// options.limit >= 1 && options.limit <= 100 ? options.limit : 50
// }
// TODO: validations getPruneCount
// if (options?.days && options.days < 1) {
// throw new Error(rest.constants.Errors.PRUNE_MIN_DAYS)
// }
// if (options?.days && options.days > 30) {
// throw new Error(rest.constants.Errors.PRUNE_MAX_DAYS)
// }
}

View File

@@ -3,4 +3,27 @@ import { editMember } from './editMember.js'
export function members (bot: Bot) {
editMember(bot)
// TODO: validations getDmChannel
// if (userId === rest.id) {
// throw new Error(rest.constants.Errors.YOU_CAN_NOT_DM_THE_BOT_ITSELF)
// }
// TODO: validations pruneMembers
// if (options.days && options.days < 1) {
// throw new Error(rest.constants.Errors.PRUNE_MIN_DAYS)
// }
// if (options.days && options.days > 30) {
// throw new Error(rest.constants.Errors.PRUNE_MAX_DAYS)
// }
// TODO: validations searchMembers
// if (options?.limit) {
// if (options.limit < 1) {
// throw new Error(rest.constants.Errors.MEMBER_SEARCH_LIMIT_TOO_LOW)
// }
// if (options.limit > 1000) {
// throw new Error(rest.constants.Errors.MEMBER_SEARCH_LIMIT_TOO_HIGH)
// }
// }
}

View File

@@ -8,6 +8,17 @@ export function deleteMessages (bot: Bot) {
ids,
reason
) {
// TODO: validations
// if (ids.length < 2) {
// throw new Error(rest.constants.Errors.DELETE_MESSAGES_MIN)
// }
// if (ids.length > 100) {
// console.warn(
// 'This endpoint only accepts a maximum of 100 messages. Using the first 100 message ids provided.'
// )
// }
// 2 WEEKS
const oldestAllowed = Date.now() - 1209600000

View File

@@ -7,4 +7,9 @@ export function messages (bot: Bot) {
deleteMessages(bot)
editMessage(bot)
sendMessage(bot)
// TODO: validations getMessages
// if (options?.limit && (options.limit < 0 || options.limit > 100)) {
// throw new Error(rest.constants.Errors.INVALID_GET_MESSAGES_LIMIT)
// }
}

View File

@@ -36,9 +36,6 @@ export async function createChannel (
guildId: BigString,
options: CreateGuildChannel
): Promise<SnakeToCamelCaseNested<DiscordChannel>> {
// BITRATE IS IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000
if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000
const result = await rest.runMethod<DiscordChannel>(
rest,
'POST',

View File

@@ -22,10 +22,6 @@ export async function editChannelPositions (
guildId: BigString,
channelPositions: ModifyGuildChannelPositions[]
): Promise<void> {
if (channelPositions.length === 0) {
throw new Error('You must provide at least one channels to be moved.')
}
return await rest.runMethod<void>(
rest,
'PATCH',

View File

@@ -5,7 +5,6 @@ import type {
WithReason,
DiscordCreateGuildEmoji
} from '@discordeno/types'
import { urlToBase64 } from '@discordeno/utils'
import type { RestManager } from '../../restManager.js'
/**
@@ -30,10 +29,6 @@ export async function createEmoji (
guildId: BigString,
options: CreateGuildEmoji
): Promise<SnakeToCamelCaseNested<DiscordEmoji>> {
if (options.image && !options.image.startsWith('data:image/')) {
options.image = await urlToBase64(options.image)
}
const result = await rest.runMethod<DiscordEmoji>(
rest,
'POST',

View File

@@ -1,14 +1,11 @@
import type {
BigString,
DefaultMessageNotificationLevels,
DiscordGuild,
ExplicitContentFilterLevels,
DiscordGuild, DiscordModifyGuild, ExplicitContentFilterLevels,
GuildFeatures,
SystemChannelFlags,
VerificationLevels,
DiscordModifyGuild
VerificationLevels
} from '@discordeno/types'
import { urlToBase64 } from '@discordeno/utils'
import type { RestManager } from '../../restManager.js'
import type { Guild } from '../../transformers/guild.js'
@@ -39,18 +36,6 @@ export async function editGuild (
options: ModifyGuild,
shardId: number
): Promise<Guild> {
if (options.icon && !options.icon.startsWith('data:image/')) {
options.icon = await urlToBase64(options.icon)
}
if (options.banner && !options.banner.startsWith('data:image/')) {
options.banner = await urlToBase64(options.banner)
}
if (options.splash && !options.splash.startsWith('data:image/')) {
options.splash = await urlToBase64(options.splash)
}
const result = await rest.runMethod<DiscordGuild>(
rest,
'PATCH',

View File

@@ -1,14 +1,11 @@
import type {
BigString,
DiscordCreateScheduledEvent,
DiscordScheduledEvent,
WithReason
DiscordScheduledEvent, ScheduledEventEntityType, WithReason
} from '@discordeno/types'
import {
ScheduledEventEntityType,
ScheduledEventPrivacyLevel
} from '@discordeno/types'
import { validateLength } from '@discordeno/utils'
import type { RestManager } from '../../../restManager.js'
import type { ScheduledEvent } from '../../../transformers/scheduledEvent.js'
@@ -34,41 +31,6 @@ export async function createScheduledEvent (
guildId: BigString,
options: CreateScheduledEvent
): Promise<ScheduledEvent> {
if (!validateLength(options.name, { min: 1, max: 100 })) {
throw new Error('Name must be between 1-100 characters.')
}
if (
options.description &&
!validateLength(options.description, { max: 1000 })
) {
throw new Error('Description must be below 1000 characters.')
}
if (options.location) {
if (!validateLength(options.location, { max: 100 })) {
throw new Error('Location must be below 100 characters.')
}
if (options.entityType === ScheduledEventEntityType.Voice) {
throw new Error('Location can not be provided for a Voice event.')
}
}
if (options.entityType === ScheduledEventEntityType.External) {
if (!options.scheduledEndTime) {
throw new Error(
'A scheduled end time is required when making an External event.'
)
}
if (!options.location) {
throw new Error('A location is required when making an External event.')
}
}
if (
options.scheduledStartTime &&
options.scheduledEndTime &&
options.scheduledStartTime > options.scheduledEndTime
) {
throw new Error('Cannot schedule event to end before starting.')
}
const result = await rest.runMethod<DiscordScheduledEvent>(
rest,
'POST',

View File

@@ -7,7 +7,6 @@ import type {
ScheduledEventStatus,
WithReason
} from '@discordeno/types'
import { validateLength } from '@discordeno/utils'
import type { RestManager } from '../../../restManager.js'
import type { ScheduledEvent } from '../../../transformers/scheduledEvent.js'
@@ -36,26 +35,6 @@ export async function editScheduledEvent (
eventId: BigString,
options: Partial<EditScheduledEvent>
): Promise<ScheduledEvent> {
if (options.name && !validateLength(options.name, { min: 1, max: 100 })) {
throw new Error('Name must be between 1-100 characters.')
}
if (
options.description &&
!validateLength(options.description, { max: 1000 })
) {
throw new Error('Description must be below 1000 characters.')
}
if (options.location && !validateLength(options.location, { max: 100 })) {
throw new Error('Location must be below 100 characters.')
}
if (
options.scheduledStartTime &&
options.scheduledEndTime &&
options.scheduledStartTime > options.scheduledEndTime
) {
throw new Error('Cannot schedule event to end before starting.')
}
const result = await rest.runMethod<DiscordScheduledEvent>(
rest,
'PATCH',

View File

@@ -42,23 +42,12 @@ export async function getScheduledEventUsers (
): Promise<
Collection<bigint, User> | Collection<bigint, { user: User, member: Member }>
> {
let url = rest.constants.routes.GUILD_SCHEDULED_EVENT_USERS(
const url = rest.constants.routes.GUILD_SCHEDULED_EVENT_USERS(
guildId,
eventId,
options
)
if (options != null) {
url = '?'
if (options.limit) url += `limit=${options.limit}`
if (options.withMember !== undefined) {
url += `&with_member=${options.withMember.toString()}`
}
if (options.after) url += `&after=${options.after}`
if (options.before) url += `&before=${options.before}`
}
const results = await rest.runMethod<
Array<{ user: DiscordUser, member?: DiscordMember }>
>(rest, 'GET', url)

View File

@@ -45,11 +45,6 @@ export async function getAuditLog (
guildId: BigString,
options?: GetGuildAuditLog
): Promise<AuditLog> {
if (options?.limit) {
options.limit =
options.limit >= 1 && options.limit <= 100 ? options.limit : 50
}
const result = await rest.runMethod<DiscordAuditLog>(
rest,
'GET',

View File

@@ -23,13 +23,6 @@ export async function getPruneCount (
guildId: BigString,
options?: GetGuildPruneCountQuery
): Promise<number> {
if (options?.days && options.days < 1) {
throw new Error(rest.constants.Errors.PRUNE_MIN_DAYS)
}
if (options?.days && options.days > 30) {
throw new Error(rest.constants.Errors.PRUNE_MAX_DAYS)
}
const result = await rest.runMethod<DiscordPrunedCount>(
rest,
'GET',

View File

@@ -15,10 +15,6 @@ export async function getDmChannel (
rest: RestManager,
userId: BigString
): Promise<Channel> {
if (userId === rest.id) {
throw new Error(rest.constants.Errors.YOU_CAN_NOT_DM_THE_BOT_ITSELF)
}
const result = await rest.runMethod<DiscordChannel>(
rest,
'POST',

View File

@@ -25,13 +25,6 @@ export async function pruneMembers (
guildId: BigString,
options: BeginGuildPrune
): Promise<number | undefined> {
if (options.days && options.days < 1) {
throw new Error(rest.constants.Errors.PRUNE_MIN_DAYS)
}
if (options.days && options.days > 30) {
throw new Error(rest.constants.Errors.PRUNE_MAX_DAYS)
}
const result = await rest.runMethod<{ pruned: number | null }>(
rest,
'POST',

View File

@@ -1,7 +1,6 @@
import type {
DiscordMemberWithUser,
SearchMembers,
BigString
BigString, DiscordMemberWithUser,
SearchMembers
} from '@discordeno/types'
import { Collection } from '@discordeno/utils'
@@ -25,15 +24,6 @@ export async function searchMembers (
query: string,
options?: Omit<SearchMembers, 'query'>
): Promise<Collection<bigint, Member>> {
if (options?.limit) {
if (options.limit < 1) {
throw new Error(rest.constants.Errors.MEMBER_SEARCH_LIMIT_TOO_LOW)
}
if (options.limit > 1000) {
throw new Error(rest.constants.Errors.MEMBER_SEARCH_LIMIT_TOO_HIGH)
}
}
const results = await rest.runMethod<DiscordMemberWithUser[]>(
rest,
'GET',

View File

@@ -23,16 +23,6 @@ export async function deleteMessages (
messageIds: BigString[],
reason?: string
): Promise<void> {
if (messageIds.length < 2) {
throw new Error(rest.constants.Errors.DELETE_MESSAGES_MIN)
}
if (messageIds.length > 100) {
console.warn(
'This endpoint only accepts a maximum of 100 messages. Using the first 100 message ids provided.'
)
}
return await rest.runMethod<void>(
rest,
'POST',

View File

@@ -24,10 +24,6 @@ export async function getMessages (
channelId: BigString,
options?: GetMessagesOptions
): Promise<Collection<bigint, Message>> {
if (options?.limit && (options.limit < 0 || options.limit > 100)) {
throw new Error(rest.constants.Errors.INVALID_GET_MESSAGES_LIMIT)
}
const results = await rest.runMethod<DiscordMessage[]>(
rest,
'GET',

View File

@@ -1,6 +1,6 @@
import type { BigString, DiscordCreateTemplate, DiscordTemplate } from '@discordeno/types'
import type { RestManager } from '../../restManager.js'
import type { Template } from '../../transformers/template.js'
import type { DiscordTemplate, BigString, DiscordCreateTemplate } from '@discordeno/types'
/**
* Creates a template from a guild.
@@ -22,14 +22,6 @@ export async function createGuildTemplate (
guildId: BigString,
options: CreateTemplate
): Promise<Template> {
if (options.name.length < 1 || options.name.length > 100) {
throw new Error('The name can only be in between 1-100 characters.')
}
if (options.description?.length && options.description.length > 120) {
throw new Error('The description can only be in between 0-120 characters.')
}
const result = await rest.runMethod<DiscordTemplate>(
rest,
'POST',

View File

@@ -28,17 +28,6 @@ export async function editGuildTemplate (
templateCode: string,
options: ModifyGuildTemplate
): Promise<Template> {
if (
options.name?.length &&
(options.name.length < 1 || options.name.length > 100)
) {
throw new Error('The name can only be in between 1-100 characters.')
}
if (options.description?.length && options.description.length > 120) {
throw new Error('The description can only be in between 0-120 characters.')
}
const result = await rest.runMethod<DiscordTemplate>(
rest,
'PATCH',

View File

@@ -2,7 +2,7 @@
// @ts-nocheck
import type { BigString } from '@discordeno/types'
import { GatewayIntents, GatewayOpcodes } from '@discordeno/types'
import { GatewayOpcodes } from '@discordeno/types'
import { calculateShardId } from '@discordeno/utils'
import type { RestManager } from '../../restManager.js'
@@ -37,17 +37,18 @@ export async function fetchMembers (
): Promise<void> {
// You can request 1 member without the intent
// Check if intents is not 0 as proxy ws won't set intents in other instances
if (
bot.intents &&
(!options?.limit || options.limit > 1) &&
!(bot.intents & GatewayIntents.GuildMembers)
) {
throw new Error(rest.constants.Errors.MISSING_INTENT_GUILD_MEMBERS)
}
// TODO: validations
// if (
// bot.intents &&
// (!options?.limit || options.limit > 1) &&
// !(bot.intents & GatewayIntents.GuildMembers)
// ) {
// throw new Error(rest.constants.Errors.MISSING_INTENT_GUILD_MEMBERS)
// }
if (options?.userIds?.length) {
options.limit = options.userIds.length
}
// if (options?.userIds?.length) {
// options.limit = options.userIds.length
// }
const shardId = calculateShardId(
bot.gateway,

View File

@@ -1,7 +1,5 @@
// @ts-nocheck
import type { DiscordGuild } from '@discordeno/types'
import { calculateShardId, urlToBase64 } from '@discordeno/utils'
import { calculateShardId } from '@discordeno/utils'
import type { RestManager } from '../../restManager.js'
import type { Guild } from '../../transformers/guild.js'
@@ -26,9 +24,10 @@ export async function createGuildFromTemplate (
numberOfShard: number,
options: CreateGuildFromTemplate
): Promise<Guild> {
if (options.icon) {
options.icon = await urlToBase64(options.icon)
}
// TODO: validations
// if (options.icon) {
// options.icon = await urlToBase64(options.icon)
// }
const createdGuild = await rest.runMethod<DiscordGuild>(
rest,