Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support discord.js #2077

Closed
2 tasks done
ThatOneBro opened this issue Feb 15, 2023 · 23 comments · Fixed by #2913
Closed
2 tasks done

Support discord.js #2077

ThatOneBro opened this issue Feb 15, 2023 · 23 comments · Fixed by #2913
Labels
ecosystem Something that relates to package or framework compatibility enhancement New feature or request

Comments

@ThatOneBro
Copy link
Contributor

ThatOneBro commented Feb 15, 2023

We've long had requests for discord.js support. It doesn't seem too far away now that we've got undici.Agent and a workaround for the worker_threads requirement.

Currently it looks like we need the following to finish support for discord.js:

@ThatOneBro ThatOneBro added ecosystem Something that relates to package or framework compatibility enhancement New feature or request labels Feb 15, 2023
@net-tech
Copy link

Is there any update on

response.arrayBuffer() hangs when called more than once (no tracking yet)

@ThatOneBro
Copy link
Contributor Author

@net-tech I think a fix may have just made it into main, I'll check tomorrow and update this issue accordingly

@ThatOneBro
Copy link
Contributor Author

Update: response.arrayBuffer() no longer hangs, but when calling the function Bun reports undefined is not a function although when console.log()ing response.arrayBuffer just before calling it, it seems to be defined as the appropriate function. Will take a closer look at this soon.

@Mw3y
Copy link

Mw3y commented Mar 20, 2023

I got the same error with the Winston logging library. It looks like it's a general issue.

@net-tech
Copy link

Update: response.arrayBuffer() no longer hangs, but when calling the function Bun reports undefined is not a function although when console.log()ing response.arrayBuffer just before calling it, it seems to be defined as the appropriate function. Will take a closer look at this soon.

Hey any update on that closer look? Not trying to be a bother, just wondering if there's been any progress.

@trnxdev
Copy link
Contributor

trnxdev commented Apr 2, 2023

I just want to say I am experiencing the same issue

bun version: 0.5.8
discord.js version: 14.9.0

68 |   }
69 | 
70 |   async arrayBuffer() {
71 |     this.#consume();
72 | 
73 |     return await this.#response.arrayBuffer();
                    ^
TypeError: undefined is not a function
      at undici:73:17
      at arrayBuffer (undici:71:22)
      at parseResponse (/mnt/c/Users/tiramify/Documents/bunsh/node_modules/.pnpm/@discordjs+rest@1.7.0/node_modules/@discordjs/rest/dist/index.js:493:29

@ThatOneBro
Copy link
Contributor Author

Gonna check on this one later today. There was an issue in our fetch implementation that was causing this but I think it might have been merged into canary already.

@GillHoang
Copy link

GillHoang commented Apr 5, 2023

Gonna check on this one later today. There was an issue in our fetch implementation that was causing this but I think it might have been merged into canary already.

i can't use bun for discord.js when update to latest bun build
image

@trnxdev
Copy link
Contributor

trnxdev commented Apr 5, 2023

i can't use bun for discord.js when update to latest bun build

Same: (token is there, it's just invisible cuz of my blur)

Screenshot 2023-04-05 140109

@GillHoang
Copy link

i can't use bun for discord.js when update to latest bun build

Same: (token is there, it's just invisible cuz of my blur)

Screenshot 2023-04-05 140109

just wait util have new update, .-. i very want to use bun intead of tsc and run

brianprost added a commit to brianprost/abrys-fam-bot that referenced this issue Apr 30, 2023
@redraskal
Copy link
Contributor

I just want to say I am experiencing the same issue

bun version: 0.5.8 discord.js version: 14.9.0

68 |   }
69 | 
70 |   async arrayBuffer() {
71 |     this.#consume();
72 | 
73 |     return await this.#response.arrayBuffer();
                    ^
TypeError: undefined is not a function
      at undici:73:17
      at arrayBuffer (undici:71:22)
      at parseResponse (/mnt/c/Users/tiramify/Documents/bunsh/node_modules/.pnpm/@discordjs+rest@1.7.0/node_modules/@discordjs/rest/dist/index.js:493:29

res.headers['content-type'] returns undefined (I think you have to use res.headers.get("content-type") with Bun?), so res.body.json() is never called.

export async function parseResponse(res: Dispatcher.ResponseData): Promise<unknown> {
	const header = parseHeader(res.headers['content-type']);
	if (header?.startsWith('application/json')) {
		return res.body.json();
	}

	return res.body.arrayBuffer();
}

https://github.com/discordjs/discord.js/blob/f27631175a97b8a7620efedc24073fdb944cb728/packages/rest/src/lib/utils/utils.ts#L64

@trnxdev
Copy link
Contributor

trnxdev commented May 4, 2023

Now it managed to succeed, after I downloaded @discordjs/rest package and replace the source code in dist/index.js (res.headers.get('content-type') instead of res.headers['content-type']) it managed to go past client.login!

Now the next problem is

612 |     super();
613 |     this.strategy = strategy;
614 |     this.id = id;
615 |   }
616 |   async connect() {
617 |     const promise = this.initialConnectResolved ? Promise.resolve() : (0, import_node_events2.once)(this, "ready" /* Ready */);
                                                                     ^
TypeError: Not enough arguments
      at /home/tiramify/Documents/newbun/node_modules/.pnpm/@discordjs+ws@0.8.2/node_modules/@discordjs/ws/dist/index.js:617:66
      at connect (/home/tiramify/Documents/newbun/node_modules/.pnpm/@discordjs+ws@0.8.2/node_modules/@discordjs/ws/dist/index.js:616:18)
      at /home/tiramify/Documents/newbun/node_modules/.pnpm/@discordjs+ws@0.8.2/node_modules/@discordjs/ws/dist/index.js:1113:20
      at connect (/home/tiramify/Documents/newbun/node_modules/.pnpm/@discordjs+ws@0.8.2/node_modules/@discordjs/ws/dist/index.js:1110:18)
      at /home/tiramify/Documents/newbun/node_modules/.pnpm/@discordjs+ws@0.8.2/node_modules/@discordjs/ws/dist/index.js:1367:10

Also I just found more res.headers[''] instead of res.headers.get() in the discord.js rest file, after changing everything I also needed to change

   if (header === void 0 || typeof header === "string") {
    return header;
  }

to

    if (header == void 0 || typeof header === "string") {
    return header;
  }

because one of the headers returned is equal to null.

it still however returned Promise.resolve error

UPDATE: The error is not in Promise.resolve but rather in import_node_events2.once(this, "ready")

after a bit of trials I found out when you use

import_node_events2.once(this, "ready", {})

it works. But there are still a lot of errors.

@trnxdev
Copy link
Contributor

trnxdev commented May 26, 2023

After I and some guys on the discord server investigated the issue (we still are) we found out that these versions work:

14.9.0: works (normal bun)
14.8.0: works (normal bun)
13.16.0: works (normal bun)
13.14.0: works (normal bun)

Anything above 14.9.0 doesn't work, Anything below 14.0.0 requires modified

node_modules/discord.js:
src/rest/APIRequest.js,
src/util/DataResolver.js,
src/util/Util.js.

you need to remove the line const fetch = require('node-fetch') from every file i mentioned above.

For anything above 14.0.0 you should (example below made for 14.9.0)

node_modules/@discordjs/rest/dist/index.js:
change res.headers["(text)"] to res.headers.get("(text)") and maybe on line 453 === void 0 with just == void 0

I didn't fully test this, but the bot seems to launch.

@trnxdev
Copy link
Contributor

trnxdev commented May 26, 2023

image
(discord.js version on the picture,14.9.0)

@paperdave
Copy link
Collaborator

discord.js is working on this branch
discord_js

didn't test every feature of it.

if you're curious why arrayBuffer() was breaking, we were trying to call .then on what we thought was a promise but it was actually just the value.

@GillHoang
Copy link

discord.js is working on this branch discord_js

didn't test every feature of it.

if you're curious why arrayBuffer() was breaking, we were trying to call .then on what we thought was a promise but it was actually just the value.

how you can do that

@trnxdev
Copy link
Contributor

trnxdev commented May 31, 2023

how you can do that

It's not out yet, just wait a couple days and upgrade your version of bun.

@GillHoang
Copy link

how you can do that

It's not out yet, just wait a couple days and upgrade your version of bun.
hmm after i edit something, it works, but when using class Client for extend, i got some error :((

@trnxdev
Copy link
Contributor

trnxdev commented May 31, 2023

how you can do that

It's not out yet, just wait a couple days and upgrade your version of bun.
hmm after i edit something, it works, but when using class Client for extend, i got some error :((

Do you have discord? It's more comfortable for me to help you there than on GitHub.
Here is a server link in case you do https://discord.gg/bun-876711213126520882

@GillHoang
Copy link

how you can do that

It's not out yet, just wait a couple days and upgrade your version of bun.
hmm after i edit something, it works, but when using class Client for extend, i got some error :((

Do you have discord? It's more comfortable for me to help you there than on GitHub. Here is a server link in case you do https://discord.gg/bun-876711213126520882

yep i am in bun server, my discord is GillCoder#7005

@ImLunaHey
Copy link
Contributor

Still hitting issue with discord.js 😢

#1987
#2688 (comment)

@Jarred-Sumner
Copy link
Collaborator

Jarred-Sumner commented Jun 3, 2023 via email

@ImLunaHey
Copy link
Contributor

@Jarred-Sumner happy to provide more details here or in the discord. Same username there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ecosystem Something that relates to package or framework compatibility enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants