From 196c03f8cc67859900a5b90ae64f2bbddb461ae3 Mon Sep 17 00:00:00 2001 From: March 7th <71698422+aiko-chan-ai@users.noreply.github.com> Date: Wed, 3 Aug 2022 19:08:32 +0700 Subject: [PATCH] refactor: clean code @TheDevYellowy :)) --- src/structures/Guild.js | 2 +- src/structures/interfaces/Application.js | 34 +++++++++++++----------- typings/index.d.ts | 4 +-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 240207c..2704b62 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -1431,7 +1431,7 @@ class Guild extends AnonymousGuild { /** * Marks the guild as read - * @returns {undefined} nothing :) + * @returns {Promise} nothing :) * @example * const guild = client.guilds.fetch('222078108977594368'); * guild.read(); diff --git a/src/structures/interfaces/Application.js b/src/structures/interfaces/Application.js index 659fc1d..3d188f2 100644 --- a/src/structures/interfaces/Application.js +++ b/src/structures/interfaces/Application.js @@ -2,6 +2,7 @@ const process = require('node:process'); const { ClientApplicationAssetTypes, Endpoints } = require('../../util/Constants'); +const Permissions = require('../../util/Permissions'); const SnowflakeUtil = require('../../util/SnowflakeUtil'); const Base = require('../Base'); @@ -77,29 +78,30 @@ class Application extends Base { /** * Invites this application to a guild / server - * @param {string} guild_id The id of the guild that you want to invite the bot to - * @param {bigint} [permissions] The permissions for the bot in number form (the default is 8 / Administrator) + * @param {Snowflake} guild_id The id of the guild that you want to invite the bot to + * @param {PermissionResolvable} [permissions] The permissions for the bot in number form (the default is 8 / Administrator) * @param {string} [captcha] The captcha key to add - * @returns {undefined} nothing :) + * @returns {Promise} nothing :) */ - async invite(guild_id, permissions = 8, captcha = null) { + async invite(guild_id, permissions = 0n, captcha = null) { + permissions = Permissions.resolve(permissions); + const postData = { + authorize: true, + guild_id, + permissions: '0', + }; + if (permissions) { + postData.permissions = permissions; + } + if (captcha && typeof captcha === 'string' && captcha.length > 0) { + postData.captcha = captcha; + } await this.client.api.oauth2.authorize.post({ query: { client_id: this.id, scope: 'bot applications.commands', }, - data: captcha - ? { - captcha_key: captcha, - authorize: true, - guild_id, - permissions, - } - : { - authorize: true, - guild_id, - permissions, - }, + data: postData, headers: { referer: `https://discord.com/oauth2/authorize?client_id=${this.id}&permissions=${permissions}&scope=bot`, }, diff --git a/typings/index.d.ts b/typings/index.d.ts index 289d88b..57554c9 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -337,7 +337,7 @@ export abstract class Application extends Base { public coverURL(options?: StaticImageURLOptions): string | null; /** @deprecated This method is deprecated as it is unsupported and will be removed in the next major version. */ public fetchAssets(): Promise; - public invite(guildID: string, permissions: bigint, captcha: string): null; + public invite(guildID: Snowflake, permissions?: PermissionResolvable, captcha?: string): Promise; public iconURL(options?: StaticImageURLOptions): string | null; public toJSON(): unknown; public toString(): string | null; @@ -1236,7 +1236,7 @@ export class Guild extends AnonymousGuild { public readonly maximumBitrate: number; public createTemplate(name: string, description?: string): Promise; public delete(): Promise; - public read(): undefined; + public read(): Promise; public discoverySplashURL(options?: StaticImageURLOptions): string | null; public edit(data: GuildEditData, reason?: string): Promise; public editWelcomeScreen(data: WelcomeScreenEditData): Promise;