Skip to content

Prefix Commands

Setting up prefixes

Not only does seyfert handle application commands but also prefix commands. They are not enabled by default but you can enable them by setting up an option inside the ClientOptions. We have to access to the command option where we can specify a suboption called prefix. This option expects a callback returning an array of possible prefixes for that command. As the callback returns a Message is pretty simple to handle diffrent prefixes per guild.

Here is an example in which we are going to set multiple prefixes for all the commands.

1
import { Client } from 'seyfert';
2
3
const client = new Client({
4
commands: {
5
prefix: (msg) => {
6
//here you can handle whatever prefixes you will want to be consider depending on the message data.
7
return ['!', '?', '.']
8
}
9
}
10
});

Handling prefix commands

Having set up our prefixes for the commands it’s about time we want to make them run.

And for your luck, the handling works as application commands. The only difference is that we receive an optional Message property inside CommandContext.

To make this property appear in our CommandContext the only thing we shall do is declaring seyfert module and set the withPrefix property inside InternalOptions into true. You can see this detailed example.

In this example we are going to create a command which cross post the message that was sent in an announcement channel.

1
import {
2
Command,
3
CommandContext,
4
Declare
5
} from 'seyfert';
6
7
@Declare({
8
name: 'crosspost',
9
description: 'Cross post and announcement message.'
10
});
11
export default class CrosspostCommand extends Command {
12
async run(ctx: CommandContext){
13
if(ctx.message) await ctx.message.crosspost();
14
return ctx.write({ content: 'I have cross posted your announcement.'});
15
}
16
};

Replying to prefix commands

By default when you use write or editOrReply function it will only send a message to the channel where the command was fired. To make the command being replied you have to set reply suboption inside command option in the client options. This option is a callback which first parameter is the CommandContext and expects a boolean to be returned, if is true it will reply to the command, if not it will just send a message.

Here is a detailed example about how it will looks your client with all the modifications mentioned in this guide:

1
import { Client } from 'seyfert';
2
3
const client = new Client({
4
commands: {
5
prefix: (msg) => {
6
//here you can handle whichever prefixes you will want to be consider depending on the message data.
7
return ['!', '?', '.']
8
},
9
reply: (ctx) => true
10
}
11
});

Prefix commands options

Like application commands you can set options for prefix commands. The only think you will have to do it’s to declare them as you do in application commands. They are parsed with a regex that indicates options will be splited when they are followed by -. You can replace the parser with your own parser in the argsParser suboption inside commands options in client options. This is a callback whose params are content, the content of the message, and command, the command which was run and expects an object whose keys are the options and values the values of the options.

Here is a brief template which shows how options work in prefix commands.

Prefix Command Template
1
!command -option option_value -option2 option2_value

Here you can see how we make the previous command but instead of cross posting the current command cross posing another message, fetching it by its id that will be given to us in the options.

1
import {
2
Command,
3
CommandContext,
4
Declare,
5
Options,
6
createStringOption
7
} from 'seyfert';
8
9
const options = {
10
id: createStringOption({
11
description: 'The id of the message we are going to cross post',
12
required: true
13
})
14
};
15
16
@Declare({
17
name: 'crosspost',
18
description: 'Cross post and announcement message.'
19
});
20
@Options(options);
21
export default class CrosspostCommand extends Command {
22
async run(ctx: CommandContext<typeof options>){
23
await ctx.client.messages.crosspost(ctx.options.id, ctx.channelId);
24
return ctx.write({ content: 'I have cross posted your announcement.'});
25
}
26
};

Defering the reply with prefix commands

As in application commands you can defer reply the response of the command. By default if you use CommandContext.deferReply() function it will make the response to be deferred showing a Loading ... message. If we want it to be custom we will have to take another look into commands option in client options as we have to create a callback for deferReplyResponse which expects the content you will send in the defer to be returned.

Here is an example of how we modify the client options and how we make the response of the command to be deferred using the previous example.

1
import { Client } from 'seyfert';
2
3
const client = new Client({
4
commands: {
5
prefix: (msg) => {
6
//here you can handle whichever prefixes you will want to be consider depending on the message data.
7
return ['!', '?', '.']
8
},
9
reply: (ctx) => true,
10
deferReplyResponse: (ctx) => ({ content: 'Thinking...' })
11
}
12
});