ColorResolvable being quirky

Hi yall I want to set e color for my embed, so I gave it a string in the format of "#abcxyz" and it said "me no like that". Then I checked the reference and that should have worked. Then I tried an array of three number with rgb format and then it said again "me no like that". What does it take? And why does the reference give an array of numbers as example, yet number[] is not in the union type?
20 Replies
d.js toolkit
d.js toolkit3w ago
Mafia
Mafia3w ago
could you share your code, error, and discord.js version
Definitely Not A Dolphin
oh sorry 14.22.1
const avgColor = await getAverageColor(iconURL);
// avgColor.value = [red, green, blue, a]

const trackEmbed = new EmbedBuilder()
.setTitle(thing.name)
.setURL(thing.url)
.setAuthor({
name: "Currently playing",
iconURL: iconURL,
})
.setThumbnail(thing.image)
.setDescription(`${thing.artist} in ${thing.album}`)
.setColor(avgColor.value?.slice(0, 2)); // this doesnt work
const avgColor = await getAverageColor(iconURL);
// avgColor.value = [red, green, blue, a]

const trackEmbed = new EmbedBuilder()
.setTitle(thing.name)
.setURL(thing.url)
.setAuthor({
name: "Currently playing",
iconURL: iconURL,
})
.setThumbnail(thing.image)
.setDescription(`${thing.artist} in ${thing.album}`)
.setColor(avgColor.value?.slice(0, 2)); // this doesnt work
Error
Argument of type 'number[]' is not assignable to parameter of type 'ColorResolvable | null'.
Type 'number[]' is not assignable to type 'readonly [red: number, green: number, blue: number]'.
Target requires 3 element(s) but source may have fewer. (deno-ts 2345)
Argument of type 'number[]' is not assignable to parameter of type 'ColorResolvable | null'.
Type 'number[]' is not assignable to type 'readonly [red: number, green: number, blue: number]'.
Target requires 3 element(s) but source may have fewer. (deno-ts 2345)
Mafia
Mafia3w ago
I would start by updating your version to latest also could you log avgColor.value?.slice(0, 2)
Definitely Not A Dolphin
[ 103, 73, 65 ]
[ 103, 73, 65 ]
an example value
Mafia
Mafia3w ago
ok I think I figured it out it is just a typing issue/bug this works
const color: [red: number, green: number, blue: number] = [255,0,255]
const embed = new EmbedBuilder().setColor(color)
const color: [red: number, green: number, blue: number] = [255,0,255]
const embed = new EmbedBuilder().setColor(color)
but this does not
const color = [255,0,255]
const embed = new EmbedBuilder().setColor(color)
const color = [255,0,255]
const embed = new EmbedBuilder().setColor(color)
is avgColor a custom object you created?
Definitely Not A Dolphin
hmmm I think that is the culprit then but then its weird that the documentation says it does
Mafia
Mafia3w ago
I did not have any issue us the string
Mafia
Mafia3w ago
JS does not care about type semantics ts does tho
Definitely Not A Dolphin
but TS does yeah I think its because there is only a conversion from string to ColorResolvable of the string starts with a # and contains onlu 6 symbols
Mafia
Mafia3w ago
you could do a // @ts-expect-error before setcolor but my recomendations is to add a fuctions that returns the type [red: number, green: number, blue: number] or other resolvable
Definitely Not A Dolphin
ill have to make a conversion since the function is from a library but sure thanks !solved
d.js docs
d.js docs3w ago
:function: resolveColor discord.js@14.24.2 Resolves a ColorResolvable into a color number.
Mafia
Mafia3w ago
I think you will find this useful maybe it will return the number for a color from ColorResolvable. not sure it solves the underling issue tho
Definitely Not A Dolphin
question how can a number a color? isnt a color a point in R^3 aka a three dimensional number?
Mafia
Mafia3w ago
0xffffff is equal to 16,777,215 there for #FFFFFF or [255,255,255] is also equal to 16,777,215 Discord API takes a number as input the builder just abstracts it for you
Definitely Not A Dolphin
okay but thats still three numbers
Mafia
Mafia3w ago
In an abstract sense, sure. But it's been collapsed down into a single number Also another solution is to type guard function
function isColorTuple(color:number[]): color is [red: number, green: number, blue: number] {
return color.length === 3
}
function isColorTuple(color:number[]): color is [red: number, green: number, blue: number] {
return color.length === 3
}
Inky
Inky3w ago
I prefer const color = […] satisfies ColorResolvable

Did you find this page helpful?