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
+41
View File
@@ -0,0 +1,41 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordPayload, MessageCreateOptions } from "../../types/mod.ts";
import { structures } from "../../structures/mod.ts";
import { cacheHandlers } from "../../cache.ts";
export async function handleMessageCreate(data: DiscordPayload) {
const payload = data.d as MessageCreateOptions;
const channel = await cacheHandlers.get("channels", payload.channel_id);
if (channel) channel.lastMessageID = payload.id;
const guild = payload.guild_id
? await cacheHandlers.get("guilds", payload.guild_id)
: undefined;
if (payload.member && guild) {
// If in a guild cache the author as a member
const memberStruct = await structures.createMemberStruct(
{ ...payload.member, user: payload.author },
guild.id,
);
await cacheHandlers.set("members", memberStruct.id, memberStruct);
}
await Promise.all(payload.mentions.map(async (mention) => {
// Cache the member if its a valid member
if (mention.member && guild) {
const memberStruct = await structures.createMemberStruct(
{ ...mention.member, user: mention },
guild.id,
);
return cacheHandlers.set("members", memberStruct.id, memberStruct);
}
}));
const message = await structures.createMessageStruct(payload);
// Cache the message
await cacheHandlers.set("messages", payload.id, message);
eventHandlers.messageCreate?.(message);
}
+16
View File
@@ -0,0 +1,16 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordPayload, MessageDeletePayload } from "../../types/mod.ts";
import { cacheHandlers } from "../../cache.ts";
export async function handleMessageDelete(data: DiscordPayload) {
const payload = data.d as MessageDeletePayload;
const channel = await cacheHandlers.get("channels", payload.channel_id);
if (!channel) return;
eventHandlers.messageDelete?.(
{ id: payload.id, channel },
await cacheHandlers.get("messages", payload.id),
);
await cacheHandlers.delete("messages", payload.id);
}
@@ -0,0 +1,17 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordPayload, MessageDeleteBulkPayload } from "../../types/mod.ts";
import { cacheHandlers } from "../../cache.ts";
export async function handleMessageDeleteBulk(data: DiscordPayload) {
const payload = data.d as MessageDeleteBulkPayload;
const channel = await cacheHandlers.get("channels", payload.channel_id);
if (!channel) return;
return Promise.all(payload.ids.map(async (id) => {
eventHandlers.messageDelete?.(
{ id, channel },
await cacheHandlers.get("messages", id),
);
await cacheHandlers.delete("messages", id);
}));
}
@@ -0,0 +1,56 @@
import { botID, eventHandlers } from "../../bot.ts";
import { DiscordPayload, MessageReactionPayload } from "../../types/mod.ts";
import { structures } from "../../structures/mod.ts";
import { cacheHandlers } from "../../cache.ts";
export async function handleMessageReactionAdd(data: DiscordPayload) {
const payload = data.d as MessageReactionPayload;
const message = await cacheHandlers.get("messages", payload.message_id);
if (message) {
const reactionExisted = message.reactions?.find(
(reaction) =>
reaction.emoji.id === payload.emoji.id &&
reaction.emoji.name === payload.emoji.name,
);
if (reactionExisted) reactionExisted.count++;
else {
const newReaction = {
count: 1,
me: payload.user_id === botID,
emoji: { ...payload.emoji, id: payload.emoji.id || undefined },
};
message.reactions = message.reactions
? [...message.reactions, newReaction]
: [newReaction];
}
await cacheHandlers.set("messages", payload.message_id, message);
}
if (payload.member && payload.guild_id) {
const guild = await cacheHandlers.get("guilds", payload.guild_id);
if (guild) {
const memberStruct = await structures.createMemberStruct(
payload.member,
guild.id,
);
await cacheHandlers.set("members", memberStruct.id, memberStruct);
}
}
const uncachedOptions = {
...payload,
id: payload.message_id,
channelID: payload.channel_id,
guildID: payload.guild_id || "",
};
eventHandlers.reactionAdd?.(
uncachedOptions,
payload.emoji,
payload.user_id,
message,
);
}
@@ -0,0 +1,58 @@
import { botID, eventHandlers } from "../../bot.ts";
import { DiscordPayload, MessageReactionPayload } from "../../types/mod.ts";
import { structures } from "../../structures/mod.ts";
import { cacheHandlers } from "../../cache.ts";
export async function handleMessageReactionRemove(
data: DiscordPayload,
) {
const payload = data.d as MessageReactionPayload;
const message = await cacheHandlers.get("messages", payload.message_id);
if (message) {
const reactionExisted = message.reactions?.find(
(reaction) =>
reaction.emoji.id === payload.emoji.id &&
reaction.emoji.name === payload.emoji.name,
);
if (reactionExisted) reactionExisted.count--;
else {
const newReaction = {
count: 1,
me: payload.user_id === botID,
emoji: { ...payload.emoji, id: payload.emoji.id || undefined },
};
message.reactions = message.reactions
? [...message.reactions, newReaction]
: [newReaction];
}
await cacheHandlers.set("messages", payload.message_id, message);
}
if (payload.member && payload.guild_id) {
const guild = await cacheHandlers.get("guilds", payload.guild_id);
if (guild) {
const memberStruct = await structures.createMemberStruct(
payload.member,
guild.id,
);
await cacheHandlers.set("members", memberStruct.id, memberStruct);
}
}
const uncachedOptions = {
...payload,
id: payload.message_id,
channelID: payload.channel_id,
guildID: payload.guild_id,
};
eventHandlers.reactionRemove?.(
uncachedOptions,
payload.emoji,
payload.user_id,
message,
);
}
@@ -0,0 +1,18 @@
import { eventHandlers } from "../../bot.ts";
import { BaseMessageReactionPayload, DiscordPayload } from "../../types/mod.ts";
import { cacheHandlers } from "../../cache.ts";
export async function handleMessageReactionRemoveAll(
data: DiscordPayload,
) {
const payload = data.d as BaseMessageReactionPayload;
const message = await cacheHandlers.get("messages", payload.message_id);
if (message?.reactions) {
message.reactions = undefined;
await cacheHandlers.set("messages", payload.message_id, message);
}
eventHandlers.reactionRemoveAll?.(data.d as BaseMessageReactionPayload);
}
@@ -0,0 +1,29 @@
import { botID, eventHandlers } from "../../bot.ts";
import {
DiscordPayload,
MessageReactionRemoveEmojiPayload,
} from "../../types/mod.ts";
import { cacheHandlers } from "../../cache.ts";
export async function handleMessageReactionRemoveEmoji(
data: DiscordPayload,
) {
const payload = data.d as MessageReactionRemoveEmojiPayload;
const message = await cacheHandlers.get("messages", payload.message_id);
if (message?.reactions) {
message.reactions = message.reactions?.filter(
(reaction) =>
!(
reaction.emoji.id === payload.emoji.id &&
reaction.emoji.name === payload.emoji.name
),
);
await cacheHandlers.set("messages", payload.message_id, message);
}
eventHandlers.reactionRemoveEmoji?.(
data.d as MessageReactionRemoveEmojiPayload,
);
}
+36
View File
@@ -0,0 +1,36 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordPayload, MessageCreateOptions } from "../../types/mod.ts";
import { structures } from "../../structures/mod.ts";
import { cacheHandlers } from "../../cache.ts";
export async function handleMessageUpdate(data: DiscordPayload) {
const payload = data.d as MessageCreateOptions;
const channel = await cacheHandlers.get("channels", payload.channel_id);
if (!channel) return;
const cachedMessage = await cacheHandlers.get("messages", payload.id);
if (!cachedMessage) return;
const oldMessage = {
attachments: cachedMessage.attachments,
content: cachedMessage.content,
embeds: cachedMessage.embeds,
editedTimestamp: cachedMessage.editedTimestamp,
tts: cachedMessage.tts,
pinned: cachedMessage.pinned,
};
// Messages with embeds can trigger update but they wont have edited_timestamp
if (
!payload.edited_timestamp ||
(cachedMessage.content === payload.content)
) {
return;
}
const message = await structures.createMessageStruct(payload);
await cacheHandlers.set("messages", payload.id, message);
eventHandlers.messageUpdate?.(message, oldMessage);
}