javascript 设置随机颜色嵌入| Discord.js

lhcgjxsq  于 2023-03-28  发布在  Java
关注(0)|答案(2)|浏览(161)

我试着用Discord.js设置一个随机的颜色,但它总是给我错误。我也在其他帖子上试过几种方法,但它不能解决这个问题
代码:

const Command = require("../structures/Command.js");
const config = require("../data/config.json");
const {MessageEmbed} = require('discord.js')

module.exports = new Command({
    name: "artlumen",
    description: "idk",

    async run(message, args, client) {
        const artlumen = new MessageEmbed()
                .setColor("RANDOM")
                .setTitle("Elite 0")
                .setImage('https://raw.githubusercontent.com/Aceship/Arknight-Images/main/characters/char_4042_lumen_1.png')
                .setTimestamp()
message.channel.send({ embeds: [artlumen] });
    }
});

错误

TypeError [COLOR_CONVERT]: Unable to convert color to a number.
    at Function.resolveColor (D:\0san-bot\node_modules\discord.js\src\util\Util.js:487:41)
    at MessageEmbed.setColor (D:\0san-bot\node_modules\discord.js\src\structures\MessageEmbed.js:402:23)
    at Command.run (D:\0san-bot\src\cmd\artwindflit.js:14:3)
    at Client.<anonymous> (D:\0san-bot\src\index.js:57:10)
    at Client.emit (node:events:390:28)
    at MessageCreateAction.handle (D:\0san-bot\node_modules\discord.js\src\client\actions\MessageCreate.js:26:14)
    at Object.module.exports [as MESSAGE_CREATE] (D:\0san-bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (D:\0san-bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31)
    at WebSocketShard.onPacket (D:\0san-bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (D:\0san-bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10) {
  [Symbol(code)]: 'COLOR_CONVERT'
}

我也试着用它与一个随机发生器功能,没有工作也

const Command = require("../structures/Command.js");
const config = require("../data/config.json");
const {MessageEmbed} = require('discord.js')

module.exports = new Command({
    name: "artlumen",
    description: "idk",

    async run(message, args, client) {
        const artlumen = new MessageEmbed()
                .setColor(ranc())
                .setTitle("Elite 0")
                .setImage('https://raw.githubusercontent.com/Aceship/Arknight-Images/main/characters/char_4042_lumen_1.png')
                .setTimestamp()
message.channel.send({ embeds: [artlumen] });
    }
});

function ranc() {
    var col = ["#000000", "#1ABC9C", "#11806A", "#57F287", "#1F8B4C", "#3498DB", "#206694", "#9B59B6", "#71368A", "#E91E63", "#AD1457", "#F1C40F", "#C27C0E", "#E67E22", "#A84300", "#ED4245", "#992D22", "#95A5A6", "#979C9F", "#7F8C8D", "#BCC0C0", "#34495E", "#2C3E50", "#FFFF00"]
    var rnd = Math.floor(Math.random() * col.length);
    return(col[rnd])
}
mi7gmzs6

mi7gmzs61#

创建一个生成随机颜色并返回的函数,或者执行.setColor('Random')

function getHex() {
  return '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padEnd(6, '0');
}

并且对于设置颜色do .setColor(getHex())

gt0wga4j

gt0wga4j2#

也许可以尝试使用EmbedBuilder(https://discordjs.guide/popular-topics/embeds.html)。我测试了你的Ranc函数,它返回一个随机颜色,所以Ranc函数可能不是问题所在。

const embed = new Discord.EmbedBuilder()
.setColor("#37AADF");
interaction.reply("Test Embed");
interaction.channel.send({embeds: [embed]});

^上面的代码工作正常
所以试试EmbedBuilder

相关问题