Creating your first command
In Seyfert, commands are the main structure of any discord bot project, although Discord support for them is quite poor, utilities have been developed for creating them.
Let’s start by declaring the first command of the application
1import { Declare, Command, type CommandContext } from 'seyfert';2
3@Declare({4 name: 'ping',5 description: 'Show the ping with discord'6})7export default class PingCommand extends Command {8
9 async run(ctx: CommandContext) {10 // average latency between shards11 const ping = ctx.client.gateway.latency;12
13 await ctx.write({14 content: `The ping is \`${ping}\``15 });16 }17}
To test if your command works correctly, you must first publish it on Discord after making minor changes to your main file:
1import { Client } from 'seyfert';2
3const client = new Client();4
5client.start().then(() => client.uploadCommands());
Pretty simple, isn’t it? But sometimes commands are not just about receiving a request and responding to it, you also have to think about what the user wants, that’s where the options
come in, let’s make the response of this command something private.
Using options
1import {2 Command,3 Declare,4 Options,5 createBooleanOption,6 type CommandContext7} from 'seyfert';8import { MessageFlags } from 'seyfert/lib/types';9
10const options = {11 hide: createBooleanOption({12 description: "Hide command output",13 }),14}15
16@Declare({17 name: 'ping',18 description: 'Show the ping with discord'19})20@Options(options)21export default class PingCommand extends Command {22
23 async run(ctx: CommandContext<typeof options>) {24 const flags = ctx.options.hide ? MessageFlags.Ephemeral : undefined;25
26 // average latency between shards27 const ping = ctx.client.gateway.latency;28
29 await ctx.write({30 content: `The ping is \`${ping}\``,31 flags,32 });33 }34
35}
Directorysrc
Directorycommands
- ping.ts
- index.ts
- package.json
- seyfert.config.js
- tsconfig.json