Add a colour override for embed colours
All checks were successful
Test / build (push) Successful in 9s

- Using a new optional  value in the JSON, if set overrides the default one set by the rarity
- Added a failsafe if the override colour is invalid to default and log a warning

#349
This commit is contained in:
Ethan Lane 2024-08-29 19:21:53 +01:00
parent a10eaf7d75
commit 5deb6fcc14
3 changed files with 30 additions and 1 deletions

View file

@ -12,6 +12,7 @@ export interface CardMetadata {
type: CardRarity, type: CardRarity,
path: string, path: string,
subseries?: string, subseries?: string,
colour?: string,
} }
export interface DropResult { export interface DropResult {

View file

@ -5,6 +5,7 @@ import { DropResult } from "../contracts/SeriesMetadata";
import { CoreClient } from "../client/client"; import { CoreClient } from "../client/client";
import AppLogger from "../client/appLogger"; import AppLogger from "../client/appLogger";
import CardConstants from "../constants/CardConstants"; import CardConstants from "../constants/CardConstants";
import StringTools from "./StringTools";
export default class CardDropHelperMetadata { export default class CardDropHelperMetadata {
public static GetRandomCard(): DropResult | undefined { public static GetRandomCard(): DropResult | undefined {
@ -82,12 +83,25 @@ export default class CardDropHelperMetadata {
AppLogger.LogSilly("CardDropHelperMetadata/GenerateDropEmbed", `Parameters: drop=${drop.card.id}, quantityClaimed=${quantityClaimed}, imageFileName=${imageFileName}`); AppLogger.LogSilly("CardDropHelperMetadata/GenerateDropEmbed", `Parameters: drop=${drop.card.id}, quantityClaimed=${quantityClaimed}, imageFileName=${imageFileName}`);
const description = drop.card.subseries ?? drop.series.name; const description = drop.card.subseries ?? drop.series.name;
let colour = CardRarityToColour(drop.card.type);
if (drop.card.colour && StringTools.IsHexCode(drop.card.colour)) {
const hexCode = Number("0x" + drop.card.colour);
if (hexCode) {
colour = hexCode;
} else {
AppLogger.LogWarn("CardDropHelperMetadata/GenerateDropEmbed", `Card's colour override is invalid: ${drop.card.id}, ${drop.card.colour}`);
}
} else if (drop.card.colour) {
AppLogger.LogWarn("CardDropHelperMetadata/GenerateDropEmbed", `Card's colour override is invalid: ${drop.card.id}, ${drop.card.colour}`);
}
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setTitle(drop.card.name) .setTitle(drop.card.name)
.setDescription(description) .setDescription(description)
.setFooter({ text: `${CardRarityToString(drop.card.type)} · ${drop.card.id}` }) .setFooter({ text: `${CardRarityToString(drop.card.type)} · ${drop.card.id}` })
.setColor(CardRarityToColour(drop.card.type)) .setColor(colour)
.setImage(`attachment://${imageFileName}`) .setImage(`attachment://${imageFileName}`)
.addFields([ .addFields([
{ {

View file

@ -39,4 +39,18 @@ export default class StringTools {
public static ReplaceAll(str: string, find: string, replace: string) { public static ReplaceAll(str: string, find: string, replace: string) {
return str.replace(new RegExp(find, "g"), replace); return str.replace(new RegExp(find, "g"), replace);
} }
public static IsHexCode(str: string): boolean {
if (str.length != 6) return false;
const characters = "0123456789abcdefABCDEF";
for (let i = 0; i < 6; i++) {
const char = str[i];
if (!characters.includes(char)) return false;
}
return true;
}
} }