First Command
We will dissect a basic command.
If you installed a new project via the cli, This is the ping
command located in src/commands folder.
Typescript
import { commandModule, CommandType } from '@sern/handler';
export default commandModule({
type: CommandType.Both,
plugins: [],
description: 'A ping command',
// alias : [],
execute: async (ctx, args) => {
await ctx.reply({ content: 'Pong 🏓' });
},
});
Javascript
const { CommandType, commandModule } = require('@sern/handler');
exports.default = commandModule({
type: CommandType.Both,
plugins: [],
description: 'A ping command',
// alias : [],
execute: async (ctx, args) => {
await ctx.reply('Pong 🏓');
},
})
To view what each of these properties mean in depth, visit the official documentation.
Types of command modules
Every command module type
is part of an enum. This field allows type inference for the rest of a module's fields.
All the command types can be found in the official documentation!
So, lets say you want to make a command module that listens to modals.
Note: Keep in mind you'll need to send a modal with a custom id dm-me
. This example below is the response to a modal being sent.
Typescript:
import { commandModule, CommandType } from '@sern/handler';
export default commandModule({
name: 'dm-me',
type: CommandType.Modal,
async execute (modal) {
const value = modal.fields.getTextInputValue('message');
modal.client.users.fetch('182326315813306368').then( u =>
u.send(value + ` from ${modal.user}`)
);
modal.reply( { ephemeral:true, content: 'Sent' })
}
});
Javascript:
const { CommandType, commandModule } = require('@sern/handler');
exports.default = commandModule({
name: 'dm-me',
type: CommandType.Modal,
async execute (modal) {
const value = modal.fields.getTextInputValue('message');
modal.client.users.fetch('182326315813306368').then( u =>
u.send(value + ` from ${modal.user}`)
);
modal.reply( { ephemeral:true, content: 'Sent' })
}
});
Commands are straight forward. Keep in mind, every other property on the commandModule object is optional except the type and execute function.
Context class
The provided Context class helps with modules of CommandType.Both
(A mixture of slash / legacy commands).
The Context class is passed into modules with type:
CommandType.Both
CommandType.Slash
CommandType.Text
This data structure helps interop between legacy commands and slash commands with ease.
View the docs
Typescript:
export default commandModule({
name: 'ping',
type: CommandType.Both,
async execute(ctx: Context) {
await ctx.reply(`pong ${ctx.user}`)
// .reply is shared between both message and interaction!
// So is an User object!
}
});
Javascript:
exports.default = commandModule({
name: 'ping',
type: CommandType.Both,
async execute(ctx) { //ctx is a Context instance
await ctx.reply(`pong ${ctx.user}`)
// .reply is shared between both message and interaction!
// So is an User object!
}
});