【问题标题】:How to get the last message of a specific discord channel如何获取特定不和谐频道的最后一条消息
【发布时间】:2019-12-28 01:20:17
【问题描述】:

我一直在尝试获取一个频道在另一个频道中写入命令的最后一条消息。

我想做这样的事情:

频道 1 :(我写)“获取频道 2 的最后一条消息 通道 2:最后一条消息是 ("Hello"); 频道 1:我收到频道 2 的最后一条消息说,频道 2 的最后一条消息是。 “你好”。

我的代码是这样的,但不起作用。

 if (message.channel.id === '613573853565681671') {
   message.channel.fetchMessages().then(collected => {
     const filtered = collected.filter(message => message.author.bot);
     const lastMessages = filtered.first(1); //Get the last message of this channel.
     const TextMessages = `${lastMessages[0].content}`;
     console.log(TextMessages + " is the last message of aviso jobs")

     if (TextMessages === "look") { //If the last message of this channel is look then go to the other channel

       if (message.channel.id === '613553889433747477') {
         message.channel.fetchMessages().then(collected => {
           const filtered = collected.filter(message => message.author.bot);
           const lastMessages2 = filtered.first(1); //Get the last message of this other channel.
           const TextMessages2 = `${lastMessages2[0].content}`;
           console.log(TextMessages2 + " is the last message of bot")
         });
       }
     }
   });
 }

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    我在理解你的代码时遇到了麻烦,所以我会这样做,也许它会对你有所帮助:

      if (message.content.startsWith("!lastmessage")) {
        let args = message.content.split(" "); //find every word of the message
        if (args.length > 1) { //make sure that there is at least 1 argument, which should be the target channel's id
          let target = message.guild.channels.find(c => c.id == args[1]) || null; // find the targeted channel
          if (target) { // make sure that the targeted channel exists, if it exists then fetch its last message
            target
              .fetchMessages({ limit: 1 })
              .then(f =>
                message.channel.send(
                  `Last message from <#${target.id}> is...\n> ${f.first().content}`
                ) // send the last message to the initial channel
              );
          } else { //if target doesn't exist, send an error
            message.channel.send("Target does not exist!");
          }
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2019-12-28
      • 1970-01-01
      • 2019-04-11
      • 2019-05-10
      • 1970-01-01
      • 2021-06-26
      • 2020-07-14
      • 2021-04-15
      • 1970-01-01
      相关资源
      最近更新 更多