Skip to main content

Class: SapphireClient<Ready>

Defined in: projects/framework/src/lib/SapphireClient.ts:220

The base Client extension that makes Sapphire work. When building a Discord bot with the framework, the developer must either use this class, or extend it.

Sapphire also automatically detects the folders to scan for pieces, please read StoreRegistry.registerPath for reference. This method is called at the start of the SapphireClient.login method.

See

SapphireClientOptions for all options available to the Sapphire Client. You can also provide all of discord.js' ClientOptions

Since

1.0.0

Examples

const client = new SapphireClient({
presence: {
activity: {
name: 'for commands!',
type: 'LISTENING'
}
}
});

client.login(process.env.DISCORD_TOKEN)
.catch(console.error);
// Automatically scan from a specific directory, e.g. the main
// file is at `/home/me/bot/index.js` and all your pieces are at
// `/home/me/bot/pieces` (e.g. `/home/me/bot/pieces/commands/MyCommand.js`):
const client = new SapphireClient({
baseUserDirectory: join(__dirname, 'pieces'),
// More options...
});
// Opt-out automatic scanning:
const client = new SapphireClient({
baseUserDirectory: null,
// More options...
});

Extends

Type Parameters

Type ParameterDefault type
Ready extends booleanboolean

Constructors

new SapphireClient()

new SapphireClient<Ready>(options: ClientOptions): SapphireClient<Ready>

Defined in: projects/framework/src/lib/SapphireClient.ts:280

Parameters

ParameterType
optionsClientOptions

Returns

SapphireClient<Ready>

Overrides

Client<Ready>.constructor

Properties

disableMentionPrefix?

optional disableMentionPrefix: boolean

Defined in: projects/framework/src/lib/SapphireClient.ts:272

Whether the bot has mention as a prefix disabled

Default

false

Example

client.disableMentionPrefix = false;

fetchPrefix

fetchPrefix: SapphirePrefixHook

Defined in: projects/framework/src/lib/SapphireClient.ts:255

The method to be overridden by the developer.

Since

1.0.0

Returns

A string for a single prefix, an array of strings for matching multiple, or null for no match (mention prefix only).

Examples

// Return always the same prefix (unconfigurable):
client.fetchPrefix = () => '!';
// Retrieving the prefix from a SQL database:
client.fetchPrefix = async (message) => {
// note: driver is something generic and depends on how you connect to your database
const guild = await driver.getOne('SELECT prefix FROM public.guild WHERE id = $1', [message.guild.id]);
return guild?.prefix ?? '!';
};
// Retrieving the prefix from an ORM:
client.fetchPrefix = async (message) => {
// note: driver is something generic and depends on how you connect to your database
const guild = await driver.getRepository(GuildEntity).findOne({ id: message.guild.id });
return guild?.prefix ?? '!';
};

Overrides

Client.fetchPrefix


id

id: null | string = null

Defined in: projects/framework/src/lib/SapphireClient.ts:225

The client's ID, used for the user prefix.

Since

1.0.0

Overrides

Client.id


logger

logger: ILogger

Defined in: projects/framework/src/lib/SapphireClient.ts:262

The logger to be used by the framework and plugins. By default, a Logger instance is used, which emits the messages to the console.

Since

1.0.0

Overrides

Client.logger


stores

stores: StoreRegistry

Defined in: projects/framework/src/lib/SapphireClient.ts:278

The registered stores.

Since

1.0.0

Overrides

Client.stores


plugins

static plugins: PluginManager

Defined in: projects/framework/src/lib/SapphireClient.ts:364

Methods

login()

login(token?: string): Promise<string>

Defined in: projects/framework/src/lib/SapphireClient.ts:339

Loads all pieces, then logs the client in, establishing a websocket connection to Discord.

Parameters

ParameterTypeDescription
token?stringToken of the account to log in with.

Returns

Promise<string>

Token of the account used.

Since

1.0.0

Overrides

Client.login


use()

static use(plugin: typeof Plugin): typeof SapphireClient

Defined in: projects/framework/src/lib/SapphireClient.ts:366

Parameters

ParameterType
plugintypeof Plugin

Returns

typeof SapphireClient