Sending Messages
The basic feature of the Discord Bots is to send messages all around Discord. And in Seyfert you can send them in the easiest way.
First of all, we have to setup a basic Hello World
command.
1import {2 Command,3 Declare4} from "seyfert";5
6@Declare({7 name: "helloworld",8 description: "Send a basic hello world message."9})10export default class HelloWorldCommand extends Command {11 async run(ctx: CommandContext) {}12}
Having set up our basic Hello World
command we are ready to send our first message using CommandContext.write()
function.
1import {2 Command,3 Declare4} from "seyfert";5
6@Declare({7 name: "helloworld",8 description: "Send a basic hello world message."9})10export default class HelloWorldCommand extends Command {11 async run(ctx: CommandContext) {12 return ctx.write({ content: "Hello World π" });13 }14}
The CommandContext.write()
function will respond to the command.
EditOrReply
But, what about responding to the command or editing itβs reply instead of simply replying to it?
We can use CommandContext.editOrReply()
function. This function is used to reply the command or, if the response has been sent, edit it.
This function is very useful if we want to develop a command which responds to the command or if the command was responded the response will be edited. If we are only using a bare CommandContext.write()
we will send a reply in all the cases.
Here is an example of how implement this function.
1export default class HelloWorldCommand extends Command {2 async run(ctx: CommandContext) {3 await ctx.deferReply();4
5 // do something that takes time and is boring6
7 await ctx.editOrReply({ content: "I made things" });8 }9}
Sending messages without a reply
Reading this guide you could have thought about the possibility of only send a message to a channel instead of replying to the command.
Here we are. To send a simple message to a specific channel we need to retrive itβs id and then access to BaseClient.messages
property and run the write
function.
Here is an example of how to send that message without replying a command:
1export default class HelloWorldCommand extends Command {2 async run(ctx: CommandContext) {3 return ctx.client.messages.write(ctx.channelId, { content: "Hello World π" });4 }5}
Sending Embeds
Discord adds the possibility to send embeded messages within a channel.
To send those embeded messages with Seyfert we will have to build up the embed with the Embed builder. For further information about the cusomization of the embeded message you can check the Embed builder within this docs.
Here is an example of how to send an embed with a custom title and description.
1import { Embed } from "seyfert"2
3export default class HelloWorldCommand extends Command {4 async run(ctx: CommandContext) {5
6
7 const embed = new Embed()8 .setTitle("My Awesome Embed")9 .setDescription("Hello World π")10
11 return ctx.write({ embeds: [embed] });12 }13}
Sending components attached to the message
Discord includes the possibility to send components attached to the message within an ActionRow
. Those components can be Buttons
or SelectMenus
.
The components are stored into an ActionRow
which can contain up to 5 diffrent buttons and only one select menu and canβt contain inside another ActionRow.
In this example we are going to send two actions rows within the message. Each row is going to have a button and a string select menu attached respectively.
1import {2 ActionRow,3 Button,4 StringSelectMenu,5 ButtonStyle,6 StringSelectOption7} from "seyfert";8
9export default class HelloWorldCommand extends Command {10 async run(ctx: CommandContext) {11
12
13 const button = new Button()14 .setCustomId("helloworld")15 .setLabel("Hello World")16 .setStyle(ButtonStyle.Primary)17
18 const buttonRow = new ActionRow<Button>()19 .addComponents(button)20
21
22 const menu = new StringSelectMenu()23 .setCustomId("select-helloworld")24 .addOption(25 new StringSelectOption().setLabel("Hello").setValue("option_1")26 )27
28 const menuRow = new ActionRow<StringSelectMenu>()29 .addComponents(menu)30
31 return ctx.write({ content: "Hello World π", components: [buttonRow, menuRow] });32 }33}