đŸ“ŦManaging messages

How to create a message?

Using REST methods

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

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

Using the Message itself

The main differences between creating a message with the message itself rather than using REST, are that it is faster, and behaves differently: it replies to it.

createMessage – Reply to the trigger message.

By replying to the trigger message, which is the message that triggers your application, that is most likely sent by a user: it creates an original response to it and acknowledges your message.

Message.createMessage(options); // acknowledges and reply to the Message

createFollowup

Send a follow-up message that replies to the original response, the current Message has to be acknowledged to be used.

Message.createFollowup(options);

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 app property and return if it is true
    // That prevents the app to send message infinitely
    // (app sends a message, it's immediately detected, so it resends and so on).
    if (member.app) 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).app) 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 app

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.

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.app will be undefined instead of a boolean.

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

OUTPUT (console):

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

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'

Though replying is already built-in the Message component methods, you can still

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.app) return;
    if (message.content == "hi"){
       message.createMessage({
          content: "hey there!",
          // replyMessageIds is a list of message IDs you're replying to.
          // (aditionally)
          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 app.

1. Storing new messages.

client.on("messageCreate", async (message)=> {
   const member = await message.member;
   if (member.app) 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.app) return;
   if (message.content == "!ping"){
       await message.createMessage({ content: ".." });
       await message.editLastMessage({ content: "pong!" });
   }
});

2. Using editOriginal

client.on("messageCreate", async (message) => {
   const member = await message.member;
   if (member.app) return;
   if (message.content == "!ping"){
       const res = await message.createMessage({ content: ".." });
       res.editOriginal({ content: "res edited!" });
       // it is way more useful when you only have a children of an original
       // response message
   }
});

2. Using editFollowup

A derivative of editLast, that only works on followups.

Last updated