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

Commit

Permalink
fix: no need to check first (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazmi35 authored Feb 24, 2024
1 parent ffe4a46 commit d7aede4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 81 deletions.
60 changes: 25 additions & 35 deletions packages/core/src/Structures/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { ChannelWrapper } from "amqp-connection-manager";
import type { Channel } from "amqplib";
import type { APIChannel, APIGuild, APIGuildMember, APIMessage, APIUser, RESTPostAPIChannelMessageJSONBody } from "discord-api-types/v10";
import { ChannelType, Routes } from "discord-api-types/v10";
import { and, eq, inArray, not, sql } from "drizzle-orm";
import { and, eq, notInArray, sql } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { drizzle } from "drizzle-orm/node-postgres";
import pg from "pg";
Expand Down Expand Up @@ -408,41 +408,31 @@ export class Client extends EventEmitter {
});

if ("permission_overwrites" in channel && channel.permission_overwrites !== undefined) {
const toBeDeleted = await this.store
.select({ id: schema.channelsOverwrite.channelId })
.from(schema.channelsOverwrite)
.where(
and(
eq(schema.channelsOverwrite.channelId, channel.id),
not(inArray(schema.channelsOverwrite.userOrRole, channel.permission_overwrites.map(x => x.id)))
)
);

if (toBeDeleted.length > 0) {
await this.store.delete(schema.channelsOverwrite).where(
and(
eq(schema.channelsOverwrite.channelId, channel.id),
inArray(schema.channelsOverwrite.userOrRole, toBeDeleted.map(x => x.id) as string[])
)
);
await this.store.delete(schema.channelsOverwrite).where(
and(
eq(schema.channelsOverwrite.channelId, channel.id),
notInArray(schema.channelsOverwrite.userOrRole, channel.permission_overwrites.map(x => x.id))
)
);

if (channel.permission_overwrites.length > 0) {
for (const overwrite of channel.permission_overwrites) {
// @ts-expect-error Intended to avoid .map
overwrite.channelId = channel.id;

// @ts-expect-error Intended to avoid .map
overwrite.userOrRole = overwrite.id;
}
await this.store.insert(schema.channelsOverwrite).values(channel.permission_overwrites)
.onConflictDoUpdate({
target: [schema.channelsOverwrite.userOrRole, schema.channelsOverwrite.channelId],
set: {
type: sql`EXCLUDED.type`,
allow: sql`EXCLUDED.allow`,
deny: sql`EXCLUDED.deny`
}
});
}

for (const overwrite of channel.permission_overwrites) {
// @ts-expect-error Intended to avoid .map
overwrite.channelId = channel.id;

// @ts-expect-error Intended to avoid .map
overwrite.userOrRole = overwrite.id;
}
await this.store.insert(schema.channelsOverwrite).values(channel.permission_overwrites)
.onConflictDoUpdate({
target: [schema.channelsOverwrite.userOrRole, schema.channelsOverwrite.channelId],
set: {
type: sql`EXCLUDED.type`,
allow: sql`EXCLUDED.allow`,
deny: sql`EXCLUDED.deny`
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { channels, channelsOverwrite } from "@nezuchan/kanao-schema";
import type { GatewayChannelUpdateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import { and, eq, inArray, not, sql } from "drizzle-orm";
import { and, eq, notInArray, sql } from "drizzle-orm";
import type { ListenerContext } from "../../../Stores/Listener.js";
import { Listener } from "../../../Stores/Listener.js";
import { stateChannels } from "../../../config.js";
Expand Down Expand Up @@ -38,24 +38,12 @@ export class ChannelUpdateListener extends Listener {
});

if ("permission_overwrites" in payload.data.d && payload.data.d.permission_overwrites !== undefined) {
const toBeDeleted = await this.container.client.drizzle
.select({ id: channelsOverwrite.channelId })
.from(channelsOverwrite)
.where(
and(
eq(channelsOverwrite.channelId, payload.data.d.id),
not(inArray(channelsOverwrite.userOrRole, payload.data.d.permission_overwrites.map(x => x.id)))
)
);

if (toBeDeleted.length > 0) {
await this.container.client.drizzle.delete(channelsOverwrite).where(
and(
eq(channelsOverwrite.channelId, payload.data.d.id),
inArray(channelsOverwrite.userOrRole, toBeDeleted.map(x => x.id) as string[])
)
);
}
await this.container.client.drizzle.delete(channelsOverwrite).where(
and(
eq(channelsOverwrite.channelId, payload.data.d.id),
notInArray(channelsOverwrite.userOrRole, payload.data.d.permission_overwrites.map(x => x.id))
)
);

if (payload.data.d.permission_overwrites.length > 0) {
for (const overwrite of payload.data.d.permission_overwrites) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { memberRoles, members, users } from "@nezuchan/kanao-schema";
import type { GatewayGuildMemberUpdateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import { and, eq, inArray, not, sql } from "drizzle-orm";
import { and, eq, notInArray, sql } from "drizzle-orm";
import type { ListenerContext } from "../../../Stores/Listener.js";
import { Listener } from "../../../Stores/Listener.js";
import { stateMembers, stateUsers } from "../../../config.js";
Expand Down Expand Up @@ -68,24 +68,12 @@ export class GuildMemberUpdateListener extends Listener {
}
});

const toBeDeleted = await this.container.client.drizzle
.select({ id: memberRoles.roleId })
.from(memberRoles)
.where(
and(
eq(memberRoles.memberId, payload.data.d.user.id),
not(inArray(memberRoles.roleId, payload.data.d.roles))
)
);

if (toBeDeleted.length > 0) {
await this.container.client.drizzle.delete(memberRoles).where(
and(
eq(memberRoles.memberId, payload.data.d.user.id),
inArray(memberRoles.roleId, toBeDeleted.map(x => x.id) as string[])
)
);
}
await this.container.client.drizzle.delete(memberRoles).where(
and(
eq(memberRoles.memberId, payload.data.d.user.id),
notInArray(memberRoles.roleId, payload.data.d.roles)
)
);

if (payload.data.d.roles.length > 0) {
await this.container.client.drizzle.insert(memberRoles).values(payload.data.d.roles.map(role => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { channels, channelsOverwrite, guilds, memberRoles, members, roles, users, voiceStates } from "@nezuchan/kanao-schema";
import type { GatewayGuildCreateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import { sql } from "drizzle-orm";
import { and, eq, notInArray, sql } from "drizzle-orm";
import type { ListenerContext } from "../../../Stores/Listener.js";
import { Listener } from "../../../Stores/Listener.js";
import { clientId, guildCreateGcEvery, stateChannels, stateRoles } from "../../../config.js";
Expand Down Expand Up @@ -239,14 +239,22 @@ export class GuildCreateListener extends Listener {
for (const ch of payload.data.d.channels) {
if (
"permission_overwrites" in ch &&
ch.permission_overwrites !== undefined &&
ch.permission_overwrites.length > 0
ch.permission_overwrites !== undefined
) {
await this.container.client.drizzle.insert(channelsOverwrite)
.values(ch.permission_overwrites)
.onConflictDoNothing({
target: [channelsOverwrite.userOrRole, channelsOverwrite.channelId]
});
await this.container.client.drizzle.delete(channelsOverwrite).where(
and(
eq(channelsOverwrite.channelId, ch.id),
notInArray(channelsOverwrite.userOrRole, ch.permission_overwrites.map(x => x.id))
)
);

if (ch.permission_overwrites.length > 0) {
await this.container.client.drizzle.insert(channelsOverwrite)
.values(ch.permission_overwrites)
.onConflictDoNothing({
target: [channelsOverwrite.userOrRole, channelsOverwrite.channelId]
});
}
}
}
}
Expand Down

0 comments on commit d7aede4

Please sign in to comment.