提问者:小点点

获取/侦听通道中的新消息


你好,所以我正在制作一个机器人,它将在一个特定的通道中获取消息,复制它,然后将它发送到另一个通道。我尝试使用messages.fetch(),但它不起作用,总是返回DiscordapiError:无法发送空消息。我的代码目前是这样的:

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

我希望有人能帮我做这件事,提前谢谢。


共1个答案

匿名用户

假设您希望bot输出通道中最后10条消息的内容,那么这应该可以工作:

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
});

如果您只想获得一条消息,您可以执行以下操作:

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

单据