Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
fix: unique member key
Browse files Browse the repository at this point in the history
  • Loading branch information
KagChi committed Mar 2, 2024
1 parent 9a117db commit 98de0db
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/Structures/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Client } from "./Client.js";

export class Base<RawType> {
public constructor(
protected readonly data: RawType & { id?: Snowflake; },
protected readonly data: RawType & { id?: Snowflake | null; },
public client: Client
) {}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Structures/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Structures/GuildMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { VoiceState } from "./VoiceState.js";

export class GuildMember extends Base<InferSelectModel<typeof members>> {
public get id(): string {
return this.data.id;
return this.data.id as unknown as string;
}

public get guildId(): string | null {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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" (
Expand All @@ -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" (
Expand Down
20 changes: 14 additions & 6 deletions packages/kanao-schema/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -77,7 +77,15 @@
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"compositePrimaryKeys": {
"members_id_guild_id": {
"name": "members_id_guild_id",
"columns": [
"id",
"guild_id"
]
}
},
"uniqueConstraints": {}
},
"users": {
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions packages/kanao-schema/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"idx": 0,
"version": "5",
"when": 1709349389002,
"tag": "0000_last_omega_sentinel",
"when": 1709350996171,
"tag": "0000_grey_mach_iv",
"breakpoints": true
}
]
Expand Down
2 changes: 1 addition & 1 deletion packages/kanao-schema/src/Schema/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ({
Expand Down

0 comments on commit 98de0db

Please sign in to comment.