Modules
Introduction
sern operates with modules. At its core, modules are simple objects which contain properties type
,execute
, and plugins
(code ran before execute
).
Modules
We’ll walk you through creating your first command module.
If you installed a new project via the CLI, your file should be here:
Directorysrc/commands/
- ping.js (right here, probably)
- …
Application Commands
- Includes
CommandType.Slash
,CommandType.Both
,CommandType.CtxUser
,CommandType.CtxMsg
Chat Input Commands (Slash Commands)
1import { commandModule, CommandType } from "@sern/handler";2
3export default commandModule({4 type: CommandType.Slash,5 description: "A ping command",6 execute: async (ctx, sdt) => { // ctx is Context, sdt is SDT type7 await ctx.reply("Pong 🏓");8 },9});
Context Menus
1import { commandModule, CommandType } from "@sern/handler";2
3export default commandModule({4 type: CommandType.CtxMsg,5 execute: async (ctx) => {6 await ctx.reply("Hello.");7 },8});
Components
So, lets say you want to make a command module that listens to buttons, or ANY component (modals, select menus).
1import { CommandType, commandModule } from "@sern/handler";2import { ButtonStyle, ActionRowBuilder, ButtonBuilder } from 'discord.js';3export default commandModule({4 type: CommandType.Slash,5 description: "A ping command",6 execute: async (ctx, sdt) => {7 const editButton = new ButtonBuilder({8 customId: "btn",9 label: "Click me",10 emoji: "🛠",11 style: ButtonStyle.Primary,12 });13 await ctx.reply({14 content: "Pong 🏓",15 components: [new ActionRowBuilder().addComponents(editButton)]16 });17 },18});
1import { CommandType, commandModule } from "@sern/handler";2
3export default commandModule({4 type: CommandType.Button,5 execute: (ibtn) => {6 ibtn.reply('clicked')7 },8});
Dynamic Component Parameters
Components can carry metadata. This comes in handy when handling multiple components of the same purpose. This is good for passing small, string based data across components.
1import { CommandType, commandModule } from "@sern/handler";2import { ButtonStyle, ActionRowBuilder, ButtonBuilder } from 'discord.js';3export default commandModule({4 type: CommandType.Slash,5 description: "A ping command",6 execute: async (ctx, sdt) => {7 const editButton = new ButtonBuilder({8 customId: "btn/1061421834341462036",9 label: "Click me",10 emoji: "🛠",11 style: ButtonStyle.Primary,12 });13 await ctx.reply({14 content: "Pong 🏓",15 components: [new ActionRowBuilder().addComponents(editButton)]16 });17 },18});
1import { CommandType, commandModule } from "@sern/handler";2
3export default commandModule({4 type: CommandType.Button,5 execute: (ibtn, sdt) => {6 ibtn.reply('clicked with ' + sdt.params)7 },8});
Event Modules
We are now moving to event modules, which listens to the vast streams of data provided by
sern
,EventType.Sern
discord.js
,EventType.Discord
yourself
,EventType.External
If you haven’t already, add the events
directory to your config
1export const events = "./dist/events";
Directorysrc/events/
- messageCreate.js (right here, probably)
- …
Listening to Discord Events
1import { eventModule, EventType } from "@sern/handler";2
3export default eventModule({4 type: EventType.Discord,5 execute: async (message) => {6 console.log(`${message.user} said`, message.content)7 },8});