简体   繁体   中英

Discord JS, console.log undefined

I want to use the msg.content somewhere else,

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)

日志

It works when I use .then but I don't want to use it.

In the docs you can see that TextChannel.awaitMessages() returns a Promise to a Collection which is not the same as an array.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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