-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetupCacheEdits.ts
249 lines (198 loc) · 8.16 KB
/
setupCacheEdits.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { Bot } from "discordeno";
import {
DiscordChannel,
DiscordGuild,
DiscordGuildMemberAdd,
DiscordGuildMemberRemove,
DiscordUnavailableGuild,
DiscordGuildMemberUpdate,
DiscordGuildRoleUpdate,
DiscordMessage,
DiscordMessageReactionAdd,
DiscordMessageReactionRemove,
DiscordMessageReactionRemoveAll,
DiscordUser,
} from "discordeno/types";
import { BotWithProxyCache, ProxyCacheTypes } from "./index.js";
export const unavailablesGuilds = new Set<bigint>();
export function setupCacheEdits<B extends Bot>(bot: BotWithProxyCache<ProxyCacheTypes, B>) {
const {
GUILD_MEMBER_ADD,
GUILD_MEMBER_REMOVE,
MESSAGE_REACTION_ADD,
MESSAGE_REACTION_REMOVE,
MESSAGE_REACTION_REMOVE_ALL,
CHANNEL_UPDATE,
MESSAGE_UPDATE,
GUILD_UPDATE,
GUILD_ROLE_UPDATE,
GUILD_MEMBER_UPDATE,
USER_UPDATE,
} = bot.handlers;
bot.handlers.GUILD_MEMBER_ADD = async function (_, data, shardId) {
const payload = data.d as DiscordGuildMemberAdd;
const guildID = bot.transformers.snowflake(payload.guild_id);
const guild = await bot.cache.guilds.get(guildID, false);
// Update cache
if (guild) {
guild.memberCount++;
await bot.cache.guilds.set(guild);
} else {
await bot.cache.guilds.get(guildID);
}
GUILD_MEMBER_ADD(bot, data, shardId);
};
bot.handlers.GUILD_MEMBER_REMOVE = async function (_, data, shardId) {
const payload = data.d as DiscordGuildMemberRemove;
const guildID = bot.transformers.snowflake(payload.guild_id);
const guild = await bot.cache.guilds.get(guildID, false);
if (guild) {
guild.memberCount--;
bot.cache.guilds.set(guild);
} else {
await bot.cache.guilds.get(guildID);
}
GUILD_MEMBER_REMOVE(bot, data, shardId);
};
bot.handlers.MESSAGE_REACTION_ADD = async function (_, data, shardId) {
const payload = data.d as DiscordMessageReactionAdd;
const messageId = bot.transformers.snowflake(payload.message_id);
const message = await bot.cache.messages.get(messageId, undefined, undefined, false);
const emoji = bot.transformers.emoji(bot, payload.emoji);
// if the message is cached
if (message) {
const reactions = message.reactions?.map((r) => r.emoji.name);
const toSet = {
count: 1,
me: bot.transformers.snowflake(payload.user_id) === bot.id,
emoji: emoji,
};
// if theres no reaction add it
if (!message.reactions || !reactions) {
message.reactions = [toSet];
} else if (!reactions.includes(emoji.name)) {
message.reactions?.push(toSet);
} else {
// otherwise the reaction has already been added so +1 to the reaction count
const current = message.reactions?.[reactions.indexOf(emoji.name)];
// rewrite
if (current) {
current.count++;
}
}
bot.cache.messages.set(message);
} else {
await bot.cache.messages.get(messageId, BigInt(payload.channel_id), payload.guild_id ? BigInt(payload.guild_id) : undefined);
}
MESSAGE_REACTION_ADD(bot, data, shardId);
};
bot.handlers.MESSAGE_REACTION_REMOVE = async function (_, data, shardId) {
const payload = data.d as DiscordMessageReactionRemove;
const messageId = bot.transformers.snowflake(payload.message_id);
const message = await bot.cache.messages.get(messageId, undefined, undefined, false);
const emoji = bot.transformers.emoji(bot, payload.emoji);
// if the message is cached
if (message) {
const reactions = message.reactions?.map((r) => r.emoji.name);
if (reactions?.indexOf(emoji.name) !== undefined) {
const current = message.reactions?.[reactions.indexOf(emoji.name)];
if (current) {
if (current.count > 0) {
current.count--;
}
// delete when count is 0
if (current.count === 0) {
message.reactions?.splice(reactions?.indexOf(emoji.name), 1);
}
// when someone deleted a reaction that doesn't exist in the cache just pass
}
}
bot.cache.messages.set(message);
} else {
await bot.cache.messages.get(messageId, BigInt(payload.channel_id), payload.guild_id ? BigInt(payload.guild_id) : undefined);
}
MESSAGE_REACTION_REMOVE(bot, data, shardId);
};
bot.handlers.MESSAGE_REACTION_REMOVE_ALL = async function (_, data, shardId) {
const payload = data.d as DiscordMessageReactionRemoveAll;
const messageId = bot.transformers.snowflake(payload.message_id);
const message = await bot.cache.messages.get(messageId);
if (message) {
// when an admin deleted all the reactions of a message
message.reactions = undefined;
bot.cache.messages.set(message);
} else {
await bot.cache.messages.get(messageId, BigInt(payload.channel_id), payload.guild_id ? BigInt(payload.guild_id) : undefined);
}
MESSAGE_REACTION_REMOVE_ALL(bot, data, shardId);
};
bot.handlers.CHANNEL_UPDATE = async function (_, data, shardId) {
const payload = data.d as DiscordChannel;
//TODO: This transformer is wierd. Make it better. {channel: channel} is not necessary.
const channel = bot.transformers.channel(bot, { channel: payload });
const oldChannel = await bot.cache.channels.get(BigInt(payload.id), payload.guild_id ? BigInt(payload.guild_id) : undefined, false);
await bot.cache.channels.set(channel);
//Send the event.
bot.events.channelUpdateWithOldChannel(bot, oldChannel!, channel);
CHANNEL_UPDATE(bot, data, shardId);
};
bot.handlers.MESSAGE_UPDATE = async function (_, data, shardId) {
const payload = data.d as DiscordMessage;
const message = bot.transformers.message(bot, payload);
const oldMessage = await bot.cache.messages.get(BigInt(payload.id), undefined, undefined, false);
await bot.cache.messages.set(message);
//Send the event.
bot.events.messageUpdateWithOldMessage(bot, oldMessage!, message);
MESSAGE_UPDATE(bot, data, shardId);
};
bot.handlers.GUILD_UPDATE = async function (_, data, shardId) {
const payload = data.d as DiscordGuild;
const guild = bot.transformers.guild(bot, {
guild: payload,
shardId: shardId,
});
const oldGuild = await bot.cache.guilds.get(guild.id, false);
await bot.cache.guilds.set(guild);
//Send the event.
bot.events.guildUpdateWithOldGuild(bot, oldGuild!, guild);
GUILD_UPDATE(bot, data, shardId);
};
bot.handlers.GUILD_ROLE_UPDATE = async function (_, data, shardId) {
const payload = data.d as DiscordGuildRoleUpdate;
const role = bot.transformers.role(bot, {
role: payload.role,
guildId: BigInt(payload.guild_id),
});
const oldRole = await bot.cache.roles.get(BigInt(payload.role.id), BigInt(payload.guild_id), false);
await bot.cache.roles.set(role);
//Send the event.
bot.events.guildRoleUpdateWithOldRole(bot, oldRole!, role);
GUILD_ROLE_UPDATE(bot, data, shardId);
};
bot.handlers.GUILD_MEMBER_UPDATE = async function (_, data, shardId) {
const payload = data.d as DiscordGuildMemberUpdate;
const member = bot.transformers.member(bot, payload, BigInt(payload.guild_id), BigInt(payload.user.id));
const oldMember = await bot.cache.members.get(BigInt(payload.user.id), BigInt(payload.guild_id), false);
await bot.cache.members.set(member);
//Send the event.
bot.events.guildMemberUpdateWithOldMember(bot, oldMember!, member);
GUILD_MEMBER_UPDATE(bot, data, shardId);
};
bot.handlers.USER_UPDATE = async function (_, data, shardId) {
const payload = data.d as DiscordUser;
const user = bot.transformers.user(bot, payload);
const oldUser = await bot.cache.users.get(BigInt(payload.id), false);
await bot.cache.users.set(user);
bot.events.userUpdateWithOldUser(bot, oldUser!, user);
USER_UPDATE(bot, data, shardId);
};
bot.handlers.GUILD_UPDATE = function (_, data, shardId) {
const payload = data.d as DiscordUnavailableGuild;
const guildID = bot.transformers.snowflake(payload.id);
// If Guild isn't available push to Set
if (payload.unavailable) unavailablesGuilds.add(guildID);
// otherwise remove from Set
else unavailablesGuilds.delete(guildID);
GUILD_UPDATE(bot, data, shardId);
};
}