From 6da5a880c6b8817015d05f7908125c0b36246a7b Mon Sep 17 00:00:00 2001 From: KagChi Date: Fri, 16 Feb 2024 15:06:09 +0700 Subject: [PATCH] fix: multiple member roles --- packages/core/package.json | 2 +- packages/core/src/Structures/Client.ts | 4 +-- .../Interactions/BaseInteraction.ts | 2 +- packages/framework/package.json | 2 +- ...ught.sql => 0000_equal_scarlet_spider.sql} | 10 ++++--- .../drizzle/meta/0000_snapshot.json | 30 +++++++++++++------ .../kanao-schema/drizzle/meta/_journal.json | 4 +-- packages/kanao-schema/package.json | 2 +- packages/kanao-schema/src/Schema/roles.ts | 9 ++++-- .../GuildRoles/GuildRoleCreateListener.ts | 7 ++++- .../Caches/Guilds/GuildCreateListener.ts | 4 +-- 11 files changed, 49 insertions(+), 27 deletions(-) rename packages/kanao-schema/drizzle/{0000_yellow_onslaught.sql => 0000_equal_scarlet_spider.sql} (94%) diff --git a/packages/core/package.json b/packages/core/package.json index a4f58354..f95b0f2b 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@nezuchan/core", - "version": "0.8.1", + "version": "0.8.2", "description": "A Core Low Level API for creating Discord bots using @nezuchan/nezu-gateway.", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", diff --git a/packages/core/src/Structures/Client.ts b/packages/core/src/Structures/Client.ts index 8c2af152..48e900cc 100644 --- a/packages/core/src/Structures/Client.ts +++ b/packages/core/src/Structures/Client.ts @@ -355,8 +355,8 @@ export class Client extends EventEmitter { public async resolveRole({ id, guildId }: { id: string; guildId: string; }): Promise { const { role } = await this.drizzle.select({ role: schema.roles }).from(schema.memberRoles) - .where(and(eq(schema.memberRoles.id, id), eq(schema.memberRoles.guildId, guildId))) - .leftJoin(schema.roles, and(eq(schema.memberRoles.id, id), eq(schema.memberRoles.guildId, guildId))) + .where(and(eq(schema.memberRoles.memberId, id), eq(schema.memberRoles.guildId, guildId))) + .leftJoin(schema.roles, eq(schema.roles.id, id)) .then(x => x[0]); if (role) { diff --git a/packages/core/src/Structures/Interactions/BaseInteraction.ts b/packages/core/src/Structures/Interactions/BaseInteraction.ts index 7833ea6e..65b53dbb 100644 --- a/packages/core/src/Structures/Interactions/BaseInteraction.ts +++ b/packages/core/src/Structures/Interactions/BaseInteraction.ts @@ -55,7 +55,7 @@ export class BaseInteraction extends Base public get member(): GuildMember | null { return this.data.member ? new GuildMember({ - id: this.data.user!.id, + id: this.data.user?.id ?? this.data.member.user.id, guildId: this.guildId, nick: this.data.member.nick ?? null, avatar: this.data.member.avatar ?? null, diff --git a/packages/framework/package.json b/packages/framework/package.json index 1a8ce2d4..0de9d3c4 100644 --- a/packages/framework/package.json +++ b/packages/framework/package.json @@ -1,6 +1,6 @@ { "name": "@nezuchan/framework", - "version": "0.8.1", + "version": "0.8.2", "description": "A Commands framework for @nezuchan/nezu-gateway.", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/packages/kanao-schema/drizzle/0000_yellow_onslaught.sql b/packages/kanao-schema/drizzle/0000_equal_scarlet_spider.sql similarity index 94% rename from packages/kanao-schema/drizzle/0000_yellow_onslaught.sql rename to packages/kanao-schema/drizzle/0000_equal_scarlet_spider.sql index b59fd4a4..02349715 100644 --- a/packages/kanao-schema/drizzle/0000_yellow_onslaught.sql +++ b/packages/kanao-schema/drizzle/0000_equal_scarlet_spider.sql @@ -30,12 +30,14 @@ CREATE TABLE IF NOT EXISTS "users" ( ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "guild_roles" ( - "id" text PRIMARY KEY NOT NULL, + "id" serial PRIMARY KEY NOT NULL, + "guild_id" text, "role_id" text ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "member_roles" ( - "id" text PRIMARY KEY NOT NULL, + "id" serial PRIMARY KEY NOT NULL, + "member_id" text, "role_id" text, "guild_id" text ); @@ -183,7 +185,7 @@ EXCEPTION END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "guild_roles" ADD CONSTRAINT "guild_roles_id_guilds_id_fk" FOREIGN KEY ("id") REFERENCES "guilds"("id") ON DELETE cascade ON UPDATE no action; + ALTER TABLE "guild_roles" ADD CONSTRAINT "guild_roles_guild_id_guilds_id_fk" FOREIGN KEY ("guild_id") REFERENCES "guilds"("id") ON DELETE cascade ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; @@ -195,7 +197,7 @@ EXCEPTION END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "member_roles" ADD CONSTRAINT "member_roles_id_members_id_fk" FOREIGN KEY ("id") REFERENCES "members"("id") ON DELETE cascade ON UPDATE no action; + ALTER TABLE "member_roles" ADD CONSTRAINT "member_roles_member_id_members_id_fk" FOREIGN KEY ("member_id") REFERENCES "members"("id") ON DELETE cascade ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; diff --git a/packages/kanao-schema/drizzle/meta/0000_snapshot.json b/packages/kanao-schema/drizzle/meta/0000_snapshot.json index ff69969e..6e7b335a 100644 --- a/packages/kanao-schema/drizzle/meta/0000_snapshot.json +++ b/packages/kanao-schema/drizzle/meta/0000_snapshot.json @@ -1,5 +1,5 @@ { - "id": "e8226987-be7a-48c4-af4a-8f734f1d9dde", + "id": "79280f55-e67c-474c-8dcd-2c11bf33f8d4", "prevId": "00000000-0000-0000-0000-000000000000", "version": "5", "dialect": "pg", @@ -207,10 +207,16 @@ "columns": { "id": { "name": "id", - "type": "text", + "type": "serial", "primaryKey": true, "notNull": true }, + "guild_id": { + "name": "guild_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, "role_id": { "name": "role_id", "type": "text", @@ -220,12 +226,12 @@ }, "indexes": {}, "foreignKeys": { - "guild_roles_id_guilds_id_fk": { - "name": "guild_roles_id_guilds_id_fk", + "guild_roles_guild_id_guilds_id_fk": { + "name": "guild_roles_guild_id_guilds_id_fk", "tableFrom": "guild_roles", "tableTo": "guilds", "columnsFrom": [ - "id" + "guild_id" ], "columnsTo": [ "id" @@ -256,10 +262,16 @@ "columns": { "id": { "name": "id", - "type": "text", + "type": "serial", "primaryKey": true, "notNull": true }, + "member_id": { + "name": "member_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, "role_id": { "name": "role_id", "type": "text", @@ -275,12 +287,12 @@ }, "indexes": {}, "foreignKeys": { - "member_roles_id_members_id_fk": { - "name": "member_roles_id_members_id_fk", + "member_roles_member_id_members_id_fk": { + "name": "member_roles_member_id_members_id_fk", "tableFrom": "member_roles", "tableTo": "members", "columnsFrom": [ - "id" + "member_id" ], "columnsTo": [ "id" diff --git a/packages/kanao-schema/drizzle/meta/_journal.json b/packages/kanao-schema/drizzle/meta/_journal.json index 5d454444..4df72f74 100644 --- a/packages/kanao-schema/drizzle/meta/_journal.json +++ b/packages/kanao-schema/drizzle/meta/_journal.json @@ -5,8 +5,8 @@ { "idx": 0, "version": "5", - "when": 1707999148094, - "tag": "0000_yellow_onslaught", + "when": 1708070649338, + "tag": "0000_equal_scarlet_spider", "breakpoints": true } ] diff --git a/packages/kanao-schema/package.json b/packages/kanao-schema/package.json index 194a57e6..b992a2f9 100644 --- a/packages/kanao-schema/package.json +++ b/packages/kanao-schema/package.json @@ -1,6 +1,6 @@ { "name": "@nezuchan/kanao-schema", - "version": "0.1.0", + "version": "0.2.0", "description": "Cache schema for Kanao Gateway", "main": "./dist/index.js", "module": "./dist/index.js", diff --git a/packages/kanao-schema/src/Schema/roles.ts b/packages/kanao-schema/src/Schema/roles.ts index 8eae7415..b983b88e 100644 --- a/packages/kanao-schema/src/Schema/roles.ts +++ b/packages/kanao-schema/src/Schema/roles.ts @@ -1,5 +1,5 @@ import { relations } from "drizzle-orm"; -import { boolean, integer, pgTable, text } from "drizzle-orm/pg-core"; +import { boolean, integer, pgTable, serial, text } from "drizzle-orm/pg-core"; import { guilds } from "./guild.js"; import { members } from "./member.js"; @@ -13,13 +13,16 @@ export const roles = pgTable("roles", { }); export const memberRoles = pgTable("member_roles", { - id: text("id").primaryKey().references(() => members.id, { onDelete: "cascade" }), + id: serial("id").primaryKey(), + memberId: text("member_id").references(() => members.id, { onDelete: "cascade" }), roleId: text("role_id").references(() => roles.id, { onDelete: "cascade" }), guildId: text("guild_id").references(() => guilds.id, { onDelete: "cascade" }) }); export const guildsRoles = pgTable("guild_roles", { - id: text("id").primaryKey().references(() => guilds.id, { onDelete: "cascade" }), + id: serial("id").primaryKey(), + + guildId: text("guild_id").references(() => guilds.id, { onDelete: "cascade" }), roleId: text("role_id").references(() => roles.id, { onDelete: "cascade" }) }); diff --git a/services/kanao-gateway/src/Listeners/Caches/GuildRoles/GuildRoleCreateListener.ts b/services/kanao-gateway/src/Listeners/Caches/GuildRoles/GuildRoleCreateListener.ts index 8a3e84a3..d1d7b5bf 100644 --- a/services/kanao-gateway/src/Listeners/Caches/GuildRoles/GuildRoleCreateListener.ts +++ b/services/kanao-gateway/src/Listeners/Caches/GuildRoles/GuildRoleCreateListener.ts @@ -1,6 +1,6 @@ import { Buffer } from "node:buffer"; import { RabbitMQ } from "@nezuchan/constants"; -import { roles } from "@nezuchan/kanao-schema"; +import { guildsRoles, roles } from "@nezuchan/kanao-schema"; import { RoutingKey } from "@nezuchan/utilities"; import type { GatewayGuildRoleCreateDispatch } from "discord-api-types/v10"; import { GatewayDispatchEvents } from "discord-api-types/v10"; @@ -34,6 +34,11 @@ export class GuildRoleCreateListener extends Listener { hoist: payload.data.d.role.hoist } }); + + await this.store.drizzle.insert(guildsRoles).values({ + roleId: payload.data.d.role.id, + guildId: payload.data.d.guild_id + }).onConflictDoNothing({ target: guildsRoles.id }); } await this.store.amqp.publish(RabbitMQ.GATEWAY_QUEUE_SEND, RoutingKey(clientId, payload.shardId), Buffer.from(JSON.stringify(payload.data))); } diff --git a/services/kanao-gateway/src/Listeners/Caches/Guilds/GuildCreateListener.ts b/services/kanao-gateway/src/Listeners/Caches/Guilds/GuildCreateListener.ts index b57cef05..9450ca39 100644 --- a/services/kanao-gateway/src/Listeners/Caches/Guilds/GuildCreateListener.ts +++ b/services/kanao-gateway/src/Listeners/Caches/Guilds/GuildCreateListener.ts @@ -121,7 +121,7 @@ export class GuildCreateListener extends Listener { await this.store.drizzle.insert(guildsRoles).values({ roleId: role.id, - id: payload.data.d.id + guildId: payload.data.d.id }).onConflictDoNothing({ target: guildsRoles.id }); } } @@ -183,7 +183,7 @@ export class GuildCreateListener extends Listener { for (const role of member.roles) { await this.store.drizzle.insert(memberRoles).values({ - id: member.user.id, + memberId: member.user.id, roleId: role, guildId: payload.data.d.id }).onConflictDoNothing({ target: memberRoles.id });