fix: client.connect() issues

This commit is contained in:
Skillz
2023-02-28 12:53:27 -06:00
parent 72113e4fc4
commit 25ac315230
6 changed files with 59 additions and 31 deletions
+6 -27
View File
@@ -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<any> {
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))
+1 -1
View File
@@ -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',
+2 -1
View File
@@ -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
+2 -2
View File
@@ -1,6 +1,6 @@
import type { Camelize, Snakelize } from '@discordeno/types'
export const camelize = <T>(object: T): Camelize<T> => {
export function camelize <T>(object: T): Camelize<T> {
if (Array.isArray(object)) {
return object.map((element) => camelize(element)) as Camelize<T>
}
@@ -16,7 +16,7 @@ export const camelize = <T>(object: T): Camelize<T> => {
return object as Camelize<T>
}
export const snakelize = <T>(object: T): Snakelize<T> => {
export function snakelize <T>(object: T): Snakelize<T> {
if (Array.isArray(object)) {
return object.map((element) => snakelize(element)) as Snakelize<T>
}
+8
View File
@@ -0,0 +1,8 @@
{
"label": "Migrating",
"position": 99,
"link": {
"type": "generated-index",
"description": "Guides for migrating to discordeno from other libraries."
}
}
+40
View File
@@ -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.