Ad

Fetch/Listen For A New Message In The Channel

- 1 answer

Hello so I'm making a bot that will fetch a message in a specific channel, copies it and then sent it to another channel. I tried using messages.fetch() but it doesn't work and always returns with DiscordAPIError: Cannot send an empty message. This is what my code is currently like :

  if (message.content === 'Log'){
message.channel.messages.fetch({limit: 10})
.then(messages => {
  message.channel.send(messages)}).catch(console.error)}

I hope anyone can help me with this, thanks in advance.

Ad

Answer

Assuming you want the bot to output the content of the last 10 messages in the channel, this should work:

let msgs = [];
message.channel.messages.fetch({limit: 10})
.then(messages => {
    return messages.each(msg => msgs.push(msg.content));
})
.then(messages => {
    message.channel.send(msgs.reverse().join("\n")); // outputs the messages separated by a newline
});

If you only want to get one message you can just do:

message.channel.messages.fetch("MESSAGE_ID")
.then(msg => {
    message.channel.send(msg.content);
});

Docs

Ad
source: stackoverflow.com
Ad