简体   繁体   English

Discord JS,console.log 未定义

[英]Discord JS, console.log undefined

I want to use the msg.content somewhere else,我想在其他地方使用msg.content

const filter = (m) => m.author.id === message.author.id;
message.channel.send('How many players?');

const msg = await message.channel.awaitMessages(filter, {
  max: 1,
  time: 10000,
});
console.log(msg.content);

but when I console log msg.content it says that it's undefined but when I console log everything I can see the content (image below)但是当我控制台记录msg.content它说它undefined但是当我控制台记录所有内容时我可以看到内容(下图)

日志

It works when I use .then but I don't want to use it.它在我使用.then时有效,但我不想使用它。

In the docs you can see that TextChannel.awaitMessages() returns a Promise to a Collection which is not the same as an array. 在文档中,您可以看到TextChannel.awaitMessages()将 Promise 返回到与数组不同的集合

So what could work is this:所以可能的工作是这样的:

const filter = (m) => m.author.id === message.author.id;
message.channel.send('How many players?');

//it will return a collection even if there's only one message in it
var msg = await message.channel.awaitMessages(filter, {
  max: 1,
  time: 10000,
});

//convert Collection to array (Array of all messages the filter caught)
msg = Array.from(msg.values())[0];

//that should give the content of the first message the filter caught
console.log(msg.content);

I hope that this works.我希望这行得通。 If not please tell me.如果没有,请告诉我。

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

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