【问题标题】:Discord.js bot embeds using weighted random choice from an arrayDiscord.js 机器人使用数组中的加权随机选择嵌入
【发布时间】:2018-09-12 03:42:31
【问题描述】:

您好,我正在使用 discord.jsdiscord.js-commando 创建一个不和谐机器人,而我遇到的问题基本上是试图让我的 discord.js 机器人发布一个带有以下属性的嵌入。

运行机器人客户端并输入命令后,我的控制台中出现错误提示

(node:6120) UnhandledPromiseRejectionWarning: UnhandledPromiseRejection (rejection id: 3): TypeError: Cannot read property 'username' of undefined

以前,在我尝试将动态返回输出(来自answer())放入嵌入之前,该命令运行良好。该函数调用的return 值是一个基于lootedItem 的字符串。

lootedItem 是从父数组的加权随机选择中选择的对象。

const embed = new Discord.RichEmbed({
     "title": "Game Results",
     "color": 3662439,
     "description": answer(),
     "thumbnail": {
          "url": lootedItem.image
     },
})
message.channel.send('Placing...', {
     embed: embed.setTimestamp()
});

【问题讨论】:

  • 你在某些时候试图获取Cannot read property 'username' of undefined的用户名。

标签: javascript discord discord.js


【解决方案1】:

您错过了一个字段,它需要某些字段才能工作。

const embed = new Discord.RichEmbed({
      "username": "Username",    // you missed this and it is required to work
      "title": "Game Results",
      "color": 3662439,
      "description": answer(),
      "thumbnail": {
          "url": lootedItem.image
    },
})

【讨论】:

    猜你喜欢
    • 2011-05-26
    • 2022-01-08
    • 2015-07-05
    • 2010-09-08
    • 1970-01-01
    相关资源
    最近更新 更多