简体   繁体   English

嵌入斜线命令中的随机颜色 (Discord.js)

[英]Random Color in Embed Slash Command (Discord.js)

I want my bot to put a random color for my embed on a slash command.我希望我的机器人在斜杠命令中为我的嵌入添加随机颜色。 I've tried using RANDOM but that doesn't work.我试过使用RANDOM但这不起作用。 Here's my code:这是我的代码:

//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 ]
    }
  }
})

If anyone knows how to do this then please let me know, Thanks!如果有人知道如何做到这一点,请告诉我,谢谢!

You could use this little piece of code to generate a random hex code that will be used as the embed color:您可以使用这段代码生成一个随机的十六进制代码,用作嵌入颜色:

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

I found out a different way which worked a lot better:我发现了一种更好的方法:

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)
  }
})

I would also have:我也会有:

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 };
}

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

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