Class: SapphireClient<Ready>
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
Example
const client = new SapphireClient({
presence: {
activity: {
name: 'for commands!',
type: 'LISTENING'
}
}
});
client.login(process.env.DISCORD_TOKEN)
.catch(console.error);
Example
// 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...
});
Example
// Opt-out automatic scanning:
const client = new SapphireClient({
baseUserDirectory: null,
// More options...
});
Type parameters
Name | Type |
---|---|
Ready | extends boolean = boolean |
Hierarchy
-
Client
<Ready
>↳
SapphireClient
Constructors
constructor
• new SapphireClient<Ready
>(options
): SapphireClient
<Ready
>
Type parameters
Name | Type |
---|---|
Ready | extends boolean = boolean |
Parameters
Name | Type |
---|---|
options | ClientOptions |
Returns
SapphireClient
<Ready
>
Overrides
Client<Ready>.constructor
Defined in
projects/framework/src/lib/SapphireClient.ts:279
Properties
application
• application: If
<Ready
, ClientApplication
, null
>
Inherited from
Client.application
Defined in
node_modules/discord.js/typings/index.d.ts:971
channels
• channels: ChannelManager
Inherited from
Client.channels
Defined in
node_modules/discord.js/typings/index.d.ts:972
disableMentionPrefix
• Optional
disableMentionPrefix: boolean
Whether the bot has mention as a prefix disabled
Default
false
Example
client.disableMentionPrefix = false;
Defined in
projects/framework/src/lib/SapphireClient.ts:271
fetchPrefix
• fetchPrefix: SapphirePrefixHook
The method to be overridden by the developer.
Since
1.0.0
Example
// Return always the same prefix (unconfigurable):
client.fetchPrefix = () => '!';
Example
// 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 ?? '!';
};
Example
// 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
Defined in
projects/framework/src/lib/SapphireClient.ts:254
guilds
• guilds: GuildManager
Inherited from
Client.guilds
Defined in
node_modules/discord.js/typings/index.d.ts:974
id
• id: null
| string
= null
The client's ID, used for the user prefix.
Since
1.0.0
Overrides
Client.id
Defined in
projects/framework/src/lib/SapphireClient.ts:224
logger
• logger: ILogger
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
Defined in
projects/framework/src/lib/SapphireClient.ts:261
options
• options: Omit
<ClientOptions
, "intents"
> & { intents
: IntentsBitField
}
Inherited from
Client.options
Defined in
node_modules/discord.js/typings/index.d.ts:975
readyTimestamp
• readyTimestamp: If
<Ready
, number
, null
>
Inherited from
Client.readyTimestamp
Defined in
node_modules/discord.js/typings/index.d.ts:977
rest
• rest: REST
Inherited from
Client.rest
Defined in
node_modules/discord.js/typings/index.d.ts:521
shard
• shard: null
| ShardClientUtil
Inherited from
Client.shard
Defined in
node_modules/discord.js/typings/index.d.ts:979
stores
• stores: StoreRegistry
The registered stores.
Since
1.0.0
Overrides
Client.stores
Defined in
projects/framework/src/lib/SapphireClient.ts:277
sweepers
• sweepers: Sweepers
Inherited from
Client.sweepers
Defined in
node_modules/discord.js/typings/index.d.ts:978
token
• token: If
<Ready
, string
, null
| string
>
Inherited from
Client.token
Defined in
node_modules/discord.js/typings/index.d.ts:980
user
• user: If
<Ready
, ClientUser
, null
>
Inherited from
Client.user
Defined in
node_modules/discord.js/typings/index.d.ts:982
users
• users: UserManager
Inherited from
Client.users
Defined in
node_modules/discord.js/typings/index.d.ts:983
voice
• voice: ClientVoiceManager
Inherited from
Client.voice
Defined in
node_modules/discord.js/typings/index.d.ts:984
ws
• ws: WebSocketManager
Inherited from
Client.ws
Defined in
node_modules/discord.js/typings/index.d.ts:985
captureRejectionSymbol
▪ Static
Readonly
captureRejectionSymbol: typeof captureRejectionSymbol
Value: Symbol.for('nodejs.rejection')
See how to write a custom rejection handler
.
Since
v13.4.0, v12.16.0
Inherited from
Client.captureRejectionSymbol
Defined in
node_modules/@types/node/events.d.ts:402
captureRejections
▪ Static
captureRejections: boolean
Value: boolean
Change the default captureRejections
option on all new EventEmitter
objects.
Since
v13.4.0, v12.16.0
Inherited from
Client.captureRejections
Defined in
node_modules/@types/node/events.d.ts:409
defaultMaxListeners
▪ Static
defaultMaxListeners: number
By default, a maximum of 10
listeners can be registered for any single
event. This limit can be changed for individual EventEmitter
instances
using the emitter.setMaxListeners(n)
method. To change the default
for allEventEmitter
instances, the events.defaultMaxListeners
property can be used. If this value is not a positive number, a RangeError
is thrown.
Take caution when setting the events.defaultMaxListeners
because the
change affects allEventEmitter
instances, including those created before
the change is made. However, calling emitter.setMaxListeners(n)
still has
precedence over events.defaultMaxListeners
.
This is not a hard limit. The EventEmitter
instance will allow
more listeners to be added but will output a trace warning to stderr indicating
that a "possible EventEmitter memory leak" has been detected. For any singleEventEmitter
, the emitter.getMaxListeners()
and emitter.setMaxListeners()
methods can be used to
temporarily avoid this warning:
import { EventEmitter } from 'node:events';
const emitter = new EventEmitter();
emitter.setMaxListeners(emitter.getMaxListeners() + 1);
emitter.once('event', () => {
// do stuff
emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
});
The --trace-warnings
command-line flag can be used to display the
stack trace for such warnings.
The emitted warning can be inspected with process.on('warning')
and will
have the additional emitter
, type
, and count
properties, referring to
the event emitter instance, the event's name and the number of attached
listeners, respectively.
Its name
property is set to 'MaxListenersExceededWarning'
.
Since
v0.11.2
Inherited from
Client.defaultMaxListeners
Defined in
node_modules/@types/node/events.d.ts:446
errorMonitor
▪ Static
Readonly
errorMonitor: typeof errorMonitor
This symbol shall be used to install a listener for only monitoring 'error'
events. Listeners installed using this symbol are called before the regular'error'
listeners are called.
Installing a listener using this symbol does not change the behavior once an'error'
event is emitted. Therefore, the process will still crash if no
regular 'error'
listener is installed.
Since
v13.6.0, v12.17.0
Inherited from
Client.errorMonitor
Defined in
node_modules/@types/node/events.d.ts:395
plugins
▪ Static
plugins: PluginManager
Defined in
projects/framework/src/lib/SapphireClient.ts:363
Accessors
_censoredToken
• get
_censoredToken(): null
| string