From 051087f5bb861641800fd45760d68e622e686848 Mon Sep 17 00:00:00 2001 From: ayntee Date: Thu, 4 Feb 2021 10:59:42 +0400 Subject: [PATCH 1/3] feat(controllers/misc): add inviteCreate & inviteDelete listeners (#504) Closes #501 --- src/api/controllers/misc.ts | 44 +++++++++++++++++++++++++++++++++++++ src/api/controllers/mod.ts | 4 ++++ src/types/discord.ts | 40 ++++++++++++++++++++++++++++++++- src/types/options.ts | 6 +++++ 4 files changed, 93 insertions(+), 1 deletion(-) diff --git a/src/api/controllers/misc.ts b/src/api/controllers/misc.ts index 839d4e9e5..cd8fbd586 100644 --- a/src/api/controllers/misc.ts +++ b/src/api/controllers/misc.ts @@ -3,6 +3,8 @@ import { DiscordPayload, IntegrationCreateUpdateEvent, IntegrationDeleteEvent, + InviteCreateEvent, + InviteDeleteEvent, PresenceUpdatePayload, ReadyPayload, TypingStartPayload, @@ -234,3 +236,45 @@ export function handleInternalIntegrationDelete(data: DiscordPayload) { guildID, }); } + +export function handleInternalInviteCreate(payload: DiscordPayload) { + if (payload.t !== "INVITE_CREATE") return; + + const { + channel_id: channelID, + created_at: createdAt, + max_age: maxAge, + guild_id: guildID, + target_user: targetUser, + target_user_type: targetUserType, + max_uses: maxUses, + ...rest + } = payload.d as InviteCreateEvent; + + eventHandlers.inviteCreate?.({ + ...rest, + channelID, + guildID, + maxAge, + targetUser, + targetUserType, + maxUses, + createdAt, + }); +} + +export function handleInternalInviteDelete(payload: DiscordPayload) { + if (payload.t !== "INVITE_DELETE") return; + + const { + channel_id: channelID, + guild_id: guildID, + ...rest + } = payload.d as InviteDeleteEvent; + + eventHandlers.inviteDelete?.({ + ...rest, + channelID, + guildID, + }); +} diff --git a/src/api/controllers/mod.ts b/src/api/controllers/mod.ts index a1b73e9dc..ce97927b5 100644 --- a/src/api/controllers/mod.ts +++ b/src/api/controllers/mod.ts @@ -35,6 +35,8 @@ import { handleInternalIntegrationCreate, handleInternalIntegrationDelete, handleInternalIntegrationUpdate, + handleInternalInviteCreate, + handleInternalInviteDelete, handleInternalPresenceUpdate, handleInternalReady, handleInternalTypingStart, @@ -92,6 +94,8 @@ export let controllers = { INTEGRATION_CREATE: handleInternalIntegrationCreate, INTEGRATION_UPDATE: handleInternalIntegrationUpdate, INTEGRATION_DELETE: handleInternalIntegrationDelete, + INVITE_CREATE: handleInternalInviteCreate, + INVITE_DELETE: handleInternalInviteDelete, }; export type Controllers = typeof controllers; diff --git a/src/types/discord.ts b/src/types/discord.ts index d12a1c0c0..cf5efb255 100644 --- a/src/types/discord.ts +++ b/src/types/discord.ts @@ -53,7 +53,9 @@ export interface DiscordPayload { | "WEBHOOKS_UPDATE" | "INTEGRATION_CREATE" | "INTEGRATION_UPDATE" - | "INTEGRATION_DELETE"; + | "INTEGRATION_DELETE" + | "INVITE_CREATE" + | "INVITE_DELETE"; } export interface DiscordBotGatewayData { @@ -325,3 +327,39 @@ export interface IntegrationDeleteEvent { /** id of the bot/OAuth2 application for this discord integration */ "application_id"?: string; } + +/** https://discord.com/developers/docs/topics/gateway#invite-create */ +export interface InviteCreateEvent { + /** the channel the invite is for */ + "channel_id": string; + /** the unique invite code */ + code: string; + /** the time at which the invite was created */ + "created_at": string; + /** the guild of the invite */ + "guild_id"?: string; + /** the user that created the invite */ + inviter?: UserPayload; + /** how long the invite is valid for (in seconds) */ + "max_age": number; + /** the maximum number of times the invite can be used */ + "max_uses": number; + /** the target user for this invite */ + "target_user"?: Partial; + /** the type of user target for this invite */ + "target_user_type"?: number; + /** whether or not the invite is temporary (invited users will be kicked on disconnect unless they're assigned a role) */ + temporary: boolean; + /** how many times the invite has been used (always will be 0) */ + uses: number; +} + +/** https://discord.com/developers/docs/topics/gateway#invite-delete */ +export interface InviteDeleteEvent { + /** the channel of the invite */ + "channel_id": string; + /** the guild of the invite */ + "guild_id"?: string; + /** the unique invite code */ + code: string; +} diff --git a/src/types/options.ts b/src/types/options.ts index 0ec4008d2..770d031e2 100644 --- a/src/types/options.ts +++ b/src/types/options.ts @@ -10,6 +10,8 @@ import { Emoji, IntegrationCreateUpdateEvent, IntegrationDeleteEvent, + InviteCreateEvent, + InviteDeleteEvent, PresenceUpdatePayload, TypingStartPayload, VoiceStateUpdatePayload, @@ -232,6 +234,10 @@ export interface EventHandlers { integrationUpdate?: (data: Camelize) => unknown; /** Sent when an integration is deleted. */ integrationDelete?: (data: Camelize) => undefined; + /** Sent when a new invite to a channel is created. */ + inviteCreate?: (data: Camelize) => unknown; + /** Sent when an invite is deleted. */ + inviteDelete?: (data: Camelize) => unknown; } /** https://discord.com/developers/docs/topics/gateway#list-of-intents */ From db64efcba75bf727cb61f1481a5e015629a521db Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Sat, 6 Feb 2021 15:18:55 +0100 Subject: [PATCH 2/3] fix(bot): fix skipChecks logic (#508) --- src/bot.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 6cb39ead3..d649f6c8d 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -113,9 +113,10 @@ export async function startBigBrainBot(data: BigBrainBotConfig) { botGatewayData, identifyPayload, data.firstShardID, - data.lastShardID || botGatewayData.shards >= 25 - ? (data.firstShardID + 25) - : botGatewayData.shards, + data.lastShardID || + (botGatewayData.shards >= 25 + ? (data.firstShardID + 25) + : botGatewayData.shards), ); } From 043df68de84d6cd8461e45665a8e451e9369c7a1 Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Tue, 9 Feb 2021 19:00:29 +0100 Subject: [PATCH 3/3] fix(controllers/guild): guildDelete listener not emitted & guildUpdate listener caching irrelevant fields (#512) * Update guilds.ts * Update guilds.ts * fix my stupidness * remove the logs * delete guild after check --- src/api/controllers/guilds.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/controllers/guilds.ts b/src/api/controllers/guilds.ts index 151666bc1..30ed36442 100644 --- a/src/api/controllers/guilds.ts +++ b/src/api/controllers/guilds.ts @@ -52,8 +52,6 @@ export async function handleInternalGuildDelete(data: DiscordPayload) { } }); - await cacheHandlers.delete("guilds", payload.id); - if (payload.unavailable) { return cacheHandlers.set("unavailableGuilds", payload.id, Date.now()); } @@ -61,6 +59,8 @@ export async function handleInternalGuildDelete(data: DiscordPayload) { const guild = await cacheHandlers.get("guilds", payload.id); if (!guild) return; + await cacheHandlers.delete("guilds", payload.id); + eventHandlers.guildDelete?.(guild); } @@ -102,7 +102,7 @@ export async function handleInternalGuildUpdate(data: DiscordPayload) { } }).filter((change) => change) as GuildUpdateChange[]; - await cacheHandlers.set("guilds", payload.id, { ...cachedGuild, ...changes }); + await cacheHandlers.set("guilds", payload.id, cachedGuild); eventHandlers.guildUpdate?.(cachedGuild, changes); }