Skip to main content

Getting started with Sapphire

To install Sapphire, you need to install both discord.js and @sapphire/framework. You can do this by running the following command:

npm install @sapphire/framework discord.js
caution

You need at least Node.js version 16.6.0 to use Sapphire.

Creating an index.js file

While it doesn't have to be called index.js, this file will be the main file for your bot (otherwise known as the entry point) as it'll handle the bot's setup and login. It's recommended to put this inside a subdirectory of your project, typically src, both for organization and for Creating Commands.

caution

Make sure that the main property in your package.json points to the right path, such as src/index.js.

To begin, import @sapphire/framework and instantiate SapphireClient. This is where you can customize Sapphire's and discord.js' behaviour. Sapphire's client extends discord.js', so everything from Client is available in SapphireClient!

import { SapphireClient } from '@sapphire/framework';

const client = new SapphireClient({ intents: ['GUILDS', 'GUILD_MESSAGES'] });

client.login('your-token-goes-here');

Sapphire detects mention prefixes (@bot command), but you can optionally define any number of default prefixes with the defaultPrefix option and regexPrefix for more complex cases.

There is also the advanced option baseUserDirectory, which allows you to define the base directory Sapphire will scan. By default, Sapphire registers this as the directory containing your entry point and locates stores within it. As such, if the root directory is src, it will register src/commands as one of the command directories.

danger

You should use environment variables or a config.json for your token instead of passing it directly! You can read more about why you should here.

And that's it for your index.js file! In the end, your directory should look like this, along with whatever .gitignore or config.json files you may have:

โ”œโ”€โ”€ node_modules
โ”œโ”€โ”€ package.json
โ””โ”€โ”€ src
โ””โ”€โ”€ index.js