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

Commit

Permalink
fix: insert multiple values
Browse files Browse the repository at this point in the history
  • Loading branch information
KagChi committed Feb 20, 2024
1 parent 15b1d9e commit 9c38b13
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Buffer } from "node:buffer";
import { RabbitMQ } from "@nezuchan/constants";
import { channels, channelsOverwrite } from "@nezuchan/kanao-schema";
import { RoutingKey } from "@nezuchan/utilities";
import { Result } from "@sapphire/result";
import type { GatewayChannelCreateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import type { ListenerContext } from "../../../Stores/Listener.js";
Expand Down Expand Up @@ -41,18 +40,16 @@ export class ChannelCreateListener extends Listener {
.returning({ id: channels.id })
.then(c => c[0]);

if ("permission_overwrites" in payload.data.d && payload.data.d.permission_overwrites !== undefined) {
await Promise.all(payload.data.d.permission_overwrites.map(async overwrite => Result.fromAsync(async () => {
await this.store.drizzle.insert(channelsOverwrite).values({
userOrRole: overwrite.id,
channelId: channel.id,
type: overwrite.type,
allow: overwrite.allow,
deny: overwrite.deny
}).onConflictDoNothing({
target: [channelsOverwrite.userOrRole, channelsOverwrite.channelId]
});
})));
if ("permission_overwrites" in payload.data.d && payload.data.d.permission_overwrites !== undefined && payload.data.d.permission_overwrites.length > 0) {
await this.store.drizzle.insert(channelsOverwrite).values(payload.data.d.permission_overwrites.map(overwrite => ({
userOrRole: overwrite.id,
channelId: channel.id,
type: overwrite.type,
allow: overwrite.allow,
deny: overwrite.deny
}))).onConflictDoNothing({
target: [channelsOverwrite.userOrRole, channelsOverwrite.channelId]
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Buffer } from "node:buffer";
import { RabbitMQ } from "@nezuchan/constants";
import { channels, channelsOverwrite } from "@nezuchan/kanao-schema";
import { RoutingKey } from "@nezuchan/utilities";
import { Result } from "@sapphire/result";
import type { GatewayChannelUpdateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import { eq } from "drizzle-orm";
Expand Down Expand Up @@ -45,17 +44,15 @@ export class ChannelUpdateListener extends Listener {
await this.store.drizzle.delete(channelsOverwrite).where(eq(channelsOverwrite.channelId, payload.data.d.id));

if ("permission_overwrites" in payload.data.d && payload.data.d.permission_overwrites !== undefined && payload.data.d.permission_overwrites.length > 0) {
await Promise.all(payload.data.d.permission_overwrites.map(async overwrite => Result.fromAsync(async () => {
await this.store.drizzle.insert(channelsOverwrite).values({
userOrRole: overwrite.id,
channelId: channel.id,
type: overwrite.type,
allow: overwrite.allow,
deny: overwrite.deny
}).onConflictDoNothing({
target: [channelsOverwrite.userOrRole, channelsOverwrite.channelId]
});
})));
await this.store.drizzle.insert(channelsOverwrite).values(payload.data.d.permission_overwrites.map(overwrite => ({
userOrRole: overwrite.id,
channelId: channel.id,
type: overwrite.type,
allow: overwrite.allow,
deny: overwrite.deny
}))).onConflictDoNothing({
target: [channelsOverwrite.userOrRole, channelsOverwrite.channelId]
});
}

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 @@ -2,7 +2,6 @@ import { Buffer } from "node:buffer";
import { RabbitMQ } from "@nezuchan/constants";
import { memberRoles, members, users } from "@nezuchan/kanao-schema";
import { RoutingKey } from "@nezuchan/utilities";
import { Result } from "@sapphire/result";
import type { GatewayGuildMemberAddDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import type { ListenerContext } from "../../../Stores/Listener.js";
Expand Down Expand Up @@ -63,13 +62,13 @@ export class GuildMemberAddListener extends Listener {
}
});

await Promise.all(payload.data.d.roles.map(async role => Result.fromAsync(async () => {
await this.store.drizzle.insert(memberRoles).values({
if (payload.data.d.roles.length > 0) {
await this.store.drizzle.insert(memberRoles).values(payload.data.d.roles.map(role => ({
memberId: payload.data.d.user!.id,
roleId: role,
guildId: payload.data.d.guild_id
}).onConflictDoNothing({ target: [memberRoles.memberId, memberRoles.roleId] });
})));
}))).onConflictDoNothing({ target: [memberRoles.memberId, memberRoles.roleId] });
}

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 @@ -2,7 +2,6 @@ import { Buffer } from "node:buffer";
import { RabbitMQ } from "@nezuchan/constants";
import { memberRoles, members, users } from "@nezuchan/kanao-schema";
import { RoutingKey } from "@nezuchan/utilities";
import { Result } from "@sapphire/result";
import type { GatewayGuildMemberUpdateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import { and, eq } from "drizzle-orm";
Expand Down Expand Up @@ -83,13 +82,13 @@ export class GuildMemberUpdateListener extends Listener {

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

await Promise.all(payload.data.d.roles.map(async role => Result.fromAsync(async () => {
await this.store.drizzle.insert(memberRoles).values({
if (payload.data.d.roles.length > 0) {
await this.store.drizzle.insert(memberRoles).values(payload.data.d.roles.map(role => ({
memberId: payload.data.d.user.id,
roleId: role,
guildId: payload.data.d.guild_id
}).onConflictDoNothing({ target: [memberRoles.memberId, memberRoles.roleId] });
})));
}))).onConflictDoNothing({ target: [memberRoles.memberId, memberRoles.roleId] });
}
}

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
@@ -1,6 +1,6 @@
import { Buffer } from "node:buffer";
import { RabbitMQ } from "@nezuchan/constants";
import { members, users } from "@nezuchan/kanao-schema";
import { memberRoles, members, users } from "@nezuchan/kanao-schema";
import { RoutingKey } from "@nezuchan/utilities";
import type { GatewayGuildMembersChunkDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
Expand Down Expand Up @@ -51,6 +51,14 @@ export class GuildMembersChunkListener extends Listener {
premiumSince: member.premium_since
}).onConflictDoNothing({ target: members.id });
}

if (member.roles.length > 0) {
await this.store.drizzle.insert(memberRoles).values(member.roles.map(role => ({
memberId: member.user!.id,
roleId: role,
guildId: payload.data.d.guild_id
}))).onConflictDoNothing({ target: [memberRoles.memberId, memberRoles.roleId] });
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,30 +185,28 @@ export class GuildCreateListener extends Listener {
}
})));

if (stateChannels) {
await Promise.all(payload.data.d.channels.map(async channel => Result.fromAsync(async () => {
await this.store.drizzle.insert(channels).values({
id: channel.id,
guildId: payload.data.d.id,
name: channel.name,
type: channel.type,
flags: channel.flags
}).onConflictDoNothing({ target: channels.id });
if (stateChannels && payload.data.d.channels.length > 0) {
await this.store.drizzle.insert(channels).values(payload.data.d.channels.map(channel => ({
id: channel.id,
guildId: payload.data.d.id,
name: channel.name,
type: channel.type,
flags: channel.flags
}))).onConflictDoNothing({ target: channels.id });

if ("permission_overwrites" in channel && channel.permission_overwrites !== undefined) {
await Promise.all(channel.permission_overwrites?.map(async overwrite => {
await this.store.drizzle.insert(channelsOverwrite).values({
userOrRole: overwrite.id,
channelId: channel.id,
type: overwrite.type,
allow: overwrite.allow,
deny: overwrite.deny
}).onConflictDoNothing({
target: [channelsOverwrite.userOrRole, channelsOverwrite.channelId]
});
}));
for (const channel of payload.data.d.channels) {
if ("permission_overwrites" in channel && channel.permission_overwrites !== undefined && channel.permission_overwrites.length > 0) {
await this.store.drizzle.insert(channelsOverwrite).values(channel.permission_overwrites.map(overwrite => ({
userOrRole: overwrite.id,
channelId: channel.id,
type: overwrite.type,
allow: overwrite.allow,
deny: overwrite.deny
}))).onConflictDoNothing({
target: [channelsOverwrite.userOrRole, channelsOverwrite.channelId]
});
}
})));
}
}

if (stateVoices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Buffer } from "node:buffer";
import { RabbitMQ } from "@nezuchan/constants";
import { memberRoles, members, messages, users } from "@nezuchan/kanao-schema";
import { RoutingKey } from "@nezuchan/utilities";
import { Result } from "@sapphire/result";
import type { GatewayMessageCreateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import type { ListenerContext } from "../../../Stores/Listener.js";
Expand Down Expand Up @@ -101,13 +100,13 @@ export class MessageCreateListener extends Listener {
}).onConflictDoNothing({ target: messages.id });
}

await Promise.all(payload.data.d.member.roles.map(async role => Result.fromAsync(async () => {
await this.store.drizzle.insert(memberRoles).values({
if (payload.data.d.member.roles.length > 0) {
await this.store.drizzle.insert(memberRoles).values(payload.data.d.member.roles.map(role => ({
memberId: payload.data.d.author.id,
roleId: role,
guildId: payload.data.d.guild_id
}).onConflictDoNothing({ target: [memberRoles.memberId, memberRoles.roleId] });
})));
}))).onConflictDoNothing({ target: [memberRoles.memberId, memberRoles.roleId] });
}
}

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
@@ -1,6 +1,6 @@
import { Buffer } from "node:buffer";
import { RabbitMQ } from "@nezuchan/constants";
import { members, messages, users } from "@nezuchan/kanao-schema";
import { memberRoles, members, messages, users } from "@nezuchan/kanao-schema";
import { RoutingKey } from "@nezuchan/utilities";
import type { GatewayMessageUpdateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
Expand Down Expand Up @@ -80,6 +80,14 @@ export class MessageUpdateListener extends Listener {
premiumSince: payload.data.d.member.premium_since
}
});

if (payload.data.d.member.roles.length > 0) {
await this.store.drizzle.insert(memberRoles).values(payload.data.d.member.roles.map(role => ({
memberId: payload.data.d.author!.id,
roleId: role,
guildId: payload.data.d.guild_id
}))).onConflictDoNothing({ target: [memberRoles.memberId, memberRoles.roleId] });
}
}

if (stateMessages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Buffer } from "node:buffer";
import { RabbitMQ } from "@nezuchan/constants";
import { memberRoles, members, users, voiceStates } from "@nezuchan/kanao-schema";
import { RoutingKey } from "@nezuchan/utilities";
import { Result } from "@sapphire/result";
import type { GatewayVoiceStateUpdateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import { and, eq } from "drizzle-orm";
Expand Down Expand Up @@ -52,12 +51,13 @@ export class VoiceStateUpdateListener extends Listener {
premiumSince: payload.data.d.member.premium_since
}).onConflictDoNothing({ target: members.id });

await Promise.all(payload.data.d.member.roles.map(async role => Result.fromAsync(async () => {
await this.store.drizzle.insert(memberRoles).values({
if (payload.data.d.member.roles.length > 0) {
await this.store.drizzle.insert(memberRoles).values(payload.data.d.member.roles.map(role => ({
memberId: payload.data.d.user_id,
roleId: role
}).onConflictDoNothing({ target: [memberRoles.memberId, memberRoles.roleId] });
})));
roleId: role,
guildId: payload.data.d.guild_id
}))).onConflictDoNothing({ target: [memberRoles.memberId, memberRoles.roleId] });
}
}

if (stateVoices) {
Expand Down

0 comments on commit 9c38b13

Please sign in to comment.