From f86fbfdd5d5eee47cd7a42cff4f448f391e18ae2 Mon Sep 17 00:00:00 2001 From: KagChi Date: Sun, 18 Feb 2024 12:51:32 +0700 Subject: [PATCH] fix: properly update user --- .../Caches/Users/UserUpdateListener.ts | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/services/kanao-gateway/src/Listeners/Caches/Users/UserUpdateListener.ts b/services/kanao-gateway/src/Listeners/Caches/Users/UserUpdateListener.ts index fef811da..65ff434f 100644 --- a/services/kanao-gateway/src/Listeners/Caches/Users/UserUpdateListener.ts +++ b/services/kanao-gateway/src/Listeners/Caches/Users/UserUpdateListener.ts @@ -4,7 +4,6 @@ import { users } from "@nezuchan/kanao-schema"; import { RoutingKey } from "@nezuchan/utilities"; import type { GatewayUserUpdateDispatch } from "discord-api-types/v10"; import { GatewayDispatchEvents } from "discord-api-types/v10"; -import { eq } from "drizzle-orm"; import type { ListenerContext } from "../../../Stores/Listener.js"; import { Listener } from "../../../Stores/Listener.js"; import { clientId } from "../../../config.js"; @@ -17,22 +16,39 @@ export class UserUpdateListener extends Listener { } public async run(payload: { data: GatewayUserUpdateDispatch; shardId: number; }): Promise { - await this.store.drizzle.update(users).set({ + await this.store.drizzle.insert(users).values({ + id: payload.data.d.id, username: payload.data.d.username, - discriminator: payload.data.d.discriminator, - globalName: payload.data.d.global_name, + discriminator: payload.data.d.discriminator ?? null, + globalName: payload.data.d.global_name ?? null, + avatar: payload.data.d.avatar ?? null, + bot: payload.data.d.bot ?? false, + flags: payload.data.d.flags, accentColor: payload.data.d.accent_color, - avatar: payload.data.d.avatar, avatarDecoration: payload.data.d.avatar_decoration, banner: payload.data.d.banner, - bot: payload.data.d.bot, - flags: payload.data.d.flags, - id: payload.data.d.id, locale: payload.data.d.locale, mfaEnabled: payload.data.d.mfa_enabled, premiumType: payload.data.d.premium_type, publicFlags: payload.data.d.public_flags - }).where(eq(users.id, payload.data.d.id)); + }).onConflictDoUpdate({ + target: users.id, + set: { + username: payload.data.d.username, + discriminator: payload.data.d.discriminator ?? null, + globalName: payload.data.d.global_name ?? null, + avatar: payload.data.d.avatar ?? null, + bot: payload.data.d.bot ?? false, + flags: payload.data.d.flags, + accentColor: payload.data.d.accent_color, + avatarDecoration: payload.data.d.avatar_decoration, + banner: payload.data.d.banner, + locale: payload.data.d.locale, + mfaEnabled: payload.data.d.mfa_enabled, + premiumType: payload.data.d.premium_type, + publicFlags: payload.data.d.public_flags + } + }); await this.store.amqp.publish(RabbitMQ.GATEWAY_QUEUE_SEND, RoutingKey(clientId, payload.shardId), Buffer.from(JSON.stringify(payload.data))); }