discord.Message

discord.Message

https://pylon.bot/docs/reference/classes/discord.message.html

A discord.Message instance represents a message on Discord. Messages extend past simple messages sent by users. They may be special system messages like member join or boost messages (if enabled for the guild), messages sent via Discord Webhooks, or even messages broadcasted from other guilds using Announcement channels.

Message Sub-Types

discord.GuildMemberMessage

discord.Message has a child class called discord.GuildMemberMessage which represents a Discord message where type is 0 (DEFAULT) and has a member field present. Any message sent by a real Discord user (or another Bot) in a guild will be of this type.

You can check for this type in an event handler to prevent processing system messages that may result in errors.

discord.on('MESSAGE_CREATE', async message => {
if (!(message instanceof discord.GuildMemberMessage)) {
return;
}
// now, we can safely access fields we know should exist on the message object
if (message.content === "hello") {
await message.reply(`${message.author.toMention()})
}
});

Outgoing Messages

Sending messages is easy. Check out the discord.OutgoingMessage, which is accepted by GuildTextChannel.sendMessage(...) and Message.reply(...).

// Send a simple message
message.reply("Hello!");

Sending Typing Events

Sometimes a response to a command may take some time to compute. Any method that accepts an OutgoingMessage may also consume a Promise\<OutgoingMessage>. When you use a Promise, a "typing" indicator is sent giving your bot a more natural feel.

If the promise resolves within 50ms, the typing event will not be sent.

// Send a "typing event" until the promise resolves, to simulate the bot "computing" a response.
message.reply(async () => {
await sleep(3000);
return "Hello, 3 seconds later!";
});

Message Embeds

Discord allows us to send Rich Embed objects attached to messages that render as nice info boxes in chat.

embed

(An example of a rich embed)

For all the Embed options and properties, check out the SDK Reference for Embed objects:

https://pylon.bot/docs/reference/classes/discord.embed.html

Below is an example of a command !userinfo @user that replies with a rich embed containing information about the user. It will look similar to the one pictured above.

const commands = new discord.command.CommandGroup();
commands.on(
"info",
(args) => ({
user: args.user(),
}),
async (message, { user }) => {
// Build the rich embed
const richEmbed = new discord.Embed();
richEmbed.setTitle(user.getTag());
richEmbed.setColor(0x00ff00);
richEmbed.setDescription("User Information Example");
richEmbed.setThumbnail({ url: user.getAvatarUrl() });
richEmbed.addField({
name: "User ID",
value: user.id,
inline: false,
});
richEmbed.setTimestamp(new Date().toISOString());
// Reply to the user's message with our constructed embed
await message.reply(richEmbed);
}
);