refactor: rename controllers to handlers and handlers to helpers (#660)

* refactor: rename controllers to handlers and handlers to helpers

* fmt
This commit is contained in:
ayntee
2021-03-11 21:41:03 +04:00
committed by GitHub
parent aaed064709
commit 8654aeded5
72 changed files with 271 additions and 296 deletions
+13
View File
@@ -0,0 +1,13 @@
import { eventHandlers } from "../../bot.ts";
import { ChannelCreatePayload, DiscordPayload } from "../../types/mod.ts";
import { structures } from "../../structures/mod.ts";
import { cacheHandlers } from "../../cache.ts";
export async function handleChannelCreate(data: DiscordPayload) {
const payload = data.d as ChannelCreatePayload;
const channelStruct = await structures.createChannelStruct(payload);
await cacheHandlers.set("channels", channelStruct.id, channelStruct);
eventHandlers.channelCreate?.(channelStruct);
}
+40
View File
@@ -0,0 +1,40 @@
import { eventHandlers } from "../../bot.ts";
import {
ChannelCreatePayload,
ChannelTypes,
DiscordPayload,
} from "../../types/mod.ts";
import { cacheHandlers } from "../../cache.ts";
export async function handleChannelDelete(data: DiscordPayload) {
const payload = data.d as ChannelCreatePayload;
const cachedChannel = await cacheHandlers.get("channels", payload.id);
if (!cachedChannel) return;
if (cachedChannel.type === ChannelTypes.GUILD_VOICE && payload.guild_id) {
const guild = await cacheHandlers.get("guilds", payload.guild_id);
if (guild) {
return Promise.all(guild.voiceStates.map(async (vs, key) => {
if (vs.channelID !== payload.id) return;
// Since this channel was deleted all voice states for this channel should be deleted
guild.voiceStates.delete(key);
const member = await cacheHandlers.get("members", vs.userID);
if (!member) return;
eventHandlers.voiceChannelLeave?.(member, vs.channelID);
}));
}
}
await cacheHandlers.delete("channels", payload.id);
cacheHandlers.forEach("messages", (message) => {
if (message.channelID === payload.id) {
cacheHandlers.delete("messages", message.id);
}
});
eventHandlers.channelDelete?.(cachedChannel);
}
@@ -0,0 +1,19 @@
import { eventHandlers } from "../../bot.ts";
import {
DiscordChannelPinsUpdateEvent,
DiscordPayload,
} from "../../types/mod.ts";
import { cacheHandlers } from "../../cache.ts";
export async function handleChannelPinsUpdate(data: DiscordPayload) {
const payload = data.d as DiscordChannelPinsUpdateEvent;
const channel = await cacheHandlers.get("channels", payload.channel_id);
if (!channel) return;
const guild = payload.guild_id
? await cacheHandlers.get("guilds", payload.guild_id)
: undefined;
eventHandlers.channelPinsUpdate?.(channel, guild, payload.last_pin_timestamp);
}
+16
View File
@@ -0,0 +1,16 @@
import { eventHandlers } from "../../bot.ts";
import { ChannelCreatePayload, DiscordPayload } from "../../types/mod.ts";
import { structures } from "../../structures/mod.ts";
import { cacheHandlers } from "../../cache.ts";
export async function handleChannelUpdate(data: DiscordPayload) {
const payload = data.d as ChannelCreatePayload;
const cachedChannel = await cacheHandlers.get("channels", payload.id);
const channelStruct = await structures.createChannelStruct(payload);
await cacheHandlers.set("channels", channelStruct.id, channelStruct);
if (!cachedChannel) return;
eventHandlers.channelUpdate?.(channelStruct, cachedChannel);
}