discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

SyntaxError: Unexpected token '??=' #7125

Closed LucasB25 closed 2 years ago

LucasB25 commented 2 years ago

Issue description

4|panais | /home/pi/panais/node_modules/discord.js/src/rest/APIRequest.js:33 4|panais | agent ??= new https.Agent({ ...this.client.options.http.agent, keepAlive: true }); 4|panais | ^^^ 4|panais | SyntaxError: Unexpected token '??=' 4|panais | at wrapSafe (internal/modules/cjs/loader.js:1001:16) 4|panais | at Module._compile (internal/modules/cjs/loader.js:1049:27) 4|panais | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) 4|panais | at Module.load (internal/modules/cjs/loader.js:950:32) 4|panais | at Function.Module._load (internal/modules/cjs/loader.js:790:12) 4|panais | at Module.require (internal/modules/cjs/loader.js:974:19) 4|panais | at Module.Hook._require.Module.require (/usr/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:80:39) 4|panais | at require (internal/modules/cjs/helpers.js:93:18) 4|panais | at Object. (/home/pi/panais/node_modules/discord.js/src/rest/RESTManager.js:4:20) 4|panais | at Module._compile (internal/modules/cjs/loader.js:1085:14)

Code sample

none

discord.js version

13.3.1

Node.js version

16+

Operating system

raspberry

Priority this issue should have

Low (slightly annoying)

Which partials do you have configured?

GUILD_MEMBER, MESSAGE

Which gateway intents are you subscribing to?

GUILDS, GUILD_MEMBERS, GUILD_VOICE_STATES, DIRECT_MESSAGES

I have tested this issue on a development release

No response

kyranet commented 2 years ago

You're not using Node.js v16.6.0+. You can run node -v to confirm this.

Please upgrade using the instructions from https://nodejs.org/en/

LucasB25 commented 2 years ago

use v16.13.1

kyranet commented 2 years ago

??= is valid syntax and is compatible since late v15 (and in turn, v16.0.0 and newer also support it): https://node.green/#ES2021-features-Logical-Assignment

This operator is called Logical Nullish Assignment, and is being used correctly, as such, this is valid syntax.

It just isn't in older versions, make sure you're running the correct version (e.g. your application is running the version you have) and not an older one.

LucasB25 commented 2 years ago

I am using a stable version so I don't know why I have this error

DTrombett commented 2 years ago

I am using a stable version so I don't know why I have this error

Try logging process.version and see the output

Irev-Dev commented 2 years ago

I get that v16 is a lts version, but I'm curious as to what the intention is behind insisting on such a new version of node is?

Currently I can't use this library on our stack because we use aws and they only support up to version 14. https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html

Can I use and old release of discord.js that supports v14?

suneettipirneni commented 2 years ago

I get that v16 is a lts version, but I'm curious as to what the intention is behind insisting on such a new version of node is?

Currently I can't use this library on our stack because we use aws and they only support up to version 14. docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html

Can I use and old release of discord.js that supports v14?

Node 16 is the LTS, and there's no version of discord.js v13 that is built for node 14. I see your using lambda functions but I don't think you can use discord.js currently with serverless setups, it's meant to be run on a VPS-like setup.

Irev-Dev commented 2 years ago

Yup, v16 is lts, still curious if there's rational behind using the latest lts version. Is it simply to stay current with latest stable?

So is there a version of discord.js (<13) that supports node14?

it's meant to be run on a VPS-like setup.

Too bad :( is that explicit anywhere in the docs?

suneettipirneni commented 2 years ago

So is there a version of discord.js (<13) that supports node14?

Yeah there's version 12, but it will most likely break in the coming months once discord updates their API.

Too bad :( is that explicit anywhere in the docs?

No, but it's mostly implied by the fact it utilizes discord gateway which can't be used work with serverless functions. This because serverless can't maintain a live connection to the gateway.

Irev-Dev commented 2 years ago

but it will most likely break in the coming months once discord updates their API.

Sorry about all the questions, is there a planned depreciation for this API version? I tried a quick google along the lines for v8 API depreciation, but couldn't see anything.

serverless can't maintain a live connection to the gateway.

at least running locally I've seen no problem connecting when sending a message and reconnecting again later, we don't need to respond to messages so listening to events is not a problem.

suneettipirneni commented 2 years ago

Sorry about all the questions, is there a planned depreciation for this API version? I tried a quick google along the lines for v8 API depreciation, but couldn't see anything.

I wasn't referring to v8 breaking, I was referring to discord.js version 12 not working. The change that would break v12 of discord.js is caused by this.

at least running locally I've seen no problem connecting when sending a message and reconnecting again later, we don't need to respond to messages so listening to events is not a problem.

Running it locally is like running on a vps, it's just on your local machine. That setup doesn't translate to serverless lambda functions. Again, as stated earlier discord.js will not work if it doesn't maintain its websocket connection to the gateway. Events are the basis of making a discord bot, so I don't understand how you could make a bot and simply "ignore" them.

advaith1 commented 2 years ago

If you're only sending messages from a serverless context, you should do a direct HTTP POST request instead of using a library such as discord.js that connects to the websocket gateway

Irev-Dev commented 2 years ago

Thanks @advaith1, that's was useful advice and what we went with :)

https://github.com/Irev-Dev/cadhub/pull/600#event-5937677799

armsnyder commented 2 years ago

No, but it's mostly implied by the fact it utilizes discord gateway which can't be used work with serverless functions.

I would like to explain the use case, because I disagree that the gateway API cannot or should not work with serverless functions. There is no issue using this library in order to send events, and it would be very useful where there is no HTTP API for something and we need to use the gateway API.

Firstly, even in the worst case it's ok to establish a new connection and close it in the same serverless function invocation. For infrequently invoked functions this is fine.

Even better, there is a strategy for keeping connections open for you across multiple serverless invocations, called top-level await.

Here's how it would look for discord.js.

import Discord from 'discord.js'

const bot = new Discord.Client();
bot.login("....")

await new Promise((resolve, reject) => {
    bot.on('ready', resolve)
    bot.on('error', reject)
})

export async function handler(event) {
    bot.user.setPresence({ activities: [{ name: 'My activity' }] })
}
almostSouji commented 2 years ago

This has strayed very far off-topic from the original issue. Please create a GitHub discussion or thread on the discord server if you want to continue discussing this.

The approach you outlined does not scale, at all, because of identify limits.

armsnyder commented 2 years ago

The document I linked explains how context reuse is possible, and would prevent the need to identify on every invocation.

I appreciate you wanting to close discussion here. I don't intend to keep it going. 😄