Laracord's Message
component provides a powerful fluent syntax to send Discord messages to users and channels.
When working in the context of Laracord, the Message
component is almost always available using the $this->message()
method.
In it's simplest form, message generally requires some form of content and a destination.
$this
->message('Hello world!')
->send('channel-id');
$this
->message('Hello user!')
->sendTo('user-id');
This gives you a sensibly styled embed with the username and avatar of your bot.
To send a plain message without an embed, you may pass your content to the ->body()
method:
$message = $this
->message()
->body('Hello world!');
When $this->message()
is not available in your class, you can typically access and make Message
directly:
use Laracord\Discord\Message;
$message = Message::make()->content('Hello world!');
Set the title of the embed:
$message = $this
->message('Hello world!')
->title('Example');
Set the content of the embed. This can be used instead of passing content to message
directly:
$message = $this
->message()
->content('Hello world!');
By default, this is the username of your bot application.
$message = $this
->message('Hello world!')
->username('Laracord');
By default, this is the avatar of your bot application.
$message = $this
->message('Hello world!')
->avatarUrl('...');
This determines whether your message is a TTS message.
$message = $this
->message('Hello world!')
->tts(true);
By default, Laracord sends embeds using ->success()
which provides a green border color.
This can be overriden by passing a color in decimal directly to ->color()
or by using the other available color helper methods:
$message = $this
->message('Hello world!')
->color('3066993')
->success()
->error()
->warning()
->info();
The embed footer can contain text as well as an icon.
$message = $this
->message('Hello world!')
->footerText('Sent by Laracord')
->footerUrl('...');
The thumbnail URL appears as a medium-sized image to the right of the embed.
$message = $this
->message('Hello world!')
->thumbnailUrl('...');
The image URL appears as a large-sized image inside of the embed.
$message = $this
->message('Hello world!')
->imageUrl('...');
The timestamp appears at the bottom of the embed and is shown to the user in their local time.
$message = $this
->message('Hello world!')
->timestamp(now());
The embed author is shown at the top of the embed. By default, Laracord set's the authorName
and authorIcon
to the bot's username and avatar.
$message = $this
->message('Hello world!')
->authorName('Laracord')
->authorIcon('...')
->authorUrl('...');
Fields consist of groups of data represented by a label and value. By default, they are inline with Discord showing up to 3 fields per line.
$message = $this
->message('Hello world!')
->field('Field 1', 'Value 1')
->field('Field 2', 'Value 2');
To pass a group of fields, you can use the ->fields()
method:
$message = $this
->message('Hello world!')
->fields([
'Field 1' => 'Value 1',
'Field 2' => 'Value 2',
]);
To prevent a field from being inline, you can pass inline: false
like so:
$message = $this
->message('Hello world!')
->field('Field 1', 'Value 1')
->field('Field 2', 'Value 2', inline: false);
You can also conditionally display a field:
$message = $this
->message('Hello world!')
->field('Field 1', 'Value 1')
->field('Field 2', 'Value 2', hidden: true);
Attaching a file to your message can be done using raw content or by passing a path:
$message = $this
->message('Hello world!')
->file('Lorem ipsum...', 'lorem.txt');
$message = $this
->message('Hello world!')
->filePath('path/to/file');
Buttons are shown at the bottom of your message and can consist of URL's or interactions. Laracord tries to make both easy to use.
A button in it's simplest form would be a link button and consists of a simple label and URL value:
$message = $this
->message('Hello world!')
->button('Visit Laracord', 'https://laracord.com');
When working with a link button, you also have the option of passing an emoji:
$message = $this
->message('Hello world!')
->button('Visit Laracord', 'https://laracord.com', emoji: '💻');
When using a custom emoji from a server your bot is in, you will have to pass the emoji along with it's internal ID.
Tip
An easy way to obtain this as a string is to escape the emoji in a Discord chat message.
\:laracord:
would return<:laracord:1204740745286656050>
containing the string needed.
Once you obtain the emoji ID, you can then pass it to emoji
as a string:
$message = $this
->message('Hello world!')
->button('Visit Laracord', 'https://laracord.com', emoji: ':laracord:1204740745286656050');
Interactions allow you to have the bot respond or perform an action when a button is clicked. This can be achieved by passing an Interaction
callback as the button's value
instead of a string:
use Discord\Parts\Interactions\Interaction;
$message = $this
->message('Say hello!')
->button('Hello', fn (Interaction $interaction) => $interaction->respondWithMessage(
$this->message('Well hello to you!')->build(),
ephemeral: true
), emoji: '👋');
In the example above, we respond to the interaction as well as set ephemeral
to true
to ensure the button can only be interacted with a single time.
When working with buttons that resolve interactions, you also have the option of changing the button style.
The available options are primary
(default), secondary
, success
, and danger
:
use Discord\Parts\Interactions\Interaction;
$message = $this
->message('Say hello!')
->button('Hello', fn (Interaction $interaction) => $interaction->respondWithMessage(
$this->message('Well hello to you!')->build(),
ephemeral: true
), emoji: '👋', style: 'success');