From 8beb036741c057ada0d86cfb2331bea3c325e310 Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Sat, 23 Jan 2021 11:51:35 +0100 Subject: [PATCH] feat(handlers): add getGatewayBot() & getApplicationInformation() (#428) * feat(handlers): add getGatewayBot() * refactor(bot): use getGatewayBot() to get gateway data * feat(handlers): add getApplicationInformation * add(handlers): getApplicationInformation jsdoc * Add oauth required types * create separate file * delete misc * OAuthApplication * forgot to update import * idk why that was here --- src/api/handlers/gateway.ts | 10 ++++++++++ src/api/handlers/oauth.ts | 10 ++++++++++ src/bot.ts | 12 ++++-------- src/types/oauth.ts | 37 +++++++++++++++++++++++++++++++++++++ src/types/teams.ts | 31 +++++++++++++++++++++++++++++++ src/util/constants.ts | 3 +++ 6 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 src/api/handlers/gateway.ts create mode 100644 src/api/handlers/oauth.ts create mode 100644 src/types/oauth.ts create mode 100644 src/types/teams.ts diff --git a/src/api/handlers/gateway.ts b/src/api/handlers/gateway.ts new file mode 100644 index 000000000..8ea84159c --- /dev/null +++ b/src/api/handlers/gateway.ts @@ -0,0 +1,10 @@ +import { RequestManager } from "../../rest/request_manager.ts"; +import { DiscordBotGatewayData } from "../../types/mod.ts"; +import { endpoints } from "../../util/constants.ts"; + +/** Get the bots Gateway metadata that can help during the operation of large or sharded bots. */ +export function getGatewayBot() { + return RequestManager.get( + endpoints.GATEWAY_BOT, + ) as Promise; +} diff --git a/src/api/handlers/oauth.ts b/src/api/handlers/oauth.ts new file mode 100644 index 000000000..f1738cc05 --- /dev/null +++ b/src/api/handlers/oauth.ts @@ -0,0 +1,10 @@ +import { RequestManager } from "../../rest/request_manager.ts"; +import { OAuthApplication } from "../../types/oauth.ts"; +import { endpoints } from "../../util/constants.ts"; + +/** Returns the bot's OAuth2 application object without `flags`. */ +export function getApplicationInformation() { + return RequestManager.get(endpoints.OAUTH2_APPLICATION) as Promise< + OAuthApplication + >; +} diff --git a/src/bot.ts b/src/bot.ts index 017d0c722..b72475b6f 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -1,11 +1,11 @@ -import { RequestManager } from "./rest/request_manager.ts"; +import { getGatewayBot } from "./api/handlers/gateway.ts"; import { BotConfig, DiscordBotGatewayData, EventHandlers, Intents, } from "./types/mod.ts"; -import { baseEndpoints, endpoints, GATEWAY_VERSION } from "./util/constants.ts"; +import { baseEndpoints, GATEWAY_VERSION } from "./util/constants.ts"; import { spawnShards } from "./ws/shard_manager.ts"; export let authorization = ""; @@ -45,9 +45,7 @@ export async function startBot(config: BotConfig) { authorization = `Bot ${config.token}`; // Initial API connection to get info about bots connection - botGatewayData = await RequestManager.get( - endpoints.GATEWAY_BOT, - ) as DiscordBotGatewayData; + botGatewayData = await getGatewayBot(); // Explicitly append gateway version and encoding botGatewayData.url += `?v=${GATEWAY_VERSION}&encoding=json`; @@ -102,9 +100,7 @@ export async function startBigBrainBot(data: BigBrainBotConfig) { ); // Initial API connection to get info about bots connection - botGatewayData = await RequestManager.get( - endpoints.GATEWAY_BOT, - ) as DiscordBotGatewayData; + botGatewayData = await getGatewayBot(); if (!data.wsURL) proxyWSURL = botGatewayData.url; await spawnShards( diff --git a/src/types/oauth.ts b/src/types/oauth.ts new file mode 100644 index 000000000..859bc87c7 --- /dev/null +++ b/src/types/oauth.ts @@ -0,0 +1,37 @@ +import { UserPayload } from "./guild.ts"; +import { TeamPayload } from "./teams.ts"; + +export interface OAuthApplication { + /** id of the app */ + id: string; + /** the name of the app */ + name: string; + /** the icon hash of the app */ + icon: string | null; + /** the description of the app */ + description: string; + /** an array of rpc origin urls, if rpx is enabled */ + rpc_origins?: string[]; + /** when false only app owner can join the app's bot to guilds */ + bot_public: boolean; + /** when true the app's bot will only join upon completion of the full oauth2 code grant flow */ + bot_require_code_grand: boolean; + /** partial user object containing info on the owner of the application */ + owner: Partial; + /** if this application is a game sold on Disccord, this field will be the summary field for the store page of its primary sku */ + summary: string; + /** the base64 enccoded key for the GameSDK'S GetTicket */ + verify_key: string; + /** if the application belongs to a team, this will be a list of the members of that team */ + team: TeamPayload | null; + /** if this application is a game sold on Discord, this field will be the guild to which it has been linked */ + guild_id?: string; + /** if this application is a game sold on Discord, this field will be the id of the "Game SKU" that is created, if exists */ + primary_sku_id?: string; + /** if this application is a game sold on Discord, this field will be the URL slug that links to the store page */ + slug?: string; + /** if this application is a game sold on Discord, this field wil be the hash of the image on store embeds */ + cover_image?: string; + /** the application's public flags */ + flags: number; +} diff --git a/src/types/teams.ts b/src/types/teams.ts new file mode 100644 index 000000000..425c2b66d --- /dev/null +++ b/src/types/teams.ts @@ -0,0 +1,31 @@ +import { UserPayload } from "./guild.ts"; + +/** https://discord.com/developers/docs/topics/teams#data-models-team-object */ +export interface TeamPayload { + /** a hash of the image of the team's icon */ + icon: string | null; + /** the unique id of the team */ + id: string; + /** the members of the team */ + members: TeamMembersPayload[]; + /** the user id of the current team owner */ + owner_user_id: string; +} + +/** https://discord.com/developers/docs/topics/teams#data-models-team-members-object */ +export interface TeamMembersPayload { + /** the user's membership state on the team */ + membership_state: MembershipState; + /** will always be ["*"] */ + permissions: string[]; + /** the id of the parent team of which they are a member */ + team_id: string; + /** the avatar, discriminator, id, and username of the user */ + user: Partial; +} + +/** https://discord.com/developers/docs/topics/teams#data-models-membership-state-enum */ +export enum MembershipState { + INVITED = 1, + ACCEPTED, +} diff --git a/src/util/constants.ts b/src/util/constants.ts index 26df7ef7f..bde99b4a8 100644 --- a/src/util/constants.ts +++ b/src/util/constants.ts @@ -164,4 +164,7 @@ export const endpoints = { USER_DM: `${baseEndpoints.BASE_URL}/users/@me/channels`, USER_CONNECTIONS: `${baseEndpoints.BASE_URL}/users/@me/connections`, USER_NICK: (guildID: string) => `${GUILDS_BASE(guildID)}/members/@me/nick`, + + // oAuth2 + OAUTH2_APPLICATION: `${baseEndpoints.BASE_URL}/oauth2/applications/@me`, };