seyfert
Version:
The most advanced framework for discord bots
147 lines (146 loc) • 6.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Guilds = void 0;
const transformers_1 = require("../../client/transformers");
const common_1 = require("../../common");
const base_1 = require("./default/base");
class Guilds extends base_1.BaseResource {
namespace = 'guild';
//@ts-expect-error
filter(data, id, from) {
return true;
}
get(id) {
return (0, common_1.fakePromise)(super.get(id)).then(guild => guild ? transformers_1.Transformers.Guild(this.client, guild) : undefined);
}
raw(id) {
return super.get(id);
}
bulk(ids) {
return (0, common_1.fakePromise)(super.bulk(ids)).then(guilds => guilds.map(x => transformers_1.Transformers.Guild(this.client, x)));
}
bulkRaw(ids) {
return super.bulk(ids);
}
values() {
return (0, common_1.fakePromise)(super.values()).then(guilds => guilds.map(x => transformers_1.Transformers.Guild(this.client, x)));
}
valuesRaw() {
return super.values();
}
async remove(id) {
const keysChannels = (await this.cache.channels?.keys(id)) ?? [];
await this.cache.adapter.bulkRemove((await Promise.all([
this.cache.members?.keys(id) ?? [],
this.cache.roles?.keys(id) ?? [],
keysChannels,
this.cache.emojis?.keys(id) ?? [],
this.cache.stickers?.keys(id) ?? [],
this.cache.voiceStates?.keys(id) ?? [],
this.cache.presences?.keys(id) ?? [],
this.cache.stageInstances?.keys(id) ?? [],
this.cache.bans?.keys(id) ?? [],
])).flat());
if (this.cache.messages && this.cache.channels) {
const keysMessages = [];
for (const i of keysChannels) {
const channelId = i.slice(this.cache.channels.namespace.length + 1);
const messages = await this.cache.messages.keys(channelId);
keysMessages.push(...messages);
}
if (keysMessages.length)
await this.cache.adapter.bulkRemove(keysMessages);
}
await this.cache.adapter.removeRelationship([
this.cache.members?.hashId(id),
this.cache.roles?.hashId(id),
this.cache.channels?.hashId(id),
this.cache.emojis?.hashId(id),
this.cache.stickers?.hashId(id),
this.cache.voiceStates?.hashId(id),
this.cache.presences?.hashId(id),
this.cache.stageInstances?.hashId(id),
].filter(x => x !== undefined));
await super.remove(id);
}
async set(from, id, data) {
const bulkData = [];
for (const member of data.members ?? []) {
if (!member.user?.id) {
continue;
}
bulkData.push([from, 'members', member, member.user.id, id]);
bulkData.push([from, 'users', member.user, member.user.id]);
}
for (const role of data.roles ?? []) {
bulkData.push([from, 'roles', role, role.id, id]);
}
for (const channel of data.channels ?? []) {
bulkData.push([from, 'channels', channel, channel.id, id]);
if (channel.permission_overwrites?.length)
bulkData.push([from, 'overwrites', channel.permission_overwrites, channel.id, id]);
}
for (const thread of data.threads ?? []) {
bulkData.push([from, 'channels', thread, thread.id, id]);
}
for (const emoji of data.emojis ?? []) {
bulkData.push([from, 'emojis', emoji, emoji.id, id]);
}
for (const sticker of data.stickers ?? []) {
bulkData.push([from, 'stickers', sticker, sticker.id, id]);
}
for (const voiceState of data.voice_states ?? []) {
bulkData.push([from, 'voiceStates', voiceState, voiceState.user_id, id]);
}
for (const presence of data.presences ?? []) {
bulkData.push([from, 'presences', presence, presence.user.id, id]);
}
for (const instance of data.stage_instances ?? []) {
bulkData.push([from, 'stageInstances', instance, instance.id, id]);
}
const { voice_states, members, channels, threads, presences, stage_instances, guild_scheduled_events, roles, emojis, stickers, guild_hashes, ...guild } = data;
bulkData.push([from, 'guilds', guild, id]);
await this.cache.bulkSet(bulkData);
}
async patch(from, id, data) {
const bulkData = [];
for (const member of data.members ?? []) {
if (!member.user?.id) {
continue;
}
bulkData.push([from, 'members', member, member.user.id, id]);
bulkData.push([from, 'users', member.user, member.user.id]);
}
for (const role of data.roles ?? []) {
bulkData.push([from, 'roles', role, role.id, id]);
}
for (const channel of data.channels ?? []) {
bulkData.push([from, 'channels', channel, channel.id, id]);
if (channel.permission_overwrites?.length) {
bulkData.push([from, 'overwrites', channel.permission_overwrites, channel.id, id]);
}
}
for (const thread of data.threads ?? []) {
bulkData.push([from, 'channels', thread, thread.id, id]);
}
for (const emoji of data.emojis ?? []) {
bulkData.push([from, 'emojis', emoji, emoji.id, id]);
}
for (const sticker of data.stickers ?? []) {
bulkData.push([from, 'stickers', sticker, sticker.id, id]);
}
for (const voiceState of data.voice_states ?? []) {
bulkData.push([from, 'voiceStates', voiceState, voiceState.user_id, id]);
}
for (const presence of data.presences ?? []) {
bulkData.push([from, 'presences', presence, presence.user.id, id]);
}
for (const instance of data.stage_instances ?? []) {
bulkData.push([from, 'stageInstances', instance, instance.id, id]);
}
const { voice_states, members, channels, threads, presences, stage_instances, guild_scheduled_events, roles, emojis, stickers, guild_hashes, ...guild } = data;
bulkData.push([from, 'guilds', guild, id]);
await this.cache.bulkPatch(bulkData);
}
}
exports.Guilds = Guilds;