Saltearse al contenido
sern

Modules

Esta página aún no está disponible en tu idioma.

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)

src/commands/ping.js
1
import { commandModule, CommandType } from "@sern/handler";
2
3
export default commandModule({
4
type: CommandType.Slash,
5
description: "A ping command",
6
execute: async (ctx, sdt) => { // ctx is Context, sdt is SDT type
7
await ctx.reply("Pong 🏓");
8
},
9
});

Context Menus

src/commands/hello.js
1
import { commandModule, CommandType } from "@sern/handler";
2
3
export 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).

src/commands/ping.js
1
import { CommandType, commandModule } from "@sern/handler";
2
import { ButtonStyle, ActionRowBuilder, ButtonBuilder } from 'discord.js';
3
export 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
});

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.

src/commands/ping.js
1
import { CommandType, commandModule } from "@sern/handler";
2
import { ButtonStyle, ActionRowBuilder, ButtonBuilder } from 'discord.js';
3
export 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
});

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

1
export const events = "./dist/events";
  • Directorysrc/events/
    • messageCreate.js (right here, probably)

Listening to Discord Events

src/events/messageCreate.js
1
import { eventModule, EventType } from "@sern/handler";
2
3
export default eventModule({
4
type: EventType.Discord,
5
execute: async (message) => {
6
console.log(`${message.user} said`, message.content)
7
},
8
});