Skip to content

Creating your first command

Updating seyfert config

Before starting this chapter we shall update seyfert.config.js in order to tell seyfert where our commands file will be.

seyfert.config.js
// @ts-check
const { config } = require('seyfert');
module.exports = config.bot({
token: process.env.BOT_TOKEN ?? "",
intents: ["Guilds"],
locations: {
base: "src",
output: "dist",
commands: "commands" // - src/commands will be our commands directory
}
});

Declaring commands

Each command file needs to export by default the command class so seyfert is able to load it.

For this example we’ll setup ping command, let’s start by declaring the ping command class as shown below.

src/commands/ping.ts
import { Declare, Command, type CommandContext } from 'seyfert';
@Declare({
name: 'ping',
description: 'Show the ping with discord'
})
export default class PingCommand extends Command {
async run(ctx: CommandContext) {
// average latency between shards
const ping = ctx.client.gateway.latency;
await ctx.write({
content: `The ping is \`${ping}\``
});
}
}

To test whether your command works, you must publish it on Discord before making minor changes to your main file:

src/index.ts
import { Client, ParseClient } from "seyfert";
const client = new Client();
client.start().then(() => client.uploadCommands());
// here goes the declare module made in the previous step

Pretty simple, isn’t it? But sometimes commands are not just about receiving a request and responding it, you also have to think about what the user wants, that’s where the options come in, let’s add an option to hide the response.

Using options

src/commands/ping.ts
import {
Command,
Declare,
Options,
createBooleanOption,
type CommandContext
} from 'seyfert';
import { MessageFlags } from 'seyfert/lib/types';
const options = {
hide: createBooleanOption({
description: "Hide command output",
}),
}
@Declare({
name: 'ping',
description: 'Show the ping with discord'
})
@Options(options)
export default class PingCommand extends Command {
async run(ctx: CommandContext<typeof options>) {
const flags = ctx.options.hide ? MessageFlags.Ephemeral : undefined;
// average latency between shards
// in http change this for the example above
const ping = ctx.client.gateway.latency;
await ctx.write({
content: `The ping is \`${ping}\``,
flags,
});
}
}

Below is how the file tree should look like after adding the command.

  • Directorysrc
    • Directorycommands
      • ping.ts
    • index.ts
  • package.json
  • seyfert.config.js
  • .env
  • tsconfig.json