From 25ac315230af5b34493bf70aea572a1661b44e51 Mon Sep 17 00:00:00 2001 From: Skillz Date: Tue, 28 Feb 2023 12:53:27 -0600 Subject: [PATCH] fix: client.connect() issues --- packages/client/src/Client.ts | 33 +++------------- packages/client/src/RequestHandler.ts | 2 +- packages/client/src/Structures/Permission.ts | 3 +- packages/utils/src/casing.ts | 4 +- website/docs/migrating/_category_.json | 8 ++++ website/docs/migrating/eris.md | 40 ++++++++++++++++++++ 6 files changed, 59 insertions(+), 31 deletions(-) create mode 100644 website/docs/migrating/_category_.json create mode 100644 website/docs/migrating/eris.md diff --git a/packages/client/src/Client.ts b/packages/client/src/Client.ts index bfd599092..bd27737c0 100644 --- a/packages/client/src/Client.ts +++ b/packages/client/src/Client.ts @@ -267,7 +267,8 @@ export class Client extends EventEmitter { this.options = { apiVersion: options.apiVersion ?? 10, - allowedMentions: this._formatAllowedMentions(options.allowedMentions), + // This is set below, + allowedMentions: {}, defaultImageFormat: options.defaultImageFormat ?? 'png', defaultImageSize: options.defaultImageSize ?? 128, proxyURL: options.proxyURL, @@ -288,6 +289,8 @@ export class Client extends EventEmitter { reconnectDelay: options.reconnectDelay ?? ((lastDelay, attempts) => Math.pow(attempts + 1, 0.7) * 20000), } + this.options.allowedMentions = this._formatAllowedMentions(options.allowedMentions) + this.guildShardMap = {} this.requestHandler = new RequestHandler(this, {}) @@ -413,31 +416,6 @@ export class Client extends EventEmitter { } } - /** Make a request to the discord api. */ - async makeRequest(data: RequestData) { - return await fetch(`${this.proxyURL}/${this.BASE_URL}/${data.url}`, { - method: data.method, - headers: { - 'Content-Type': 'application/json', - Authorization: this.proxyRestAuthorization, - 'X-Audit-Log-Reason': data.reason ?? '', - ...(data.headers ?? {}), - }, - body: data.body - ? JSON.stringify(data.body, (str) => { - if (str.endsWith('ID')) str = str.substring(0, str.length - 2) + 'Id' - - return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`) - }) - : undefined, - }) - .then(async (res) => await res.json()) - .catch((error) => { - console.log(error) - return null - }) - } - /** Make a GET request to the discord api. */ async get(url: string): Promise { return await this.requestHandler.discordeno.get(url) @@ -1805,7 +1783,8 @@ export class Client extends EventEmitter { return await get((_before ?? !_after) && messages[messages.length - 1].id, _after && messages[0].id) } - return await get(options.before, options.after) + // @ts-expect-error todo use typeguards here + return await get(options.before, options.after); } const messages = await this.get(CHANNEL_MESSAGES(channelID)) diff --git a/packages/client/src/RequestHandler.ts b/packages/client/src/RequestHandler.ts index 729e79bbc..f5a37853c 100644 --- a/packages/client/src/RequestHandler.ts +++ b/packages/client/src/RequestHandler.ts @@ -37,7 +37,7 @@ export class RequestHandler { { // agent: client.options.agent || null, agent: null, - baseURL: client.BASE_URL, + baseURL: "https://discord.com/api", decodeReasons: true, disableLatencyCompensation: false, domain: 'discord.com', diff --git a/packages/client/src/Structures/Permission.ts b/packages/client/src/Structures/Permission.ts index bb751bb17..475a9ff6b 100644 --- a/packages/client/src/Structures/Permission.ts +++ b/packages/client/src/Structures/Permission.ts @@ -2,7 +2,8 @@ import { BitwisePermissionFlags } from '@discordeno/types' import { Base } from '../Base.js' import type { BigString } from '../Client.js' -import { PermissionClientStrings, Permissions } from '../Constants.js' +import type { PermissionClientStrings } from '../Constants.js' +import { Permissions } from '../Constants.js' export class Permission { allow: bigint diff --git a/packages/utils/src/casing.ts b/packages/utils/src/casing.ts index 7164e2eb7..6ae619c82 100644 --- a/packages/utils/src/casing.ts +++ b/packages/utils/src/casing.ts @@ -1,6 +1,6 @@ import type { Camelize, Snakelize } from '@discordeno/types' -export const camelize = (object: T): Camelize => { +export function camelize (object: T): Camelize { if (Array.isArray(object)) { return object.map((element) => camelize(element)) as Camelize } @@ -16,7 +16,7 @@ export const camelize = (object: T): Camelize => { return object as Camelize } -export const snakelize = (object: T): Snakelize => { +export function snakelize (object: T): Snakelize { if (Array.isArray(object)) { return object.map((element) => snakelize(element)) as Snakelize } diff --git a/website/docs/migrating/_category_.json b/website/docs/migrating/_category_.json new file mode 100644 index 000000000..49a1db017 --- /dev/null +++ b/website/docs/migrating/_category_.json @@ -0,0 +1,8 @@ +{ + "label": "Migrating", + "position": 99, + "link": { + "type": "generated-index", + "description": "Guides for migrating to discordeno from other libraries." + } +} diff --git a/website/docs/migrating/eris.md b/website/docs/migrating/eris.md new file mode 100644 index 000000000..5f5afdbb1 --- /dev/null +++ b/website/docs/migrating/eris.md @@ -0,0 +1,40 @@ +--- +sidebar_position: 1 +sidebar_label: Eris To Discordeno +--- + +# Migrating From Eris To Discordeno Guide + +## Understanding The Goals of This Guide + +This guide is a quick-paced walkthrough meant for explaining the migration process for Eris bots to using Discordeno. If you are not sure whether you want to migrate, please read [Should I Migrate?](../intro.md) + +## TypeScript + +I really hope you wrote your bot with TypeScript as it is going to save you a lot of time. If you have not written it in TypeScript, you should probably start now. Using this migrating as a push to switch to TypeScript. + +If your bot is using TypeScript, this migration will be a lot easier. + +## Getting Started + +Open up a terminal and run `tsc --watch --noEmit` to keep TypeScript warning of us of errors that may appear as we migrate. + +Once you are ready, let's go ahead and install Discordeno, while we are at it we can uninstall eris. Open a new terminal and run the following command for the package manager you use: + +```ts +// NPM users +npm uninstall eris && npm install @discordeno/client@19.0.0-next.99fbe1e +// Yarn users +yarn remove eris && yarn add @discordeno/client@19.0.0-next.99fbe1e +``` + +:::caution +Currently, Discordeno v19 is in development. This is why you can not install it as @discordeno/client but have to specify the commit version. Once this version is released, it will be as easy as `@discordeno/client`. +::: + +We are going to use [NayuBot](https://github.com/AwesomeStickz/Nayu-Bot) which is a small bot written in TypeScript with Eris and is open source as the example we are going to migrate to Discordeno. + +At this moment in time, the tsc terminal(from now one we are going to refer to this as TypeScript), is telling us we have 13 errors. This is because although we removed eris, we need to fix any imports it may have. So let's run a search in VSC to find any `from 'eris';` and `from "eris"`. We need to replace these with `from '@discordeno/client'` and `from "@discordeno/client"`. + +We now have 20 errors in TypeScript. Some of these errors are not Discordeno/Eris related. However, some of them are. Let's discuss them below. +