Trying to get Client from ShardingManager for Test Suite
Hello!
I'm trying to create a test suite for my Discord Bot.
I had a working version without sharding, but once I added sharding to simulate the actual production environment, it kept spawning 2 instances of the bot.
27 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
- ✅
Marked as resolved by OPTo counteract it, I tried using
shard.eval
to get the Client instance from ShardClientUtil
, but I keep reaching the following error:Here is a gist with the code and the last working version:
https://gist.github.com/Rocky43007/75803ae37f5c3e832bd2d4371f355296
just to clarify, the error shown still points to
at Client.toJSON
, which suggests that a Client
is still being flattened, which is what lead me to believe that you hadn't updated every instance of shard.eval()
this is why I was asking for the full updated codeAh, I did update
tester.ts
with the latest version I tested
you're still returning the client here
the string is being returned from the event listener callback
though as mentioned previously, you still wouldn't have access to
runAllTests
since this is being evaluated in a different contextHm.
(and as a side note, the
Client
being returned is also the reason you were receiving your original error on worker mode)👍
Welp, I tried this and it didn't work because Client is always null:
Gist: https://gist.github.com/Rocky43007/75803ae37f5c3e832bd2d4371f355296#file-tester_v3-ts
(At this point, I'm just going to document what it took to get it working [if it ever works] for future devs)
I think you may be misunderstanding what I mean by "this is being evaluated in a different context"
the eval'd function executes on the client's process/worker
it is not executed here in this scope where
client
is defined
as mentioned previously, this method is for sending code to the shard to execute there
there is no means of returning the client back to the ShardingManager
Ohhhhh, that makes more sense
:faceDesk:
So, I've changed gears and I'm trying to have the mock_interaction generated before being sent to a test event on client. I've had it work with
c.emit("test," "test")
with the bot's client.on("test")
outputting test
in the console.
However, the second I try this:
I now get Error [ReferenceError]: inter is not defined
. Now, this should be working because I'm running the code and just sending an object now to the client. But... it's not.this is the same issue
Oh, come on!
however for this specifically, you can send the mock interaction as part of the
context
assuming it's json serializable:guide: Sharding: Additional information - Eval arguments
The context option only accepts properties which are JSON-serializable. This means you cannot pass complex data types in the context directly. For example, if you sent a User instance, the function would receive the raw data object.
read more
(actually
<Shard>.eval()
seems to take the second param as the context as opposed to a context
option in an options object)So something like ?
Which, doesn't work. But that might be because I just don't understand the docs.
Ok, figured it out!
Got a better error this time:
TypeError: Do not know how to serialize a BigInt
well do you have a bigint in your mock interaction?
as mentioned previously, the context needs to be json serializable
Maybe it's the bitfields?
It was the bitfields
Wait, can client emit to the sharding manager?
that depends on what you want to emit
<Client>.shard.send()
exists (though I wouldn't recommend using it unless you know what you're doing), but it's not as if it can send anything that can't be sent through an evalI just need it to send a string saying
testComplete
and that's it.
Hm. client.shard.send()
should work.to be more specific, I believe it emits the
message
event on the ShardingManager
(which it seems that this is no longer documented despite <ShardClientUtil>.send()
being documented)
but in general this would be leaving the scope of what we supportFair enough. I did get it working though. Thanks for all your help!
Also, JSON serializable can't send functions if I'm correct?
correct
Cool
If anyone wants the working code, here it is: https://gist.github.com/Rocky43007/f0c8f75a37c846ffcd21ab9ad4bb8e8b
WARNING: You do have to patch any functions used in the Interaction object. For example,
.reply()
needs to be patched in the interaction object from the client side or your command will error out why it tries to run <CommandInteraction>.reply()
Gist
Discord Bot Automated Command Tester
Discord Bot Automated Command Tester. GitHub Gist: instantly share code, notes, and snippets.