Listener run() signature

Feel like I'm missing something obvious.. how do I find out the correct signature for a run method of any given listener - they're different for each event. I'm sure there's an obvious way to get this, without trial and error and debugging to see what's available to it - where am I not looking?
import { Events, Listener, ListenerOptions } from "@sapphire/framework";

import { logger } from "@repo/logger";

export class GuildCreateListener extends Listener {
public constructor(context: Listener.LoaderContext, options?: ListenerOptions) {
super(context, {
...options,
event: Events.GuildCreate,
once: true,
});
}

public run(): void { //??
const guildName = "???"; //??
logger.info(`The bot has been added to a new server! ${guildName}`, "Bot Added");
}
}
import { Events, Listener, ListenerOptions } from "@sapphire/framework";

import { logger } from "@repo/logger";

export class GuildCreateListener extends Listener {
public constructor(context: Listener.LoaderContext, options?: ListenerOptions) {
super(context, {
...options,
event: Events.GuildCreate,
once: true,
});
}

public run(): void { //??
const guildName = "???"; //??
logger.info(`The bot has been added to a new server! ${guildName}`, "Bot Added");
}
}
Solution:
The only thing you can do is extends Listener<typeof Events.GuildCreate> but that won't implicitly type the run parameters, it'll just error if you provide the wrong types. Sadly TS offers nothing better.
Jump to solution
7 Replies
Bejasc
BejascOP11mo ago
For this example, I can of course refer to the discord docs - but I would have expected this information should be available somehow in the framework itself, or aided in some way by TS - am I missing something?
Discord.js
Discord.js is a powerful node.js module that allows you to interact with the Discord API very easily. It takes a much more object-oriented approach than most other JS Discord libraries, making your bot's code significantly tidier and easier to comprehend.
Solution
Favna
Favna11mo ago
The only thing you can do is extends Listener<typeof Events.GuildCreate> but that won't implicitly type the run parameters, it'll just error if you provide the wrong types. Sadly TS offers nothing better.
Bejasc
BejascOP11mo ago
could they be annotated in the Events const? would maybe provide the benefit of event: Events.GuildCreate providing a intelisense hint of Accepts a single Guild parameter etc Though I imagine this may change over time and fall out of date.
Bejasc
BejascOP11mo ago
/** Accepts a single `Guild` parameter */
readonly GuildCreate: Events$1.GuildCreate;
/** Accepts a single `Guild` parameter */
readonly GuildCreate: Events$1.GuildCreate;
No description
Favna
Favna11mo ago
Yeah.... It's the outdated part that's kind of annoying. I suppose it's still something though. Maybe we can make a script that auto updates them
Bejasc
BejascOP11mo ago
Maybe there's some way to infer the link to the discord doc based on the event name? And it would become a Refer to <link> for usage information etc script would be ideal approach. I'm unsure if this is a common problem/question though I'll mark as solved, thanks for the context 👍
Favna
Favna11mo ago
Tsdoc cannot use dynamic content. A script would still have to inject it into the TS code. Feasible though with some trickery.
Want results from more Discord servers?
Add your server