I’m trying to set a random color with Discord.js but it keeps throwing me errors. I have tried serveral methods on other posts as well, but it couldn’t fix the problem
The code:
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] });
}
});
The error
TypeError [COLOR_CONVERT]: Unable to convert color to a number.
at Function.resolveColor (D:san-botnode_modulesdiscord.jssrcutilUtil.js:487:41)
at MessageEmbed.setColor (D:san-botnode_modulesdiscord.jssrcstructuresMessageEmbed.js:402:23)
at Command.run (D:san-botsrccmdartwindflit.js:14:3)
at Client.<anonymous> (D:san-botsrcindex.js:57:10)
at Client.emit (node:events:390:28)
at MessageCreateAction.handle (D:san-botnode_modulesdiscord.jssrcclientactionsMessageCreate.js:26:14)
at Object.module.exports [as MESSAGE_CREATE] (D:san-botnode_modulesdiscord.jssrcclientwebsockethandlersMESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (D:san-botnode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:351:31)
at WebSocketShard.onPacket (D:san-botnode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:444:22)
at WebSocketShard.onMessage (D:san-botnode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:301:10) {
[Symbol(code)]: 'COLOR_CONVERT'
}
I also tried to use it with a randomizer function, didn’t work also
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])
}
2
Answers
Create function which generate random color and return it or do
.setColor('Random')
and for set color do
.setColor(getHex())
Maybe try using the EmbedBuilder (https://discordjs.guide/popular-topics/embeds.html). I tested your ranc function and it returns a random color, so the ranc function is prolly not the problem.
^This code above works fine
So try the EmbedBuilder