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

Commit

Permalink
chore: use promise all
Browse files Browse the repository at this point in the history
  • Loading branch information
KagChi committed Feb 16, 2024
1 parent d702fdb commit adff693
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ 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 { eq } from "drizzle-orm";
import type { ListenerContext } from "../../../Stores/Listener.js";
import { Listener } from "../../../Stores/Listener.js";
import { clientId, stateChannels } from "../../../config.js";
Expand All @@ -18,7 +18,7 @@ export class ChannelCreateListener extends Listener {

public async run(payload: { data: GatewayChannelCreateDispatch; shardId: number; }): Promise<void> {
if (stateChannels) {
await this.store.drizzle.insert(channels).values({
const channel = await this.store.drizzle.insert(channels).values({
id: payload.data.d.id,
guildId: "guild_id" in payload.data.d ? payload.data.d.guild_id : null,
name: payload.data.d.name,
Expand All @@ -37,21 +37,22 @@ export class ChannelCreateListener extends Listener {
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
}
});
})
.returning({ id: channels.id })
.then(c => c[0]);

if ("permission_overwrites" in payload.data.d && payload.data.d.permission_overwrites !== undefined) {
await this.store.drizzle.delete(channelsOverwrite).where(eq(channelsOverwrite.channelId, payload.data.d.id));
for (const overwrite of payload.data.d.permission_overwrites) {
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: payload.data.d.id,
channelId: channel.id,
type: overwrite.type,
allow: overwrite.allow,
deny: overwrite.deny
}).onConflictDoNothing({
target: channelsOverwrite.id
});
}
})));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ 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";
import type { ListenerContext } from "../../../Stores/Listener.js";
import { Listener } from "../../../Stores/Listener.js";
import { clientId, stateChannels } from "../../../config.js";
Expand All @@ -18,7 +18,7 @@ export class ChannelUpdateListener extends Listener {
}

public async run(payload: { data: GatewayChannelUpdateDispatch; shardId: number; }): Promise<void> {
await this.store.drizzle.insert(channels).values({
const channel = await this.store.drizzle.insert(channels).values({
id: payload.data.d.id,
guildId: "guild_id" in payload.data.d ? payload.data.d.guild_id : null,
name: payload.data.d.name,
Expand All @@ -37,21 +37,22 @@ export class ChannelUpdateListener extends Listener {
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
}
});
})
.returning({ id: channels.id })
.then(c => c[0]);

if ("permission_overwrites" in payload.data.d && payload.data.d.permission_overwrites !== undefined) {
await this.store.drizzle.delete(channelsOverwrite).where(eq(channelsOverwrite.channelId, payload.data.d.id));
for (const overwrite of payload.data.d.permission_overwrites) {
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: payload.data.d.id,
channelId: channel.id,
type: overwrite.type,
allow: overwrite.allow,
deny: overwrite.deny
}).onConflictDoNothing({
target: channelsOverwrite.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 @@ -2,6 +2,7 @@ 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 @@ -62,13 +63,13 @@ export class GuildMemberAddListener extends Listener {
}
});

for (const role of payload.data.d.roles) {
await Promise.all(payload.data.d.roles.map(async role => Result.fromAsync(async () => {
await this.store.drizzle.insert(memberRoles).values({
memberId: payload.data.d.user!.id,
roleId: role,
guildId: payload.data.d.guild_id
}).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 @@ -2,6 +2,7 @@ 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 type { ListenerContext } from "../../../Stores/Listener.js";
Expand Down Expand Up @@ -79,13 +80,13 @@ export class GuildMemberUpdateListener extends Listener {
}
});

for (const role of payload.data.d.roles) {
await Promise.all(payload.data.d.roles.map(async role => Result.fromAsync(async () => {
await this.store.drizzle.insert(memberRoles).values({
memberId: payload.data.d.user.id,
roleId: role,
guildId: payload.data.d.guild_id
}).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 @@ -2,6 +2,7 @@ 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 @@ -100,13 +101,13 @@ export class MessageCreateListener extends Listener {
}).onConflictDoNothing({ target: messages.id });
}

for (const role of payload.data.d.member.roles) {
await Promise.all(payload.data.d.member.roles.map(async role => Result.fromAsync(async () => {
await this.store.drizzle.insert(memberRoles).values({
memberId: payload.data.d.author.id,
roleId: role,
guildId: payload.data.d.guild_id
}).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 @@ -2,6 +2,7 @@ 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 { eq } from "drizzle-orm";
Expand Down Expand Up @@ -51,12 +52,12 @@ export class VoiceStateUpdateListener extends Listener {
premiumSince: payload.data.d.member.premium_since
}).onConflictDoNothing({ target: members.id });

for (const role of payload.data.d.member.roles) {
await Promise.all(payload.data.d.member.roles.map(async role => Result.fromAsync(async () => {
await this.store.drizzle.insert(memberRoles).values({
memberId: payload.data.d.user_id,
roleId: role
}).onConflictDoNothing({ target: memberRoles.id });
}
})));
}

if (stateVoices) {
Expand Down

0 comments on commit adff693

Please sign in to comment.