đŸ“ŦManaging messages

How to create a message?

Accessing from the Client the method is create a message is pretty simple and straightforward.

client.rest.channels.createMessage(channelID, options);
// e.g:
client.rest.channels.createMessage("abcd-12345", { content: "hi!" });

You can also access it using various helper methods provided by the library (aka shortcut methods):

client.createMessage(channelID, options);
myMessage.createMessage(channelID, options);
// e.g:
client.createMessage("abcd-12345", { content: "hi!" });
// you don't have to specify the channelID when sending directly from a Message class!
myMessage.createMessage({ content: "hi!" });

How do I detect when a message is created, edited or deleted?

We use client.on to detect when an event is triggered.

Detect when a message is created

client.on("messageCreate", async (message) => {
    // Whatever you want to do when a message is created
}

Detect when a message is edited

client.on("messageUpdate", async (message, oldMessage) => {
    // Whatever you want to do when a message is edited
}

Detect when a message is deleted

client.on("messageUpdate", async () => {
    // Whatever you want to do when a message is deleted
}

We're shipping TouchGuild with a wide range of events, you can check them out here.

Create a message when someone says 'hi'

client.on("messageCreate", async (message)=> {
    // the member property is a getter that returns a Promise
    // that's why we await it before using it.
    const member = await message.member;
    // We can get the bot property and return if it is true
    // That prevents the bot to send message infinitely
    // (bot sends a message, it's immediately detected, so it resends and so on).
    if (member.bot) return;
    // We check if the content fo the message is hi
    if (message.content == "hi") {
       // We create a message!
       message.createMessage({ content: "hey there!" });
    }
});

Another way to do that without storing the message 'member'

client.on("messageCreate", async (message)=> {
    if (await (message.member).bot) return;
    if (message.content == "hi") {
       message.createMessage({ content: "hey there!" });
    }
});

Common mistakes

Not adding the condition to return in case the user that sent the message is a bot

Yes, it is a fairly common mistake among beginners, by not putting this condition and send a message whatever the message is, this can cause sooner or later a creation of a bunch of messages in a short period of time.

client.on("messageCreate", async (message)=> {
    if (message.content == "hi") {
       message.createMessage({ content: "hey there!" });
    }
    // this will loop
    message.createMessage({ content: "a message has been sent!" });
    
    // this will loop as well, as the sent message
    // is the same as the message content detected
    if (message.content == "hello") {
       message.createMessage({ content: "hello" });
    }
});

In this case, we're detecting if the message content is 'hi', which makes it unable to loop, but it could happen by running messageCreate outside the condition.

E.g:

I send the message 'hello'

  • It detects the message hello

  • Sends a message with content 'a message has been sent!'

  • Detects if the content is hello, if it is, it sends a message with content: hello

  • Detects a message 'hello'

  • ... loops .. and so on..

OUTPUT (chat channel):

  • a message has been sent!

  • hello

  • a message has been sent!

  • hello

  • ... and so on..

Using message.member directly without awaiting it

message.member is a getter method that returns a Promise, you have to await it to be able to access to its properties, in that case message.member.bot will be undefined instead of a boolean.

client.on("messageCreate", async (message)=> {
    if (message.member.bot) return;
    if (message.content == "hi") {
       message.createMessage({ content: "hey there!" });
    }
});

OUTPUT (console):

TypeError: Cannot read properties of undefined (reading 'bot')

and don't forget to make the function asynchronous (async) if you're using await inside it!

Replying to a message

The difference with creating a message and replying, is that the reply mentions the user, and links the new message to a 'replied message'

It is very similar to creating a message, you only add the property replyMessageIds

client.on("messageCreate", async (message)=> {
    const member = await message.member;
    if (member.bot) return;
    if (message.content == "hi"){
       message.createMessage({
          content: "hey there!",
          // replyMessageIds is a list of message IDs you're replying to.
          replyMessageIds: [message.id]
       });
    }
});

Logging things to a specific channel

client.on("messageUpdate", async (message)=> {
    client.createMessage("channel id", { content: `A message has been updated (channel id: ${message.channelID})` });
});

Deleting messages containing swear words

client.on("messageCreate", (message) => {
    if (message.content.includes("fuck")) message.delete();
});

Editing messages

There's different ways to edit messages sent by the bot.

1. Storing new messages.

client.on("messageCreate", async (message)=> {
   const member = await message.member;
   if (member.bot) return;
   if (message.content == "!ping"){
       // store inside a let the message
       let reply = await message.createMessage({ content: ".." });
       // edit the stored, recently created message.
       await reply.editMessage({ content: "pong!" });
   }
});

2. Using editLast

This method called editLast was previously editLastMessage in older TouchGuild versions

client.on("messageCreate", async (message)=> {
   const member = await message.member;
   if (member.bot) return;
   if (message.content == "!ping"){
       await message.createMessage({ content: ".." });
       await message.editLastMessage({ content: "pong!" });
   }
});

Last updated