From c880a737712d80b06f83d8914fffc1184b8e70e0 Mon Sep 17 00:00:00 2001 From: Helloyunho Date: Mon, 25 Jan 2021 03:49:08 +0900 Subject: [PATCH] Do some tests and fix (or add) some --- mod.ts | 10 ++- src/managers/guilds.ts | 64 +++++++++--------- src/managers/roles.ts | 6 ++ src/models/rest.ts | 21 +++++- src/models/slashClient.ts | 4 +- src/structures/guild.ts | 24 +++++-- src/structures/role.ts | 34 ++++++++++ src/structures/user.ts | 5 ++ src/test/guild.ts | 137 ++++++++++++++++++++++++++++++++++++++ src/test/template.ts | 21 ++++++ src/test/user.ts | 14 ++++ src/types/guild.ts | 2 +- src/utils/snowflake.ts | 4 ++ 13 files changed, 300 insertions(+), 46 deletions(-) create mode 100644 src/test/guild.ts create mode 100644 src/test/template.ts create mode 100644 src/test/user.ts diff --git a/mod.ts b/mod.ts index b279793..52305b0 100644 --- a/mod.ts +++ b/mod.ts @@ -102,12 +102,16 @@ export type { MessageOptions } from './src/types/channel.ts' export type { EmojiPayload } from './src/types/emoji.ts' +export { Verification } from './src/types/guild.ts' export type { - GuildBanPayload, - GuildFeatures, GuildIntegrationPayload, GuildPayload, - GuildChannels + GuildBanPayload, + GuildFeatures, + GuildChannels, + GuildCreateOptions, + GuildCreateChannelOptions, + GuildCreateRolePayload } from './src/types/guild.ts' export type { InvitePayload, PartialInvitePayload } from './src/types/invite.ts' export { PermissionFlags } from './src/types/permissionFlags.ts' diff --git a/src/managers/guilds.ts b/src/managers/guilds.ts index 7371267..97d4aad 100644 --- a/src/managers/guilds.ts +++ b/src/managers/guilds.ts @@ -70,45 +70,45 @@ export class GuildManager extends BaseManager { if (options.icon !== undefined && !options.icon.startsWith('data:')) { options.icon = await fetchAuto(options.icon) } + if (options.roles !== undefined && options.roles[0].name !== '@everyone') { + options.roles.unshift({ + id: Math.floor(Math.random() * 18392375458).toString(), + name: '@everyone' + }) + } const body: GuildCreatePayload = { name: options.name, region: options.region, icon: options.icon, verification_level: options.verificationLevel, - roles: - options.roles !== undefined - ? options.roles.map((obj) => { - let result: GuildCreateRolePayload - if (obj instanceof Role) { - result = { - id: obj.id, - name: obj.name, - color: obj.color, - hoist: obj.hoist, - position: obj.position, - permissions: obj.permissions.bitfield.toString(), - managed: obj.managed, - mentionable: obj.mentionable - } - } else { - result = obj - } + roles: options.roles?.map((obj) => { + let result: GuildCreateRolePayload + if (obj instanceof Role) { + result = { + id: obj.id, + name: obj.name, + color: obj.color, + hoist: obj.hoist, + position: obj.position, + permissions: obj.permissions.bitfield.toString(), + managed: obj.managed, + mentionable: obj.mentionable + } + } else { + result = obj + } - return result - }) - : undefined, - channels: - options.channels !== undefined - ? options.channels.map( - (obj): GuildCreateChannelPayload => ({ - id: obj.id, - name: obj.name, - type: obj.type, - parent_id: obj.parentID - }) - ) - : undefined, + return result + }), + channels: options.channels?.map( + (obj): GuildCreateChannelPayload => ({ + id: obj.id, + name: obj.name, + type: obj.type, + parent_id: obj.parentID + }) + ), afk_channel_id: options.afkChannelID, afk_timeout: options.afkTimeout, system_channel_id: options.systemChannelID diff --git a/src/managers/roles.ts b/src/managers/roles.ts index c1b371e..3133ae0 100644 --- a/src/managers/roles.ts +++ b/src/managers/roles.ts @@ -41,6 +41,12 @@ export class RolesManager extends BaseManager { return new Role(this.client, raw, this.guild) } + async array(): Promise { + let arr = await (this.client.cache.array(this.cacheName) as RolePayload[]) + if (arr === undefined) arr = [] + return arr.map((e) => new Role(this.client, e, this.guild)) + } + async fromPayload(roles: RolePayload[]): Promise { for (const role of roles) { await this.set(role.id, role) diff --git a/src/models/rest.ts b/src/models/rest.ts index 7d38968..9d00f62 100644 --- a/src/models/rest.ts +++ b/src/models/rest.ts @@ -28,8 +28,23 @@ export interface RequestHeaders { [name: string]: string } +export interface DiscordAPIErrorPayload { + url: string + status: number + method: string + code?: number + message?: string + errors: object +} + export class DiscordAPIError extends Error { name = 'DiscordAPIError' + error?: DiscordAPIErrorPayload + + constructor(message?: string, error?: DiscordAPIErrorPayload) { + super(message) + this.error = error + } } export interface QueuedItem { @@ -399,7 +414,7 @@ export class RESTManager { ) // At this point we know it is error - const error: { [name: string]: any } = { + const error: DiscordAPIErrorPayload = { url: response.url, status, method: data.method, @@ -443,9 +458,9 @@ export class RESTManager { HttpResponseCode.MethodNotAllowed ].includes(status) ) { - reject(new DiscordAPIError(Deno.inspect(error))) + reject(new DiscordAPIError(Deno.inspect(error), error)) } else if (status === HttpResponseCode.GatewayUnavailable) { - reject(new DiscordAPIError(Deno.inspect(error))) + reject(new DiscordAPIError(Deno.inspect(error), error)) } else reject(new DiscordAPIError('Request - Unknown Error')) } diff --git a/src/models/slashClient.ts b/src/models/slashClient.ts index 9520025..273d4c4 100644 --- a/src/models/slashClient.ts +++ b/src/models/slashClient.ts @@ -381,9 +381,7 @@ export class SlashClient { this.commands = new SlashCommandsManager(this) this.publicKey = options.publicKey - if (options !== undefined) { - this.enabled = options.enabled ?? true - } + this.enabled = options.enabled ?? true if (this.client?._decoratedSlash !== undefined) { this.client._decoratedSlash.forEach((e) => { diff --git a/src/structures/guild.ts b/src/structures/guild.ts index f438b4d..f7234d5 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -41,6 +41,7 @@ import { RequestMembersOptions } from '../gateway/index.ts' import { GuildPresencesManager } from '../managers/presences.ts' import { TemplatePayload } from '../types/template.ts' import { Template } from './template.ts' +import { DiscordAPIError } from '../models/rest.ts' export class GuildBan extends Base { guild: Guild @@ -398,7 +399,22 @@ export class Guild extends Base { /** Returns a partial invite object for guilds with that feature enabled. */ async getVanity(): Promise<{ code: string | null; uses: number }> { - return this.client.rest.api.guilds[this.id]['vanity-url'].get() + try { + const value = await this.client.rest.api.guilds[this.id][ + 'vanity-url' + ].get() + return value + } catch (error) { + if (error instanceof DiscordAPIError) { + if (error.error?.code === 50020) { + return { + code: null, + uses: 0 + } + } + } + throw error + } } /** Returns a PNG (URL) image widget for the guild. */ @@ -441,7 +457,7 @@ export class Guild extends Base { async syncTemplate(code: string): Promise