From 98de0dbd071d6fcdcd49793c34ba06145eb8ecad Mon Sep 17 00:00:00 2001 From: KagChi Date: Sat, 2 Mar 2024 10:43:39 +0700 Subject: [PATCH] fix: unique member key --- packages/core/src/Structures/Base.ts | 2 +- packages/core/src/Structures/Client.ts | 2 +- packages/core/src/Structures/GuildMember.ts | 2 +- ...ega_sentinel.sql => 0000_grey_mach_iv.sql} | 9 +++++---- .../drizzle/meta/0000_snapshot.json | 20 +++++++++++++------ .../kanao-schema/drizzle/meta/_journal.json | 4 ++-- packages/kanao-schema/src/Schema/roles.ts | 2 +- .../GuildMembers/GuildMemberAddListener.ts | 2 +- .../GuildMembers/GuildMemberUpdateListener.ts | 2 +- .../GuildMembers/GuildMembersChunkListener.ts | 2 +- .../Caches/Guilds/GuildCreateListener.ts | 2 +- .../Caches/Messages/MessageCreateListener.ts | 2 +- .../Caches/Voices/VoiceStateUpdateListener.ts | 2 +- 13 files changed, 31 insertions(+), 22 deletions(-) rename packages/kanao-schema/drizzle/{0000_last_omega_sentinel.sql => 0000_grey_mach_iv.sql} (94%) diff --git a/packages/core/src/Structures/Base.ts b/packages/core/src/Structures/Base.ts index b42ed8b1..6a5b9447 100644 --- a/packages/core/src/Structures/Base.ts +++ b/packages/core/src/Structures/Base.ts @@ -3,7 +3,7 @@ import type { Client } from "./Client.js"; export class Base { public constructor( - protected readonly data: RawType & { id?: Snowflake; }, + protected readonly data: RawType & { id?: Snowflake | null; }, public client: Client ) {} diff --git a/packages/core/src/Structures/Client.ts b/packages/core/src/Structures/Client.ts index 87966498..accbdf76 100644 --- a/packages/core/src/Structures/Client.ts +++ b/packages/core/src/Structures/Client.ts @@ -118,7 +118,7 @@ export class Client extends EventEmitter { pending: member.pending, premiumSince: member.premium_since }).onConflictDoUpdate({ - target: schema.members.id, + target: [schema.members.id, schema.members.guildId], set: { avatar: sql`EXCLUDED.avatar`, flags: sql`EXCLUDED.flags`, diff --git a/packages/core/src/Structures/GuildMember.ts b/packages/core/src/Structures/GuildMember.ts index 55dd8dbc..f0ece1f5 100644 --- a/packages/core/src/Structures/GuildMember.ts +++ b/packages/core/src/Structures/GuildMember.ts @@ -10,7 +10,7 @@ import type { VoiceState } from "./VoiceState.js"; export class GuildMember extends Base> { public get id(): string { - return this.data.id; + return this.data.id as unknown as string; } public get guildId(): string | null { diff --git a/packages/kanao-schema/drizzle/0000_last_omega_sentinel.sql b/packages/kanao-schema/drizzle/0000_grey_mach_iv.sql similarity index 94% rename from packages/kanao-schema/drizzle/0000_last_omega_sentinel.sql rename to packages/kanao-schema/drizzle/0000_grey_mach_iv.sql index 394212cb..2d51aa46 100644 --- a/packages/kanao-schema/drizzle/0000_last_omega_sentinel.sql +++ b/packages/kanao-schema/drizzle/0000_grey_mach_iv.sql @@ -1,6 +1,6 @@ CREATE TABLE IF NOT EXISTS "members" ( - "id" text NOT NULL, - "guild_id" text NOT NULL, + "id" text, + "guild_id" text, "nick" text, "avatar" text, "flags" integer, @@ -9,7 +9,8 @@ CREATE TABLE IF NOT EXISTS "members" ( "deaf" boolean, "mute" boolean, "pending" boolean, - "communication_disabled_until" text + "communication_disabled_until" text, + CONSTRAINT "members_id_guild_id" PRIMARY KEY("id","guild_id") ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "users" ( @@ -33,7 +34,7 @@ CREATE TABLE IF NOT EXISTS "member_roles" ( "member_id" text NOT NULL, "role_id" text NOT NULL, "guild_id" text NOT NULL, - CONSTRAINT "voice_states_member_id_role_id_guild_id" PRIMARY KEY("member_id","role_id") + CONSTRAINT "voice_states_member_id_role_id" PRIMARY KEY("member_id","role_id") ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "roles" ( diff --git a/packages/kanao-schema/drizzle/meta/0000_snapshot.json b/packages/kanao-schema/drizzle/meta/0000_snapshot.json index 82509332..a9719e18 100644 --- a/packages/kanao-schema/drizzle/meta/0000_snapshot.json +++ b/packages/kanao-schema/drizzle/meta/0000_snapshot.json @@ -1,5 +1,5 @@ { - "id": "4882b47c-3c42-49a4-bdba-74babb63547b", + "id": "f7401341-0beb-4c78-a112-bf04a92a185f", "prevId": "00000000-0000-0000-0000-000000000000", "version": "5", "dialect": "pg", @@ -12,13 +12,13 @@ "name": "id", "type": "text", "primaryKey": false, - "notNull": true + "notNull": false }, "guild_id": { "name": "guild_id", "type": "text", "primaryKey": false, - "notNull": true + "notNull": false }, "nick": { "name": "nick", @@ -77,7 +77,15 @@ }, "indexes": {}, "foreignKeys": {}, - "compositePrimaryKeys": {}, + "compositePrimaryKeys": { + "members_id_guild_id": { + "name": "members_id_guild_id", + "columns": [ + "id", + "guild_id" + ] + } + }, "uniqueConstraints": {} }, "users": { @@ -200,8 +208,8 @@ "indexes": {}, "foreignKeys": {}, "compositePrimaryKeys": { - "voice_states_member_id_role_id_guild_id": { - "name": "voice_states_member_id_role_id_guild_id", + "voice_states_member_id_role_id": { + "name": "voice_states_member_id_role_id", "columns": [ "member_id", "role_id" diff --git a/packages/kanao-schema/drizzle/meta/_journal.json b/packages/kanao-schema/drizzle/meta/_journal.json index 1afcdafe..15f27536 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": 1709349389002, - "tag": "0000_last_omega_sentinel", + "when": 1709350996171, + "tag": "0000_grey_mach_iv", "breakpoints": true } ] diff --git a/packages/kanao-schema/src/Schema/roles.ts b/packages/kanao-schema/src/Schema/roles.ts index 68aa52f7..3a8beed0 100644 --- a/packages/kanao-schema/src/Schema/roles.ts +++ b/packages/kanao-schema/src/Schema/roles.ts @@ -16,7 +16,7 @@ export const memberRoles = pgTable("member_roles", { roleId: text("role_id").notNull(), guildId: text("guild_id").notNull() }, table => ({ - pkWithCustomName: primaryKey({ name: "voice_states_member_id_role_id_guild_id", columns: [table.memberId, table.roleId] }) + pkWithCustomName: primaryKey({ name: "voice_states_member_id_role_id", columns: [table.memberId, table.roleId] }) })); export const memberRolesRelations = relations(memberRoles, ({ one }) => ({ diff --git a/services/kanao-cache/src/Listeners/Caches/GuildMembers/GuildMemberAddListener.ts b/services/kanao-cache/src/Listeners/Caches/GuildMembers/GuildMemberAddListener.ts index a2be6921..b5f91d29 100644 --- a/services/kanao-cache/src/Listeners/Caches/GuildMembers/GuildMemberAddListener.ts +++ b/services/kanao-cache/src/Listeners/Caches/GuildMembers/GuildMemberAddListener.ts @@ -54,7 +54,7 @@ export class GuildMemberAddListener extends Listener { pending: payload.data.d.pending, premiumSince: payload.data.d.premium_since }).onConflictDoUpdate({ - target: members.id, + target: [members.id, members.guildId], set: { avatar: sql`EXCLUDED.avatar`, flags: sql`EXCLUDED.flags`, diff --git a/services/kanao-cache/src/Listeners/Caches/GuildMembers/GuildMemberUpdateListener.ts b/services/kanao-cache/src/Listeners/Caches/GuildMembers/GuildMemberUpdateListener.ts index ba8230dc..fc480cdf 100644 --- a/services/kanao-cache/src/Listeners/Caches/GuildMembers/GuildMemberUpdateListener.ts +++ b/services/kanao-cache/src/Listeners/Caches/GuildMembers/GuildMemberUpdateListener.ts @@ -54,7 +54,7 @@ export class GuildMemberUpdateListener extends Listener { pending: payload.data.d.pending, premiumSince: payload.data.d.premium_since }).onConflictDoUpdate({ - target: members.id, + target: [members.id, members.guildId], set: { avatar: sql`EXCLUDED.avatar`, flags: sql`EXCLUDED.flags`, diff --git a/services/kanao-cache/src/Listeners/Caches/GuildMembers/GuildMembersChunkListener.ts b/services/kanao-cache/src/Listeners/Caches/GuildMembers/GuildMembersChunkListener.ts index 8ffa0574..d36ab26b 100644 --- a/services/kanao-cache/src/Listeners/Caches/GuildMembers/GuildMembersChunkListener.ts +++ b/services/kanao-cache/src/Listeners/Caches/GuildMembers/GuildMembersChunkListener.ts @@ -66,7 +66,7 @@ export class GuildMembersChunkListener extends Listener { guildId: payload.data.d.guild_id })) ).onConflictDoUpdate({ - target: members.id, + target: [members.id, members.guildId], set: { avatar: sql`EXCLUDED.avatar`, communicationDisabledUntil: sql`EXCLUDED.premium_since`, diff --git a/services/kanao-cache/src/Listeners/Caches/Guilds/GuildCreateListener.ts b/services/kanao-cache/src/Listeners/Caches/Guilds/GuildCreateListener.ts index 5e87f495..bd59fb52 100644 --- a/services/kanao-cache/src/Listeners/Caches/Guilds/GuildCreateListener.ts +++ b/services/kanao-cache/src/Listeners/Caches/Guilds/GuildCreateListener.ts @@ -173,7 +173,7 @@ export class GuildCreateListener extends Listener { joinedAt: bot.joined_at, premiumSince: bot.premium_since }).onConflictDoUpdate({ - target: members.id, + target: [members.id, members.guildId], set: { avatar: sql`EXCLUDED.avatar`, flags: sql`EXCLUDED.flags`, diff --git a/services/kanao-cache/src/Listeners/Caches/Messages/MessageCreateListener.ts b/services/kanao-cache/src/Listeners/Caches/Messages/MessageCreateListener.ts index 0a17e0f5..9643b5f5 100644 --- a/services/kanao-cache/src/Listeners/Caches/Messages/MessageCreateListener.ts +++ b/services/kanao-cache/src/Listeners/Caches/Messages/MessageCreateListener.ts @@ -65,7 +65,7 @@ export class MessageCreateListener extends Listener { pending: payload.data.d.member.pending, premiumSince: payload.data.d.member.premium_since }).onConflictDoUpdate({ - target: members.id, + target: [members.id, members.guildId], set: { avatar: sql`EXCLUDED.avatar`, flags: sql`EXCLUDED.flags`, diff --git a/services/kanao-cache/src/Listeners/Caches/Voices/VoiceStateUpdateListener.ts b/services/kanao-cache/src/Listeners/Caches/Voices/VoiceStateUpdateListener.ts index fc9c94ce..5a61cb78 100644 --- a/services/kanao-cache/src/Listeners/Caches/Voices/VoiceStateUpdateListener.ts +++ b/services/kanao-cache/src/Listeners/Caches/Voices/VoiceStateUpdateListener.ts @@ -47,7 +47,7 @@ export class VoiceStateUpdateListener extends Listener { nick: payload.data.d.member.nick, pending: payload.data.d.member.pending, premiumSince: payload.data.d.member.premium_since - }).onConflictDoNothing({ target: members.id }); + }).onConflictDoNothing({ target: [members.id, members.guildId] }); if (payload.data.d.member.roles.length > 0) { await this.container.client.drizzle.insert(memberRoles).values(payload.data.d.member.roles.map(role => ({