Skip to main content

Variable: container

const container: Container

The injected variables that will be accessible to any place. To add an extra property, simply add a property with a regular assignment, and it will be available in all places simultaneously.

Examples

// Add a reference for the version:
import { container } from '@sapphire/pieces';

container.version = '1.0.0';

// Can be placed anywhere in a TypeScript file, for JavaScript projects,
// you can create an `augments.d.ts` and place the code there.
declare module '@sapphire/pieces' {
interface Container {
version: string;
}
}

// In any piece, core, plugin, or custom:
export class UserCommand extends Command {
public messageRun(message, args) {
// The injected version is available here:
const { version } = this.container;

// ...
}
}
// In a plugin's context, e.g. API:
class Api extends Plugin {
static [postInitialization]() {
const server = new Server(this);
container.server = server;

// ...
}
}

declare module '@sapphire/pieces' {
interface Container {
server: Server;
}
}

// In any piece, even those that aren't routes nor middlewares:
export class UserRoute extends Route {
public [methods.POST](message, args) {
// The injected server is available here:
const { server } = this.container;

// ...
}
}

Source

projects/pieces/src/lib/shared/Container.ts:78