Commit Graph

71 Commits

Author SHA1 Message Date
Fleny
27c261fee2 formatter: Use semicolons (#4686)
I prefer semicolors, they also help avoiding certain pitfalls in JavaScript/TypeScript, such as the following code sample:
```js
const xyz = "test"
(something.else as string) = "another"
```
This results in a TypeError: "test" is not a function, this is because js thinks we are trying to call the string "test" as a function.
To fix this it requires a `;` somewhere before the `(`, such as `;(something ... ` which in my opinion is ugly and less clean overall.
2026-01-17 21:54:15 +01:00
8au
bd98b3ea36 fix: shard state incorrect after receiving ready event (#4671)
* fix: shard state incorrect after receiving ready event

* chore: updated website bot guild counts

* Revert "chore: updated website bot guild counts"

This reverts commit 1eda82bb0b.

---------

Co-authored-by: Fleny <Fleny113@outlook.com>
2026-01-15 19:17:46 +01:00
Fleny
96fa6e3a3f chore(gateway)!: Remove Shard forwardToBot (#4431)
forwardToBot has been removed in favor of directly using the events.message.

the default implementation for forwardToBot used to camelize the packet, however in most cases you want to preserve the snake_case, and for those that need camelCase it can be easly done in the message event function.

This also removes the gateway.preferSnakeCase option as it no longer has a use
2025-09-05 08:28:48 +02:00
Fleny
ae650a04d9 refactor(typedoc): update typedoc configuration and mark internal APIs as private (#4405)
The typedoc config is now only in the top-level config file instead of also being one per package
and all internal APIs are now marked as private so typedoc won't generate documentation for them, however we still need to export them to avoid typedoc warnings

The new internal APIs that are exported, since are marked as `@private` can have breaking changes without notices
2025-09-02 08:17:59 +02:00
Fleny
d79e672f5a feat(gateway): Use node:zlib zstd when available (#4373)
Co-authored-by: Link <link20050703@gmail.com>
2025-08-17 13:38:46 -07:00
Fleny
d50d47ec99 feat(gateway)!: Rename shard.events.identified to shard.events.ready (#4334) 2025-08-14 08:29:19 +02:00
Fleny
f199bbf71c build(dev-deps): Update biome to v2 (#4246)
* update biome to v2

* Run biome check --write

* Update biome.jsonc

Co-authored-by: Link <lts20050703@gmail.com>

* Fix config error

* Bump biome version

* Update website/yarn.lock

* Update biome to 2.1.3

---------

Co-authored-by: Link <lts20050703@gmail.com>
2025-08-09 12:45:04 -05:00
Fleny
c86c512c60 fix(gateway): Pass down the logger to the buckets leakyBucket (#4303) 2025-08-08 20:55:21 +02:00
Fleny
27ab08a61d feat(gateway)!: Resharding with workers (#4206)
* feat(gateway): Rework how shards are resharded

* Make bigbot example use the resharding

* fix reshard doesn't increment the lastShardId

From my testing it works, however i don't know if we should just do
this or is there a better way

* revert createGatewayManager type changes
It breaks if you want to disable resharding, didn't think of that

* Fix typo (#4252)

* Apply suggestions from code review

Co-authored-by: Link <lts20050703@gmail.com>

---------

Co-authored-by: NotDemonix <90858555+NotDemonix@users.noreply.github.com>
Co-authored-by: Link <lts20050703@gmail.com>
2025-08-04 19:46:24 +02:00
Awesome Stickz
94040813bb feat(gateway): add options.spreadShardsInRoundRobin to spread shards in a round-robin manner (#4204)
* feat(gateway): add options.spreadShardsInRoundRobin to spread shards in a round-robin manner

* fix: forgor to add to gateway
2025-05-26 11:57:48 +05:30
Max Zargov
dff9df5a59 fix: Unexpected TypeError in GatewayManager.requestMembers (#4197)
* fix: Unexpected TypeError in `GatewayManager.requestMembers` debug message

closes #4196

* refactor: prefer JSON.stringify replacer parameter when serializing

* Order functions in utils.ts
2025-05-24 08:19:51 +02:00
Fleny
83d329c374 Remove shardIsReady (#4190) 2025-05-11 00:44:42 +05:30
Fleny
b14328240b refactor(gateway): Improve request identify flow (#4126)
* Improve request identify flow

* Use leaky bucket for requestIdentify

* Revert behavior change to identify

* Move approved request identify to requestIdentify

* Apply some suggestions from code review

Co-authored-by: Awesome Stickz <awesome@stickz.dev>

* move request identify to identify

* remove requestIdentify call from manager

* Add flag to skip requestIdentify in shard identify

* Add logs about socket creation

* Update newly added logs

* change "shard" to "Shard"

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
2025-05-10 16:46:30 +05:30
Awesome Stickz
5ebe549ada refactor: don't log about resuming in shard.handleClose (#4187) 2025-05-03 13:37:15 +02:00
Fleny
a624cad2ca fix(gateway): Keep state to avoid unwanted disconnections (#4138)
* Keep state to avoid unwanted disconnections

This keeps a state to know that we wanted to end the connection with a
code of 1000 or 1001 to avoid unwanted disconnections in case discord /
someone in the middle of the TCP connection (such as cloudflare) sends a
close code of 1000 or 1001.

* Apply suggestions from code review

Co-authored-by: Awesome Stickz <awesome@stickz.dev>

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
2025-02-25 13:23:12 +05:30
Fleny
0efc4543da fix(types, gateway)!: UpdatePresence inconsistencies (#4124)
* FIx Update Presence incosistencies

* use keyof typeof for DiscordUpdatePresence status

* Remove deprecated gateway types
2025-02-17 14:05:06 +05:30
Fleny
0fb403a332 refactor(types): split discord.ts into multiple files (#4088)
* Split reference types

* Split webhook events types

* Split interactions types

* Split gateway types

* Move application related types to applications.ts

* Split auditLog types

* Split autoModeration types

* Split channel types

* Split emoji types

* Split entitlement types

* Split guild types

* Split guildScheduledEvent types

* Split GuildTemplate types

* Split Invite types

* Split message types

* Split poll types

* Split sku types

* Split soundboard types

* Split stageInstances types

* Split sticker types

* Split subscription types

* Split user types

* Split voice types

* Split webhooks types

* Split oauth2 types

* Split opcodes types

* Split permission types

* Split teams types

* Sort out remaining types

* Rename to singlar to match discord docs
2025-02-01 18:32:42 +01:00
Fleny
ebbb5f5541 Fix double socket bug on zombie (#4109) 2025-01-31 01:18:45 +05:30
Max Zargov
65b70a0089 fix(shutdown): prevent resume when closed with GatewayCloseEventCodes.NormalClosure. (#4103)
* fix(shutdown): prevent resume when closed with `GatewayCloseEventCodes.NormalClosure` (1000).

* feat(GatewayCloseEventCodes): Add GoingAway (1001) close code.

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
2025-01-31 00:58:33 +05:30
qreepex
153408f286 fix(gateway): wrong calculation in calculateWorkerId() (#4096)
* Update manager.ts

Fix wrong calculation of workerId

* Update manager.ts

* spread incrementally

* fix usage of incorrect variable

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
2025-01-31 00:50:41 +05:30
Awesome Stickz
6abf453319 fix(gateway): set a nonce if none is provided for gateway.requestMembers (#4057)
* fix(gateway): set a nonce if none is provided for gateway.requestMembers

* chore: update comment for gateway.requestMembers to clarify about gateway.cache.requestMembers.enabled

* chore: make the comment better

* fix: add node:crypto import to deno import map
2024-12-27 20:43:01 +05:30
Fleny
618d8dd108 fix(gateway): close not resolving in some cases (#4046)
* Fix close not resolving in some cases

* update some logging in close()
2024-12-10 11:01:51 +05:30
Awesome Stickz
91b5b196bd fix(gateway): shard.resume() ending in a loop of constant resumes (#4017)
* fix(gateway): shard.resume() ending in a loop of constant resumes

* refactor(gateway): change shard.close() to async and move resolve logic to shard.close()

* refactor: wait for shard.close() promise in gateway.shutdown()

* Fix integration test

---------

Co-authored-by: Fleny <fleny113@outlook.com>
2024-11-27 13:24:07 +01:00
Fleny
cfdf77027a refactor(types,utils,rest,bot)!: Cleanup types & files (#3951)
* Cleanup some un-used & sort types, split files

* Remove commented code from reverse/component.ts

* Fix type error on the bot E2E test

* Add comment, remove DiscordInteractionResponse

* Remove camel.ts

* Error on unusedImport, refactor type imports

* Run biome check

* fix: typo

* Update comments for skuId and defaultValues

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
2024-11-09 17:27:55 +01:00
Fleny
61acc5781f fix(gateway): Fix shard reconnecting in a loop (#3907)
* Attempt to fix shard reconnecting in a loop

* Add a bit of logging on READY & RESUMED

* Fix integration tests

* Revert turbo.json change

* Pass down the logger to LeakyBucket on HELLO

* Update some logs

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
2024-11-08 19:46:10 +05:30
Fleny
dfa7ff4045 feat(bot,rest,gateway,utils,types)!: Add soundboard support (#3919)
* Add soundboard support

* Add rest endpoints

* add comment to gateway requestSoundboardSounds

* Apply suggestions from code review

Co-authored-by: Awesome Stickz <awesome@stickz.dev>

* Update for discord/discord-api-docs#7207

* Update discord.ts

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
Co-authored-by: ITOH <to@itoh.at>
2024-11-05 19:44:37 +05:30
Fleny
7303a20d7a feat(gateway): Add transport compression support (#3815)
* Add zlib-stream transport-compression support

* remove external buffer, add the url param

this also fixes the CI Deno error

* use a queue and promises

instead of calling handleDiscordPacket from the callback of inflate.on('data')

* Add zstd support

* Re-add payload compression, set fzstd as optional, refactor

* add comment for decompressionPromisesQueue

* fix comment for isCompressed

* Don't create a new Uint8Array if the input is a Buffer

* Use a Uint8Array buffer instead of parsing incomplete JSON chunks

* remove old commented code

* Change ts expect error message

* use type import for ShardGatewayConfig

* Apply suggestions from code review

Co-authored-by: LTS (Link) <lts20050703@gmail.com>

---------

Co-authored-by: LTS (Link) <lts20050703@gmail.com>
2024-10-05 19:48:05 +02:00
Fleny
b3a8c866b4 feat(gateway): Expose makePresence from GatewayManager (#3851)
* Expose makePresence from GatewayManager

* Pass down the makePresence method on resharding shards

* inline makePresence option

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
2024-08-18 18:03:38 +02:00
Awesome Stickz
352887c215 feat(gateway): add resharding options to CreateGatewayManagerOptions (#3856) 2024-08-12 17:21:00 +02:00
Fleny
08a5e4456f fix(gateway): change logLevel for some resharding logs (#3855)
* change logLevel for some resharding logs

* Apply suggestions from code review

Co-authored-by: Awesome Stickz <awesome@stickz.dev>

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
2024-08-12 16:41:36 +02:00
Fleny
b7063580e0 feat(gateway): Make resharding.getSessionInfo optional (#3854)
* Make getSessionInfo optional

* change resharding.getSessionInfo to throw in checkIfReshardingIsNeeded

* Change warn message on spawnShards
2024-08-12 16:32:59 +02:00
Fleny
d6898f2b7b feat(gateway): Use the native WebSocket implementation when available (#3808)
* Use the native WebSocket implementation when available

* merge types.js import in shard

* Apply suggestions from code review

Co-authored-by: Awesome Stickz <awesome@stickz.dev>

* Do not use the WebSocket global on Node

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
2024-08-07 01:16:55 -05:00
Fleny
658acfb906 fix(gateway): Make heartbeat logging clearer (#3793)
* Make heartbeat logging clearer

* Update packages/gateway/src/Shard.ts

Co-authored-by: LTS20050703 <lts20050703@gmail.com>

---------

Co-authored-by: LTS20050703 <lts20050703@gmail.com>
Co-authored-by: Awesome Stickz <awesome@stickz.dev>
2024-07-30 23:36:03 -05:00
Fleny
ef4cc80c63 fix(bot,gateway): Fix (some) resharding bugs (#3792)
* Improve logging, keep shard collections separated

* revert to erroring in getSessionInfo. Fill the value in createBot

* removed useless if

* remove same number of shards check

Co-authored-by: Awesome Stickz <awesome@stickz.dev>

* Update manager.ts

* revert shard.events.message assignment for the spread

* use new Collection instead of .clear() + for-of

* Use fixed formula and note in the comment the inaccuracy

* Apply suggestions from code review

Co-authored-by: Awesome Stickz <awesome@stickz.dev>

* Add a remark to shardsFullPercentage to let the user know of the inaccuracy

* Update comment and add a clearInterval

The clearInterval is not strictly needed, however it is better to ensure there is always one and only one internal running at a given point

* revert check for all shards online
It was bugged as gateway.resharding.shards gets populated overtime and not instantly

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
2024-07-30 23:34:32 +05:30
Fleny
faaa8af48a clear offlineSendQueue after resolving all the promises (#3806)
Co-authored-by: Awesome Stickz <awesome@stickz.dev>
2024-07-30 04:15:21 +05:30
Fleny
fb8af2ba54 Use passed in logger instead of the imported one (#3765) 2024-07-20 16:54:38 -05:00
Fleny
919474069d chore: Migrate ESLint and prettier to Biome (#3634)
* Migrate eslint and prettier to biomejs

This does NOT include examples/bigbot as it has its own formatter

* Update to biome 1.8.0

* Readd dotenv dev dependency to rest

During a merge it got lost
2024-07-13 13:05:02 -05:00
Fleny
c7e0ccc87c always log close.code in debug, log close.code in info log (#3740) 2024-07-13 08:49:53 -05:00
Fleny
06664ed8bb fix(website): fix typedoc configuration (#3733)
* Fix typedoc CI crashse

And adapt the website to its new output

* Revert /tsconfig.json changes

* Update footer
2024-07-13 08:15:19 -05:00
Skillz4Killz
9ec197a9b3 Feat: resharding (#3014)
* feat: base work for reshardinf system

* fix: magic

* fix: y did i do this

* Fix typings for checkIfReshardingIsNeeded

* Update packages/gateway/src/manager.ts

Co-authored-by: Awesome Stickz <awesome@stickz.dev>

* Update packages/gateway/src/manager.ts

Co-authored-by: Awesome Stickz <awesome@stickz.dev>

* Update packages/gateway/src/manager.ts

Co-authored-by: LTS20050703 <lts20050703@gmail.com>

* fix: checking info.firstShardId, lastShardId

* fix: checking pending shards against totalShards instead of total used shards

* fix: changing new shards' message event when trying to change old shards' message event, causing it to be unable to process those events

* ci can stop crying now

* chore: specify usaage of updateGuildsShardId

* move executing updateGuildsShardId from shard.events to shard.forwardToBot as none of those events are being executed

* remove todo

* fix: calculateTotalShards() for bots in over 100k servers that don't have LBS and update comments

* use logger.debug in updateGuildsShardId instead of logger.warn

* feat: automatic resharding

* move auto resharding into gateway.spawnShards() and clear interval on gateway.shutdown()

* fix: both old shards and new shards processing events at the same time

* Copy all shard events provided in the options to new shards instead of just the mesasge event

* Added clearReshardingInterval parameter to manager.shutdown function so reshard function doesn't end up clearing this interval

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
Co-authored-by: Fleny <fleny113@outlook.com>
Co-authored-by: LTS20050703 <lts20050703@gmail.com>
2024-07-09 19:10:56 -05:00
Fleny
d0d810dfb5 Remove bot token from the shards logs (#3558) 2024-05-11 14:44:17 +00:00
Fleny
efe56fbe06 feat(bot, rest, gateway, utils): Add support for custom loggers (#3383)
* Add support for custom loggers

* update docs for loggerFactory

* Use union for names in the bot logger factory

* Fix formaating errors
2024-03-17 11:22:45 +00:00
Dorian Oszczęda
05a20c3bf8 refactor(bot, gateway, rest)!: Improve the interface of the createBot() function. (#3422)
* misc: Make the token a required property on the rest manager.

* refactor: Make unnecessarily required properties optional.

* misc: Improve interface for `createBot()` and allow passing in `transformers`/`handlers`.

* fix: Test made redundant by changes still being included.

* fix: Missing non-null assertions.

* fix: Benchmarks failing.

* misc: Remove `cache.requestMembers.pending` as an exposed option.

* style: Switch back to interface approach.
2024-03-07 22:39:48 -06:00
Fleny
e2a34082bc move rtt calculation to HeartbeatACK op code event (#3334) 2023-12-21 17:50:40 +00:00
Matthew Hatcher
a62f06c3e8 chore(deps)!: upgrade all deps to new major versions (#3195)
* chore(deps): upgrade deps to latest in range

* feat: upgrade node engine minimum

Closes #3173

* fix: typing errors after dep upgrade

* chore(deps): upgrade benchmark typescript version

* feat: upgrade eslint, lint staged, and es version

* fix: lint-staged failures

* style: formatting

* chore(deps): major version upgrades

* chore(deps): upgrade yarn to 4.0

* fix: lock typescript to 5.2.2 until ts-node/mocha fix

bug: https://github.com/mochajs/mocha/issues/5031

* chore: remove old Deno file

Went through history and can't find any reference to this after Deno rewrite

* chore(deps): more dep updates

* feat: typedoc config upgrade

* fix: temporarily reset docusaurus to main

* chore(deps): minor range upgrades

* chore(deps)!: docusaurus major upgrade

* revert: go back to original 2.x docusaurus
2023-12-04 13:57:15 +00:00
Fleny
c0615a2983 refactor(gateway)!: Remove guildMemberChunk shard event (#3151)
* Remove guildMemberChunk shard event

* use DiscordMemberWithUser for requestMembers
2023-10-18 22:21:46 +00:00
Fleny
a8f9e5ec1b feat: allow for custom function to send the payload to shards (#3149)
* Initial work for sendPayload

* fix docs comment
2023-10-15 04:30:58 +00:00
ITOH
bccbc73f77 fix(gateway): workerId calculation (#3126) 2023-09-19 20:22:52 +00:00
Skillz
b2f65c2acc fix: chunk resolve bug 2023-08-20 11:50:19 -05:00
Skillz
1c8442eb39 fix: broken nonce on large guild ids Closes #3066 2023-08-20 11:24:38 -05:00