Recipes

Sending Messages

The basic feature of Discord Bots is sending messages across Discord. That's why here we will see how to send messages with Seyfert.

First of all, we need to set up a basic Hello World command.

src/commands/helloworld.ts
import { , , type  } from 'seyfert';
 
@({
  : 'helloworld',
  : 'Sends a basic hello world message.',
})
export default class  extends  {
  async (: ) {}
}

Having set up our basic Hello World command, we are now ready to send our first message using the CommandContext.write() function.

src/commands/helloworld.ts
import { , , type  } from 'seyfert';
 
@({
  : 'helloworld',
  : 'Sends a basic hello world message.',
})
export default class  extends  {
  async (: ) {
    return .({ : 'Hello world 👋' });
  }
}

The CommandContext.write() function will respond to the command.

EditOrReply

But what if we want to respond to the command or edit its response instead of just replying?

We can use the CommandContext.editOrReply() function. This function is used to reply to the command, or if a reply has already been sent, it will edit it.

This function is very useful if we want to develop a command that responds to the command or, if the command has been answered, edits the response. If we are only using a simple CommandContext.write(), a response will be sent in all cases.

Here’s an example of how to implement this function.

src/commands/helloworld.ts
import { , type  } from 'seyfert';
 
export default class  extends  {
  async (: ) {
    await .();
 
    // do something that takes time and is boring
 
    await .({ : 'I did some stuff' });
  }
}

Sending Messages Without a Response

While reading this guide, you may have thought about the possibility of simply sending a message to a channel instead of responding to a command.

Here we are. To send a simple message to a specific channel, we need to get its ID and then access the BaseClient.messages property and use the write function.

Here’s an example of how to send that message without replying to a command:

src/commands/helloworld.ts
import { , type  } from 'seyfert';
 
export default class  extends  {
  async (: ) {
    return ...(., { : 'Hello world 👋' });
  }
}

Sending Embeds

Discord adds the ability to send embedded messages within a channel.

To send these embedded messages with Seyfert, we need to construct the embed using the Embed builder. For more information about customizing the embedded message, you can check out the Embed builder within this documentation.

Here’s an example of how to send an embed with a custom title and description.

src/commands/helloworld.ts
import { , , type  } from 'seyfert';
 
export default class  extends  {
  async (: ) {
    
    
    const  = new ()
      .('My Amazing Embed')
      .('Hello world 👋');
 
    await .({ : [] });
  }
}

Sending Components Attached to the Message

Discord includes the ability to send components attached to a message within an ActionRow. These components can be Buttons or SelectMenus.

Components are stored in an ActionRow, which can contain up to 5 different buttons and only one select menu and cannot contain another ActionRow inside.

In this example, we are going to send two action rows within the message. Each row will have a button and a string select menu attached, respectively.

src/commands/helloworld.ts
import {
  ,
  ,
  ,
  ,
  ,
  type 
} from 'seyfert';
import {  } from 'seyfert/lib/types'
 
export default class  extends  {
  async (: ) {
   
   
    const  = new ()
      .('helloworld')
      .('Hello World')
      .(.);
 
    const  = new <>().();
 
 
 
    const  = new ()
      .('select-helloworld')
      .(
        new ().('Hello').('option_1')
      );
 
    const  = new <>().();
 
    await .({ : 'Hello world 👋', : [, ] });
  }
}

For more information about components

For more information about components, check out the component guide.

On this page