Yup
Yup
SIASapphire - Imagine a framework
Created by Yup on 2/13/2024 in #sapphire-support
Any way to register a database client to the application command
Let's take this example:
import { Command } from "@sapphire/framework";

export default class extends Command {
public constructor(context: Command.LoaderContext, options: Command.Options) {
super(context, { ...options });
}

public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand((builder) =>
builder.setName("ping").setDescription("Ping bot to see if it is alive")
);
}
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
await interaction.reply("Yup");
}
}
import { Command } from "@sapphire/framework";

export default class extends Command {
public constructor(context: Command.LoaderContext, options: Command.Options) {
super(context, { ...options });
}

public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand((builder) =>
builder.setName("ping").setDescription("Ping bot to see if it is alive")
);
}
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
await interaction.reply("Yup");
}
}
It registers nice. Now let's say we register out DB client:
const db = new DB(); //For testing
const db = new DB(); //For testing
Now since I don't wanna create new instances everytime a command is invoked. I need a way to register this. (With type safety). My question: How can I register the db client onto the context of the command? Singleton class? Well, if there are no other possibilites. I'd rather just instantiate it in main and keep references to it throughout the commands where I can invoke.
4 replies