Colorful roles

This commit is contained in:
Rory& 2025-07-12 20:12:37 +02:00
parent 43046f401d
commit 528662446c
6 changed files with 57 additions and 1 deletions

Binary file not shown.

View File

@ -72,7 +72,7 @@ router.post(
throw DiscordApiErrors.MAXIMUM_ROLES.withParams(maxRoles);
const role = Role.create({
// values before ...body are default and can be overriden
// values before ...body are default and can be overridden
position: 1,
hoist: false,
color: 0,
@ -88,6 +88,11 @@ router.post(
icon: undefined,
unicode_emoji: undefined,
id: Snowflake.generate(),
colors: {
primary_color: body.colors?.primary_color || body.color || 0,
secondary_color: body.colors?.secondary_color || undefined, // gradient
tertiary_color: body.colors?.tertiary_color || undefined, // "holographic"
},
});
await Promise.all([

View File

@ -22,6 +22,20 @@ import { BaseClass } from "./BaseClass";
import { Guild } from "./Guild";
import { dbEngine } from "../util/Database";
export class RoleColors {
primary_color: number;
secondary_color: number | undefined; // only used for "holographic" and "gradient" styles
tertiary_color?: number | undefined; // only used for "holographic" style
toJSON(): RoleColors {
return {
...this,
secondary_color: this.secondary_color ?? undefined,
tertiary_color: this.tertiary_color ?? undefined,
};
}
}
@Entity({
name: "roles",
engine: dbEngine,
@ -74,6 +88,9 @@ export class Role extends BaseClass {
@Column({ default: 0 })
flags: number;
@Column({ nullable: false, type: "simple-json" })
colors: RoleColors;
toJSON(): Role {
return {
...this,

View File

@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class RoleColors1752321571508 implements MigrationInterface {
name = 'RoleColors1752321571508'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "roles" ADD "colors" text`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "roles" DROP COLUMN "colors"`);
}
}

View File

@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class RoleColorsSolidColor1752342900886 implements MigrationInterface {
name = 'RoleColorsSolidColor1752342900886'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`UPDATE "roles" SET "colors" = jsonb_build_object('primary_color', "color") WHERE "colors" IS NULL`);
await queryRunner.query(`ALTER TABLE "roles" ALTER COLUMN "colors" SET NOT NULL`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "roles" ALTER COLUMN "colors" DROP NOT NULL`);
}
}

View File

@ -25,4 +25,9 @@ export interface RoleModifySchema {
position?: number;
icon?: string;
unicode_emoji?: string;
colors?: {
primary_color: number;
secondary_color: number | null | undefined; // only used for "holographic" and "gradient" styles
tertiary_color?: number | null | undefined; // only used for "holographic" style
} | undefined;
}