【问题标题】:How do I get all the messages of a specific user on a specific channel如何获取特定频道上特定用户的所有消息
【发布时间】:2019-05-21 04:11:24
【问题描述】:

所以这里是聊天(频道名称是newchannel - 频道是在玩家执行命令时创建的):

Bot: Hello, what's your name.
User: BaconSoda479
Bot: When is your birthday
User: 21/06/1999
Bot: Do you like this bot?
User: Yes

现在,我想将用户的所有消息发送到特定频道,以便我可以创建一个在频道中显示时看起来像这样的嵌入:

User: BaconSode479
Birthday: 21/06/1999
Opinion: Yes

我预测嵌入会是这样的:

`User: ${client.channels.get(newchannel.id).second.message}`
`Birthday: ${client.channels.get(newchannel.id).fourth.message}`
`Opinion: ${client.channels.get(newchannel.id).sixth.message}`

我正在尝试使用字符串作为聊天中特定消息的${message.content} 的变量。

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    要获取频道的消息,您可以使用TextChannel.fetchMessages()。然后,您可以过滤这些消息以仅保留用户使用Collection.filter() 发送的消息。之后,您可以使用前三个消息来构建您的嵌入。
    这是一个例子:

    // Assuming newchannel is the TextChannel where the messages are, and that the only user allowed to write in the channel is the one you want to get the data from.
    
    newchannel.fetchMessages().then(collected => {
      const filtered = collected.filter(msg => !msg.author.bot);
      const messages = filtered.first(3);
    
      const text = `
        User: ${messages[0].content}
        Birthday: ${messages[1].content}
        Opinion: ${messages[2].content}
        `;
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用awaitMessages() 实时准确地记录结果,而不是稍后获取消息。无论如何,您应该对一系列问题这样做。

      在您创建新频道 (channel) 后,将代码放在下面。机器人将询问第一个问题并等待用户的响应。然后它将被添加到嵌入中,并提出下一个问题。最后一个问题后,将发送嵌入并删除频道。

      const questions = [
        { title: 'Name', message: 'Hello! What\'s your name?' },  // You can change these
        { title: 'Birthday', message: 'When\'s your birthday?' }, // messages or add any
        { title: 'Opinion', message: 'Do you like this bot?' }    // questions as you please.
      ];
      
      const filter = m => m.author.id === message.author.id;
      
      var embed = new Discord.RichEmbed()
        .setColor('#ffffff')                                             // Feel free to edit this
        .setAuthor(message.author.tag, message.author.displayAvatarURL); // embed; only an example.
      
      for (const question of questions) {    
        await channel.send(question.message);
        await channel.awaitMessages(filter, { max: 1, time: 60000 })
          .then(collected => {
            const response = collected.first() ? collected.first().content : '*No Answer*';
            embed.addField(question.title, response.length <= 1024 ? response : `${response.slice(0, 1021)}...`)
          });
      }
      
      const responsesTo = message.guild.channels.get('ID'); // Insert the ID to your channel here.
      if (!responsesTo) return;
      
      await responsesTo.send(embed); // Could make a webhook for efficiency instead.
      await channel.delete();
      
      console.log('Done.');
      

      注意:代码需要在异步函数中。

      【讨论】:

        猜你喜欢
        • 2020-06-29
        • 1970-01-01
        • 1970-01-01
        • 2021-07-28
        • 1970-01-01
        • 2012-12-07
        • 2014-09-13
        • 2021-04-21
        • 2019-12-28
        相关资源
        最近更新 更多