Your first bot

In this guide you'll build a working bot from scratch in a few minutes, using a simple text command. By the end you'll have a base to extend with slash commands, events, and components.

1. Create the client

ERXClient is the heart of your bot. At minimum it needs a token and intents:

JavaScript
require("dotenv").config();
const { ERXClient, Intents } = require("syntx.js");

const client = new ERXClient({
  token: process.env.TOKEN,
  intents: Intents.Fast,
  prefix: "!",
});

Note

prefix is only required if you're going to register text commands with client.command(). If you only plan to use slash commands, you can skip it; you'll need to pass clientId instead. See SlashCommand.

2. React when the bot connects

JavaScript
client.ready(() => {
  console.log(`Logged in as ${client.bot.user.tag}`);
});

client.bot is the real discord.js Client underneath, so anything you can normally do with discord.js, you can do through it.

3. Add your first command

Define a command with client.command(). content receives the discord.js Message that triggered it:

JavaScript
client.command({
  name: "greet",
  content: (message) => {
    message.reply(`Hello, ${message.author.username}!`);
  },
});

command() only stores the definition; call client.registerCommands() once, after every client.command() call, so syntx.js actually starts listening for messages:

JavaScript
client.registerCommands();

Warning

Without client.registerCommands(), your bot will never respond to !greet; it just won't be listening yet.

4. Start the bot

JavaScript
client.start();

Full code

JavaScript
require("dotenv").config();
const { ERXClient, Intents } = require("syntx.js");

const client = new ERXClient({
  token: process.env.TOKEN,
  intents: Intents.Fast,
  prefix: "!",
});

client.ready(() => {
  console.log(`Logged in as ${client.bot.user.tag}`);
});

client.command({
  name: "greet",
  content: (message) => message.reply(`Hello, ${message.author.username}!`),
});

client.registerCommands();
client.start();

Run it with node src/index.js (or wherever your entry file lives), then type !greet in a channel your bot can see.

Next steps

  • Add a / slash command with SlashCommand.
  • Listen to any discord.js event with client.event().
  • Once you have more than a couple of commands, split them into files and let client.handler() load them for you automatically.
  • See the full Client reference for everything else ERXClient can do.