Plugin
Base class for a seedcord plugin. Extend it and implement init(). this.core is the running
bot.
Take the host as your constructor's first parameter and pass it to super(). Add your own parameters after
it. attach() types them at the call site.
A plugin that narrows transport or runtime in Opts passes the same values to super().
attach() throws when either one differs from the bot's.
abstract class Plugin<
Opts extends PluginOptions = {},
TCore extends CoreBase = CoreBase,
Fixed extends ScopeAxis = never
>
implements Initializeable, HmrAware, PluginSlotsclass Analytics extends Plugin<{ transport: 'gateway' }> {
constructor(
host: CoreBase,
private readonly apiKey: string
) {
super(host, { transport: 'gateway', init: { phase: StartupPhase.Login } });
}
public async init(): Promise<void> {
await this.connect(this.apiKey);
}
}
seedcord.attach('analytics', Analytics, apiKey);Member visibility
Filter class members by access level.
Plugin(
host: CoreBase,
[spec],
input: PluginSpecArgs<Opts, Fixed>
)Showing members with Protected visibility and higher.
Opts
Where this plugin may attach.
Opts extends PluginOptions = {}TCore
The Core each transport binds this.core to.
Fixed
The transport and runtime values a transport's own Plugin base sets. Leave it out.
protected core: TCoreAdds values that every handler can read. Each key becomes a member, like this.tag, and its
function returns the value. Gates and Notice.render read the same values on ctx.
Seedcord calls each function once per interaction, and once per event that has a handler.
attach() throws if seedcord already uses one of these keys, like reply, if another plugin on the
bot uses it, or if the bot attaches this plugin twice.
public readonly dispatchMembers?: DispatchMemberFunctionspublic override readonly dispatchMembers = {
tag: (origin: DispatchOrigin) => `${origin.guildId}:${origin.userId}`
};protected kv: Kv// works in a field initializer, the constructor, and every method
private readonly jobs = this.kv.collection<Job>('job');Throws:
A SeedcordTypeError when something built the plugin with
newoutsideattach().
protected readonly logger: LoggerReturns data that seedcord codegen writes into the bot's seedcord-gen.ts as a type.
Read it back with Generated<typeof YourPlugin>. Return plain data, or classes and objects
that a project file or your plugin's package exports. The class needs a static readonly id.
seedcord codegen calls this without running init(). Read only what the constructor stored
and the bot's files.
public dispose?(): Promisable<void>Answers a web request sent to the bot. seedcord calls this on each plugin in attach order and
answers with the first Response. On an http bot, seedcord calls it for interactions too.
Return undefined to pass the request on. If this method throws, seedcord logs the error on
this plugin's logger and answers the request with a 500.
On an http bot, check the path before you await anything because every interaction waits for
this method. On a server, requests reach it once the Ready phase begins. It can run before
ready() finishes, and before an init() that you moved to Ready.
public fetch?(
request: Request
): Promisable<Response | undefined>Throws:
seedcord throws a SeedcordError when this method reads or locks the body without answering the request.
public fetch(request: Request): Response | undefined {
if (new URL(request.url).pathname !== '/status') return undefined;
return Response.json({ ok: true });
}Runs in StartupPhase.Configuration by default.
Move it to a different phase with the PluginLifecycleSpec.
public abstract init(): Promisable<void>public onHmr(_event: HmrUpdateEvent): Promisable<void>Changes commands before seedcord sends them to Discord. Write your changes onto these objects,
like setting name_localizations. Each command appears once, even when it deploys to several guilds.
If it throws, the deploy stops, and so do seedcord codegen, a Workers build, seedcord commands deploy,
and seedcord commands list.
The CLI calls this without running init(). Read only what the constructor stored and the bot's files.
public prepareCommands?(
commands: readonly RESTPostAPIApplicationCommandsJSONBody[]
): Promisable<void>public ready?(): Promisable<void>