如何获得特定用户在特定频道上的所有消息

时间:2019-05-19 09:01:22

标签: javascript discord.js

这就是聊天室(频道名称为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}

2 个答案:

答案 0 :(得分:0)

要获取频道消息,可以使用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}
    `;
});

答案 1 :(得分:0)

您可以使用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.');

注意:代码必须在异步函数中。

相关问题