🚀 First Start
Hello there! 👋 This step-by-step guide is here to help you make and run a simple nyx bot.
⏬ Installation
First, let's start by installing nyx:
- npm
- Yarn
- pnpm
npm install @nyx-discord/framework @nyx-discord/core
yarn add @nyx-discord/framework @nyx-discord/core
pnpm add @nyx-discord/framework @nyx-discord/core
👷 Bot Creation
The first coding step is to create a Discord.js client. This can be any client (as long as it's not logged in or destroyed).
import {
Client,
GatewayIntentBits
} from 'discord.js';
const client = new Client({
intents: [GatewayIntentBits.Guilds],
});
Next up, we'll create a new instance of Bot
, which is @framework
's implementation of NyxBot
.
You can do that with Bot#create()
, which takes a function that gets passed the currently constructing bot
instance, and should return the options that you want for the bot.
This is where you'll provide your bot's token, client instance, ID, logger, and other optional settings, like managers.
You can also create multiple bots and they'll all work independently of each other.
import { Bot } from '@nyx-discord/framework';
const myBot = Bot.create((bot) => ({
token: 'TOKEN',
client,
id: Symbol('My bot'),
logger: console, // Can be your own logger
deployCommands: true, // Whether to deploy commands to Discord on start
// For example, if you had a custom command manager:
// commands: new MyCustomCommandManager(bot),
}));
Details
About new Bot()
You can also instantiate Bot
with the new
operator, but in order to allow dependency
injection, this constructor requires every manager on its options, including default ones.You would need to instantiate and pass every manager to it, like so:
const myBot = new Bot((bot) => ({
token: 'TOKEN',
client,
id: Symbol('My bot'),
logger: console,
deployCommands: true,
events: DefaultEventManager.create(bot, client),
plugins: DefaultPluginManager.create(bot),
// and so on
}));
You will find this pattern in a lot of other @framework
objects, where you can instantiate either with
new SomeComponent(depA, depB, ...)
specifying every dependency, or with SomeComponent.create()
"without" default dependencies.
When creating a bot (either via new
or #create()
), the type of parameters that you return are used in the bot's
type signature.
This implies that, for example, if you create a MyCustomCommandManager
and pass it to the bot's options, the bot will
know that its command manager is MyCustomCommandManager
and not just the generic CommandManager
, allowing you to use
custom properties or methods that you may have added or modified.
🚀 Bot Starting
To start a bot, you can set it up and then login, or just directly start it:
await bot.start(); // setups and logins
// or:
await bot.setup();
await bot.login();
Trying to login a bot without first setting it up will throw an IllegalStateError
.
As a quick explanation:
- When a bot is first created, it starts in the
Unsetup
status. It's NOT recommended to interact with the bot on this state. - Upon calling
Bot#setup()
, all the managers and plugins are notified about the setup. This is done to safely perform possible asynchronous logic that these objects need to actually function, outside the "object construction" step. The bot then switches toWaiting
. - It's now safe to log in the bot on Discord with
Bot#login()
. Managers and plugins are also notified after the login, but at this point they should already be ready for it.
📚 Summary
The final code would look as follows:
import { Bot } from '@nyx-discord/framework';
import {
Client,
GatewayIntentBits
} from 'discord.js';
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const bot = Bot.create(() => ({
token: 'TOKEN',
client,
id: Symbol('Bot'),
logger: console,
deployCommands: true,
}));
// Make sure you're on an async context to be able to await
await bot.start();
And that's it! You now have a running Discord bot with nyx.
🔜 Next...
Check the rest of nyx features like command adding, event subscribing, schedule creation and more:
- 💻 Commands - Create Discord application commands.
- 📸 Events - Subscribe to events, either from Discord.js or your own.
- ️⏰ Schedules - Program timed tasks.
- 👤 Sessions - Create user interaction sessions.
- ️🧩 Plugins - Extend nyx.