简体   繁体   English

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

[英]How do I get all the messages of a specific user on a specific channel

So here's the chat (the channel's name is newchannel - the channel is created when the player does the command): 所以这是聊天(频道的名称是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

Now, I'd like to send all the User's messages to a specific channel so that I can create an embed that looks like this when display in the channel: 现在,我想将所有用户的消息发送到特定频道,以便在频道中显示时可以创建一个看起来像这样的嵌入:

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

I predict the embed will be something like: 我预测嵌入将是这样的:

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

I'm trying to make a variable with the string being the ${message.content} of a specific message in chat. 我正在尝试使用字符串作为聊天中特定邮件的${message.content}来创建变量。

To get the messages of a channel you can use TextChannel.fetchMessages() . 要获取通道的消息,可以使用TextChannel.fetchMessages() You can then filter those messages to keep only the ones sent by the user with Collection.filter() . 然后,您可以过滤这些消息,以便仅保留用户使用Collection.filter()发送的消息。 After that, you can use the first three messages to build your embed. 之后,您可以使用前三条消息来构建嵌入。
Here's an example: 这是一个例子:

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

Instead of fetching the messages later, you can log the results accurately in real time using awaitMessages() . 您可以使用awaitMessages()实时准确地记录结果,而不是稍后获取消息。 You should be doing so anyway with the chain of questions. 无论如何,你应该通过一系列问题这样做。

Place the code below after you've created the new channel ( channel ). 在创建新频道( channel )后,将代码放在下面。 The bot will ask the first question and wait for the user's response. 机器人将询问第一个问题并等待用户的回复。 It will then be added to the embed and the next question will be asked. 然后它将被添加到嵌入中,并将询问下一个问题。 After the last question, the embed will be sent and the channel will be deleted. 在最后一个问题之后,将发送嵌入并删除该频道。

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.');

Note: The code needs to be within an async function. 注意:代码需要在异步函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM