Accessing the client in pieces
There are multiple ways to access the client in pieces:
- By accessing
container
fromthis
, if you're extending a Sapphire Piece (commands, listeners, etc):
- CommonJS
- ESM
- TypeScript
const { Listener } = require('@sapphire/framework');
class ExampleListener extends Listener {
run() {
const { client } = this.container;
// rest of your code
}
}
module.exports = {
ExampleListener
};
import { Listener } from '@sapphire/framework';
export class ExampleListener extends Listener {
run() {
const { client } = this.container;
// rest of your code
}
}
import { Listener } from '@sapphire/framework';
export class ExampleListener extends Listener {
public run() {
const { client } = this.container;
// rest of your code
}
}
- By importing
container
from the framework:
- CommonJS
- ESM
- TypeScript
const { container, Listener } = require('@sapphire/framework');
class ExampleListener extends Listener {
run() {
const { client } = container;
// rest of your code
}
}
module.exports = {
ExampleListener
};
import { container, Listener } from '@sapphire/framework';
export class ExampleListener extends Listener {
run() {
const { client } = container;
// rest of your code
}
}
import { container, Listener } from '@sapphire/framework';
export class ExampleListener extends Listener {
public run() {
const { client } = container;
// rest of your code
}
}