【问题标题】:Random Color in Embed Slash Command (Discord.js)嵌入斜线命令中的随机颜色 (Discord.js)
【发布时间】:2021-07-22 02:06:01
【问题描述】:

我希望我的机器人在斜杠命令中为我的嵌入添加随机颜色。我试过使用RANDOM 但这不起作用。这是我的代码:

//I would usually do: color: 0x[hex code]
const randomcolorembed = {
  color: `RANDOM`, //This doesn't work
  description: `blah blah blah...`
};

bot.api.interactions(interaction.id, interaction.token).callback.post({
  data: {
    type: 4,
    data: {
      embeds: [ randomcolorembed ]
    }
  }
})

如果有人知道怎么做,请告诉我,谢谢!

【问题讨论】:

  • #api 是出于私人原因。不会提供对此的支持。

标签: javascript node.js discord discord.js


【解决方案1】:

我发现了另一种效果更好的方法:

const randomcolorembed = new Discord.MessageEmbed()
.setColor(`RANDOM`)
.setDescription(`blah blah blah...`)

bot.api.interactions(interaction.id, interaction.token).callback.post({
  data: {
    type: 4,
    data: await createAPIMessage(interaction, embed)
  }
})

我也会有:

async function createAPIMessage(interaction, content) {
    const apiMessage = await Discord.APIMessage.create(bot.channels.resolve(interaction.channel_id), content)
        .resolveData()
        .resolveFiles();
    
    return { ...apiMessage.data, files: apiMessage.files };
}

【讨论】:

    【解决方案2】:

    您可以使用这段代码生成一个随机的十六进制代码,用作嵌入颜色:

    const randomcolorembed = {
      color: '#'+(Math.random()*0xFFFFFF<<0).toString(16),
      description: `blah blah blah...`
    };
    
    

    【讨论】:

    • 它需要像color: 0x[hex code]。
    • 那就改成'0x'+(Math.random()*0xFFFFFF&lt;&lt;0).toString(16),
    • 我已经尝试过了,但我收到一个错误提示它不是整数
    猜你喜欢
    • 2019-08-21
    • 2021-06-11
    • 2021-02-05
    • 2022-01-25
    • 2021-11-17
    • 2021-08-17
    • 1970-01-01
    • 2021-05-21
    • 2021-11-03
    相关资源
    最近更新 更多