Javascript 不和谐嵌入问题和[对象承诺]
我正在编写一个discord机器人,我需要知道谁对指定的消息做出了反应,并将他们的用户名放入discord嵌入 代码如下:Javascript 不和谐嵌入问题和[对象承诺],javascript,node.js,discord,discord.js,es6-promise,Javascript,Node.js,Discord,Discord.js,Es6 Promise,我正在编写一个discord机器人,我需要知道谁对指定的消息做出了反应,并将他们的用户名放入discord嵌入 代码如下: MSG.messages.fetch({ around: GameID, limit: 1 }).then((msg) => { fetchedMsg = msg.first(); let MessageOBJ = fetchedMsg.reactions.cache.get("Try initiating the promise before yo
MSG.messages.fetch({ around: GameID, limit: 1 }).then((msg) => {
fetchedMsg = msg.first();
let MessageOBJ = fetchedMsg.reactions.cache.get("Try initiating the promise before you create the embed.
function promise() {
return new Promise((resolve, reject) => {
resolve(['User 1', 'User 2', 'User 3']);
});
}
console.log(promise().then((result) => result)) // putting the promise inside
promise().then((result) => console.log(result)) // initiating it outside
// also, make sure to use `Array.map` instead of `Array.forEach`
promise().then((result) => console.log(result.forEach((user) => `${user}\n`)))
promise().then((result) => console.log(result.map((user) => `${user}\n`)))
MSG.messages.fetch({around:GameID,limit:1})。然后((MSG)=>{
fetchedMsg=msg.first();
让MessageOBJ=fetchedMsg.reactions.cache.get(“在创建嵌入之前尝试启动承诺
函数承诺(){
返回新承诺((解决、拒绝)=>{
解析(['User 1'、'User 2'、'User 3']);
});
}
log(promise().then((result)=>result))//将承诺放入
promise()。然后((结果)=>console.log(结果))//在外部启动它
//另外,请确保使用'Array.map'而不是'Array.forEach'`
promise().then((result)=>console.log(result.forEach((user)=>`${user}\n`)))
promise().then((result)=>console.log(result.map((user)=>`${user}\n`))
我不是discord api的专家,但是您遇到的可能是promises的问题。该方法返回一个promise,其中可能包含信息
有几种方法可以解决这个问题。我更喜欢的方法是使用
正如您所见,用户需要的是实际数据,而不是返回数据的承诺
要使其工作,您可能必须将函数标记为
此外,对于您要实现的功能,某些内联函数的括号格式似乎也不正确
你追求的“最终结果”可能是这样的:
MSG.messages.fetch({around:GameID,limit:1})
。然后(异步消息=>{
const fetchedMsg=msg.first();
const MessageOBJ=fetchedMsg.reactions.cache.get(“使用Array.prototype.map()
而不是Array.prototype.forEach()
我试过了,但这仍然给了我[object Promise]你能试着console.log(users)
吗?我想,但我是我试过的问题,这给了我“未定义的”这里有一篇关于arrow函数返回规则的随机文章:这不是我的下一票。有没有一种方法可以在users.map(element=>…)中使用“if”并且代码可以继续正常工作?是的,但是你必须使用return.Egusers.map(user=>{if(user.something){return something}else{return'something'}})
const embed = new Discord.MessageEmbed()
.setTitle('All players here !')
.setDescription('Here\'s all of the players for this game')
.addField('Players', await MessageOBJ.users.fetch().then(users => {
users.map(element =>
`${element.username}\n`
)
}))