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

Commit

Permalink
feat: everything working as expected
Browse files Browse the repository at this point in the history
Co-authored-by: Vann <[email protected]>
  • Loading branch information
KagChi and Vann-Dev committed Feb 12, 2024
1 parent e1c16c9 commit 0665736
Show file tree
Hide file tree
Showing 22 changed files with 288 additions and 267 deletions.
31 changes: 28 additions & 3 deletions src/Listeners/Caches/Channels/ChannelCreateListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { RabbitMQ } from "@nezuchan/constants";
import { RoutingKey } from "@nezuchan/utilities";
import type { GatewayChannelCreateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import { channels, guildsChannels } from "../../../Schema/index.js";
import { eq } from "drizzle-orm";
import { channels, channelsOverwrite, guildsChannels } from "../../../Schema/index.js";
import type { ListenerContext } from "../../../Stores/Listener.js";
import { Listener } from "../../../Stores/Listener.js";
import { clientId, stateChannels } from "../../../config.js";
Expand All @@ -19,14 +20,38 @@ export class ChannelCreateListener extends Listener {
if (stateChannels) {
await this.store.drizzle.insert(channels).values({
id: payload.data.d.id,
name: payload.data.d.name
name: payload.data.d.name,
type: payload.data.d.type,
position: "position" in payload.data.d ? payload.data.d.position : null,
topic: "topic" in payload.data.d ? payload.data.d.topic : null,
nsfw: "nsfw" in payload.data.d ? payload.data.d.nsfw : null,
lastMessageId: "last_message_id" in payload.data.d ? payload.data.d.last_message_id : undefined
}).onConflictDoUpdate({
target: channels.id,
set: {
name: payload.data.d.name
name: payload.data.d.name,
type: payload.data.d.type,
position: "position" in payload.data.d ? payload.data.d.position : null,
topic: "topic" in payload.data.d ? payload.data.d.topic : null,
nsfw: "nsfw" in payload.data.d ? payload.data.d.nsfw : null,
lastMessageId: "last_message_id" in payload.data.d ? payload.data.d.last_message_id : undefined
}
});

if ("permission_overwrites" in payload.data.d && payload.data.d.permission_overwrites !== undefined) {
await this.store.drizzle.delete(channelsOverwrite).where(eq(channelsOverwrite.id, payload.data.d.id));
for (const overwrite of payload.data.d.permission_overwrites) {
await this.store.drizzle.insert(channelsOverwrite).values({
id: payload.data.d.id,
type: overwrite.type,
allow: overwrite.allow,
deny: overwrite.deny
}).onConflictDoNothing({
target: channelsOverwrite.id
});
}
}

if ("guild_id" in payload.data.d) {
await this.store.drizzle.insert(guildsChannels).values({
id: payload.data.d.id,
Expand Down
4 changes: 1 addition & 3 deletions src/Listeners/Caches/Channels/ChannelDeleteListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export class ChannelPintsUpdateListener extends Listener {
public async run(payload: { data: GatewayChannelDeleteDispatch; shardId: number; }): Promise<void> {
await this.store.drizzle.delete(channels).where(eq(channels.id, payload.data.d.id));

await this.store.amqp.publish(RabbitMQ.GATEWAY_QUEUE_SEND, RoutingKey(clientId, payload.shardId), Buffer.from(JSON.stringify({
...payload.data
})));
await this.store.amqp.publish(RabbitMQ.GATEWAY_QUEUE_SEND, RoutingKey(clientId, payload.shardId), Buffer.from(JSON.stringify(payload.data)));
}
}
46 changes: 0 additions & 46 deletions src/Listeners/Caches/Channels/ChannelPinsUpdateListener.ts

This file was deleted.

49 changes: 42 additions & 7 deletions src/Listeners/Caches/Channels/ChannelUpdateListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RoutingKey } from "@nezuchan/utilities";
import type { GatewayChannelUpdateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import { eq } from "drizzle-orm";
import { channels } from "../../../Schema/index.js";
import { channels, channelsOverwrite, guildsChannels } from "../../../Schema/index.js";
import type { ListenerContext } from "../../../Stores/Listener.js";
import { Listener } from "../../../Stores/Listener.js";
import { clientId, stateChannels } from "../../../config.js";
Expand All @@ -18,12 +18,47 @@ export class ChannelUpdateListener extends Listener {
}

public async run(payload: { data: GatewayChannelUpdateDispatch; shardId: number; }): Promise<void> {
await this.store.drizzle.update(channels).set({
id: payload.data.d.id
}).where(eq(channels.id, payload.data.d.id));
await this.store.drizzle.insert(channels).values({
id: payload.data.d.id,
name: payload.data.d.name,
type: payload.data.d.type,
position: "position" in payload.data.d ? payload.data.d.position : null,
topic: "topic" in payload.data.d ? payload.data.d.topic : null,
nsfw: "nsfw" in payload.data.d ? payload.data.d.nsfw : null,
lastMessageId: "last_message_id" in payload.data.d ? payload.data.d.last_message_id : undefined
}).onConflictDoUpdate({
target: channels.id,
set: {
name: payload.data.d.name,
type: payload.data.d.type,
position: "position" in payload.data.d ? payload.data.d.position : null,
topic: "topic" in payload.data.d ? payload.data.d.topic : null,
nsfw: "nsfw" in payload.data.d ? payload.data.d.nsfw : null,
lastMessageId: "last_message_id" in payload.data.d ? payload.data.d.last_message_id : undefined
}
});

if ("permission_overwrites" in payload.data.d && payload.data.d.permission_overwrites !== undefined) {
await this.store.drizzle.delete(channelsOverwrite).where(eq(channelsOverwrite.id, payload.data.d.id));
for (const overwrite of payload.data.d.permission_overwrites) {
await this.store.drizzle.insert(channelsOverwrite).values({
id: payload.data.d.id,
type: overwrite.type,
allow: overwrite.allow,
deny: overwrite.deny
}).onConflictDoNothing({
target: channelsOverwrite.id
});
}
}

if ("guild_id" in payload.data.d) {
await this.store.drizzle.insert(guildsChannels).values({
id: payload.data.d.id,
guildId: payload.data.d.guild_id
}).onConflictDoNothing({ target: guildsChannels.id });
}

await this.store.amqp.publish(RabbitMQ.GATEWAY_QUEUE_SEND, RoutingKey(clientId, payload.shardId), Buffer.from(JSON.stringify({
...payload.data
})));
await this.store.amqp.publish(RabbitMQ.GATEWAY_QUEUE_SEND, RoutingKey(clientId, payload.shardId), Buffer.from(JSON.stringify(payload.data)));
}
}
37 changes: 0 additions & 37 deletions src/Listeners/Caches/GuildEmojis/GuildEmojisUpdate.ts

This file was deleted.

26 changes: 16 additions & 10 deletions src/Listeners/Caches/GuildMembers/GuildMemberAddListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ export class GuildMemberAddListener extends Listener {
}).onConflictDoNothing({ target: users.id });
}

const member = await this.store.drizzle.query.members.findFirst({
where: (m, { eq }) => eq(m.id, payload.data.d.user!.id),
columns: {
id: true
}
}) ?? await this.store.drizzle.insert(members).values({
await this.store.drizzle.insert(members).values({
id: payload.data.d.user!.id,
avatar: payload.data.d.avatar,
flags: payload.data.d.flags,
Expand All @@ -51,13 +46,24 @@ export class GuildMemberAddListener extends Listener {
nick: payload.data.d.nick,
pending: payload.data.d.pending,
premiumSince: payload.data.d.premium_since
}).onConflictDoNothing({ target: members.id })
.returning({ id: members.id })
.then(x => x[0]);
}).onConflictDoUpdate({
target: members.id,
set: {
avatar: payload.data.d.avatar,
flags: payload.data.d.flags,
communicationDisabledUntil: payload.data.d.premium_since,
deaf: payload.data.d.deaf,
joinedAt: payload.data.d.joined_at,
mute: payload.data.d.mute,
nick: payload.data.d.nick,
pending: payload.data.d.pending,
premiumSince: payload.data.d.premium_since
}
});

for (const role of payload.data.d.roles) {
await this.store.drizzle.insert(memberRoles).values({
id: member!.id,
id: payload.data.d.user!.id,
roleId: role
}).onConflictDoNothing({ target: memberRoles.id });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ export class GuildMemberRemoveListener extends Listener {

public async run(payload: { data: GatewayGuildMemberRemoveDispatch; shardId: number; }): Promise<void> {
await this.store.drizzle.delete(members).where(eq(members.id, payload.data.d.user.id));

await this.store.amqp.publish(RabbitMQ.GATEWAY_QUEUE_SEND, RoutingKey(clientId, payload.shardId), Buffer.from(JSON.stringify({
...payload.data
})));
await this.store.amqp.publish(RabbitMQ.GATEWAY_QUEUE_SEND, RoutingKey(clientId, payload.shardId), Buffer.from(JSON.stringify(payload.data)));
}
}
51 changes: 46 additions & 5 deletions src/Listeners/Caches/GuildMembers/GuildMemberUpdateListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RoutingKey } from "@nezuchan/utilities";
import type { GatewayGuildMemberUpdateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import { eq } from "drizzle-orm";
import { members, users } from "../../../Schema/index.js";
import { memberRoles, members, users } from "../../../Schema/index.js";
import type { ListenerContext } from "../../../Stores/Listener.js";
import { Listener } from "../../../Stores/Listener.js";
import { clientId, stateMembers, stateUsers } from "../../../config.js";
Expand All @@ -18,7 +18,8 @@ export class GuildMemberUpdateListener extends Listener {

public async run(payload: { data: GatewayGuildMemberUpdateDispatch; shardId: number; }): Promise<void> {
if (stateUsers) {
await this.store.drizzle.update(users).set({
await this.store.drizzle.insert(users).values({
id: payload.data.d.user.id,
username: payload.data.d.user.username,
accentColor: payload.data.d.user.accent_color,
avatar: payload.data.d.user.avatar,
Expand All @@ -32,11 +33,29 @@ export class GuildMemberUpdateListener extends Listener {
mfaEnabled: payload.data.d.user.mfa_enabled,
premiumType: payload.data.d.user.premium_type,
publicFlags: payload.data.d.user.public_flags
}).where(eq(users.id, payload.data.d.user.id));
}).onConflictDoUpdate({
target: users.id,
set: {
username: payload.data.d.user.username,
accentColor: payload.data.d.user.accent_color,
avatar: payload.data.d.user.avatar,
avatarDecoration: payload.data.d.user.avatar_decoration,
banner: payload.data.d.user.banner,
bot: payload.data.d.user.bot,
discriminator: payload.data.d.user.discriminator,
flags: payload.data.d.user.flags,
globalName: payload.data.d.user.global_name,
locale: payload.data.d.user.locale,
mfaEnabled: payload.data.d.user.mfa_enabled,
premiumType: payload.data.d.user.premium_type,
publicFlags: payload.data.d.user.public_flags
}
});
}

if (stateMembers) {
await this.store.drizzle.update(members).set({
await this.store.drizzle.insert(members).values({
id: payload.data.d.user.id,
avatar: payload.data.d.avatar,
communicationDisabledUntil: payload.data.d.premium_since,
deaf: payload.data.d.deaf,
Expand All @@ -46,7 +65,29 @@ export class GuildMemberUpdateListener extends Listener {
nick: payload.data.d.nick,
pending: payload.data.d.pending,
premiumSince: payload.data.d.premium_since
}).where(eq(members.id, payload.data.d.user.id));
}).onConflictDoUpdate({
target: members.id,
set: {
avatar: payload.data.d.avatar,
communicationDisabledUntil: payload.data.d.premium_since,
deaf: payload.data.d.deaf,
flags: payload.data.d.flags,
joinedAt: payload.data.d.joined_at,
mute: payload.data.d.mute,
nick: payload.data.d.nick,
pending: payload.data.d.pending,
premiumSince: payload.data.d.premium_since
}
});

await this.store.drizzle.delete(memberRoles).where(eq(memberRoles.id, payload.data.d.user.id));

for (const role of payload.data.d.roles) {
await this.store.drizzle.insert(memberRoles).values({
id: payload.data.d.user.id,
roleId: role
}).onConflictDoNothing({ target: memberRoles.id });
}
}

await this.store.amqp.publish(RabbitMQ.GATEWAY_QUEUE_SEND, RoutingKey(clientId, payload.shardId), Buffer.from(JSON.stringify(payload.data)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class GuildMembersChunkListener extends Listener {
nick: member.nick,
pending: member.pending,
premiumSince: member.premium_since
}).onConflictDoNothing({ target: users.id });
}).onConflictDoNothing({ target: members.id });
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/Listeners/Caches/GuildRoles/GuildRoleCreateListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ export class GuildRoleCreateListener extends Listener {
position: payload.data.d.role.position,
color: payload.data.d.role.color,
hoist: payload.data.d.role.hoist
}).onConflictDoUpdate({
target: roles.id,
set: {
name: payload.data.d.role.name,
permissions: payload.data.d.role.permissions,
position: payload.data.d.role.position,
color: payload.data.d.role.color,
hoist: payload.data.d.role.hoist
}
});
}
await this.store.amqp.publish(RabbitMQ.GATEWAY_QUEUE_SEND, RoutingKey(clientId, payload.shardId), Buffer.from(JSON.stringify(payload.data)));
Expand Down
Loading

0 comments on commit 0665736

Please sign in to comment.