broadcastEval() and Typescript

A bit at a loss here, trying to use .broadcastEval to run a function on all shards, for this function I would like to add two parameters as context, one of which is a guild object, but I'm getting these Typescript errors and I just can't seem to find how one is supposed to work with the .broadcastEval method and typescript. Pictures below show the problem. Removing the types from the function leaves all the code inside with the values as implicitly :any which then causes a bunch of new errors. Do I really need to leave these as "any" and then check inside the function if they are the required type to make typescript be happy, or is there a correct way to do this all?
No description
No description
14 Replies
d.js toolkit
d.js toolkit10mo ago
- 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!
TayDex
TayDexOP10mo ago
Error here:
await client.shard?.broadcastEval(reportServerAmount, { context: {guild: guild, add: add}})
await client.shard?.broadcastEval(reportServerAmount, { context: {guild: guild, add: add}})
Removing types here will cause all the code inside to complain about implicit any type for guild.
async function reportServerAmount(c: Client, {guild, add}: {guild: Guild, add: boolean}) {
async function reportServerAmount(c: Client, {guild, add}: {guild: Guild, add: boolean}) {
monbrey
monbrey10mo ago
It seems the guild argument is of unknown type here And your typecast is erroring
TayDex
TayDexOP10mo ago
client.on("guildCreate", async (guild) => {
await client.shard?.broadcastEval(reportServerAmount, { context: {guild: guild, add: true}})
})
client.on("guildCreate", async (guild) => {
await client.shard?.broadcastEval(reportServerAmount, { context: {guild: guild, add: true}})
})
Comes straight from the guildCreate event
monbrey
monbrey10mo ago
:Thonk:
TayDex
TayDexOP10mo ago
Well it's the result of multiple tries, can't find a way to get broadcastEval to be happy unless I let guild be type unkown
monbrey
monbrey10mo ago
Can you give me the definition of reportServerAmount so I can look into this o nvm you did
TayDex
TayDexOP10mo ago
If I change it to:
async function reportServerAmount(c: Client, {guild, add}) {
async function reportServerAmount(c: Client, {guild, add}) {
The error immediately disappears from broadcastEval, but then I get a Binding element 'guild' implicitly has an 'any' type.ts(7031) on the function definition So its either Typescript complains about the function or about the use of it in broadcastEval
monbrey
monbrey10mo ago
Yeah I have no idea sorry, someone better with TS will need to help
TayDex
TayDexOP10mo ago
Yeah, thanks for the attempt, my TS also seems to be insufficient, just can't believe I'm the first to get this, couldn't find anything though.
monbrey
monbrey10mo ago
Oh, I think I know why. One sec @TayDex yeah, got it. You can't send classes between processes - anything you send has to be JSON serialisable Which technically Guilds are, but the type for Guild#toJSON() => unknown So what is it you need the whole Guild object for, or can you just send its ID as a string? No type error if I do this:
async function reportServerAmount(c: Client, { guild: string, add: boolean }) { }

client.on("guildCreate", async (guild) => {
await client.shard?.broadcastEval(reportServerAmount, { context: { guild: guild.id, add: true } })
})
async function reportServerAmount(c: Client, { guild: string, add: boolean }) { }

client.on("guildCreate", async (guild) => {
await client.shard?.broadcastEval(reportServerAmount, { context: { guild: guild.id, add: true } })
})
TayDex
TayDexOP10mo ago
Oh its all starting to make sense now, well its a few things like name, id, member count and maybe a few more, so I didnt want to pass each as its own context/parameter Okay so as like a summary for anyone who finds this in the future and as a response to you: That is indeed the problem, although the code you have sent still give me an error and I have to type it differently:
//Typescript complains
async function reportServerAmount(c: Client, { guild: string, add: boolean }) { }
//Unless I write it like this:
async function reportServerAmount(c: Client, { guild, add }: {guild: string, add: boolean}) { }
//Typescript complains
async function reportServerAmount(c: Client, { guild: string, add: boolean }) { }
//Unless I write it like this:
async function reportServerAmount(c: Client, { guild, add }: {guild: string, add: boolean}) { }
As I want multiple things of the guild object, I'm now using this approach: :galaxybrain:
client.on("guildCreate", async (guild) => {
await client.shard?.broadcastEval(reportServerAmount, { context: {guild:{name: guild.name, memberCount: guild.memberCount}, add: true}})
})

async function reportServerAmount(c: Client, {guild, add}: {guild: {name: string, memberCount: number}, add: boolean}) {
client.on("guildCreate", async (guild) => {
await client.shard?.broadcastEval(reportServerAmount, { context: {guild:{name: guild.name, memberCount: guild.memberCount}, add: true}})
})

async function reportServerAmount(c: Client, {guild, add}: {guild: {name: string, memberCount: number}, add: boolean}) {
So I guess I have to pass each element individually, maybe someone knows an easier way. Take what I say with a grain of salt, had a break from coding for a few months and just started with sharding, but the above seems to accomplish what I need without erroring. Anyways thank you a lot for the help ended a 2:30h search of pure confusion. Although i do wonder if the guild class can technically be Json serialized wouldnt the best fix be discord.js adding a proper type instead of unknown?
monbrey
monbrey10mo ago
I suspect there's a reason why it's unknown but honestly I don't know what it is I think it just inherits it from Base#toJSON And we havent made the effort to manually type every toJSON method The typescript rewrite should address it natively
GitHub
GitHub10mo ago
:issue_open: #9432 in discordjs/discord.js by papsavas opened <t:1682014152:R> Type currently unknown toJSON() methods
Want results from more Discord servers?
Add your server