From cca34edaf9cf58974385504fba77b0b99d29b97f Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Thu, 21 Oct 2021 18:01:24 +0000 Subject: [PATCH 1/2] integration and interaction handlers --- src/bot.ts | 17 +++- .../integrations/INTEGRATION_CREATE.ts | 10 +- .../integrations/INTEGRATION_DELETE.ts | 13 ++- .../integrations/INTEGRATION_UPDATE.ts | 10 +- .../interactions/INTERACTION_CREATE.ts | 54 +---------- src/transformers/integration.ts | 91 +++++++++++++++++++ src/transformers/interaction.ts | 43 +++++++++ src/transformers/message.ts | 7 +- 8 files changed, 182 insertions(+), 63 deletions(-) create mode 100644 src/transformers/integration.ts create mode 100644 src/transformers/interaction.ts diff --git a/src/bot.ts b/src/bot.ts index 9b5140c25..5f2c8d4a0 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -67,7 +67,7 @@ import { iconBigintToHash, iconHashToBigInt } from "./util/hash.ts"; import { validateLength } from "./util/validate_length.ts"; import { processGlobalQueue } from "./rest/process_global_queue.ts"; import { ChannelPinsUpdate } from "./types/channels/channel_pins_update.ts"; -import { ApplicationCommandTypes, Emoji } from "./types/mod.ts"; +import { ApplicationCommandTypes, Emoji, IntegrationCreateUpdate } from "./types/mod.ts"; import { ApplicationCommandOption } from "./types/mod.ts"; import { handleGuildLoaded } from "./handlers/guilds/GUILD_LOADED_DD.ts"; import { @@ -119,6 +119,8 @@ import { handleIntegrationUpdate, handleIntegrationDelete, } from "./handlers/mod.ts"; +import { DiscordenoInteraction, transformInteraction } from "./transformers/interaction.ts"; +import { DiscordenoIntegration, transformIntegration } from "./transformers/integration.ts"; export async function createBot(options: CreateBotOptions) { return { @@ -227,9 +229,12 @@ export function createEventHandlers(events: Partial): EventHandle function ignore() {} return { - channelCreate: events.channelCreate ?? ignore, debug: events.debug ?? ignore, dispatchRequirements: events.dispatchRequirements ?? ignore, + integrationCreate: events.integrationCreate ?? ignore, + integrationDelete: events.integrationDelete ?? ignore, + interactionCreate: events.interactionCreate ?? ignore, + channelCreate: events.channelCreate ?? ignore, voiceChannelLeave: events.voiceChannelLeave ?? ignore, channelDelete: events.channelDelete ?? ignore, channelPinsUpdate: events.channelPinsUpdate ?? ignore, @@ -470,6 +475,8 @@ export interface Transformers { message: typeof transformMessage; role: typeof transformRole; voiceState: typeof transformVoiceState; + interaction: typeof transformInteraction; + integration: typeof transformIntegration, } export function createTransformers(options: Partial) { @@ -482,6 +489,7 @@ export function createTransformers(options: Partial) { message: options.message || transformMessage, role: options.role || transformRole, voiceState: options.voiceState || transformVoiceState, + integration: options.integration || transformIntegration, }; } @@ -591,6 +599,10 @@ export interface GatewayManager { export interface EventHandlers { debug: (text: string) => unknown; + interactionCreate: (bot: Bot, interaction: DiscordenoInteraction) => any; + integrationCreate: (bot: Bot, integration: DiscordenoIntegration) => any; + integrationDelete: (bot: Bot, payload: { id: bigint; guildId: bigint; applicationId?: bigint }) => any; + integrationsUpdate: (bot: Bot, integration: DiscordenoIntegration) => any; channelCreate: (bot: Bot, channel: DiscordenoChannel) => any; dispatchRequirements: (bot: Bot, data: GatewayPayload, shardId: number) => any; voiceChannelLeave: (bot: Bot, voiceState: DiscordenoVoiceState, channel: DiscordenoChannel) => any; @@ -643,7 +655,6 @@ export interface EventHandlers { guildCreate: (bot: Bot, guild: DiscordenoGuild) => any; guildDelete: (bot: Bot, id: bigint, guild?: DiscordenoGuild) => any; guildUpdate: (bot: Bot, guild: DiscordenoGuild, cachedGuild?: DiscordenoGuild) => any; - integrationsUpdate: (bot: Bot, data: { guildId: bigint }) => any; raw: (bot: Bot, data: GatewayPayload, shardId: number) => any; } diff --git a/src/handlers/integrations/INTEGRATION_CREATE.ts b/src/handlers/integrations/INTEGRATION_CREATE.ts index 47776f8c8..c298ddf46 100644 --- a/src/handlers/integrations/INTEGRATION_CREATE.ts +++ b/src/handlers/integrations/INTEGRATION_CREATE.ts @@ -1,7 +1,11 @@ -import { eventHandlers } from "../../bot.ts"; +import { Bot } from "../../bot.ts"; import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import type { IntegrationCreateUpdate } from "../../types/integrations/integration_create_update.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; -export function handleIntegrationCreate(data: DiscordGatewayPayload) { - eventHandlers.integrationCreate?.(data.d as IntegrationCreateUpdate); +export function handleIntegrationCreate(bot: Bot, data: DiscordGatewayPayload) { + bot.events.integrationCreate( + bot, + bot.transformers.integration(bot, data.d as SnakeCasedPropertiesDeep) + ); } diff --git a/src/handlers/integrations/INTEGRATION_DELETE.ts b/src/handlers/integrations/INTEGRATION_DELETE.ts index 6fc85523e..aaf3f93ab 100644 --- a/src/handlers/integrations/INTEGRATION_DELETE.ts +++ b/src/handlers/integrations/INTEGRATION_DELETE.ts @@ -1,7 +1,14 @@ -import { eventHandlers } from "../../bot.ts"; +import { Bot } from "../../bot.ts"; import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import type { IntegrationDelete } from "../../types/integrations/integration_delete.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; -export function handleIntegrationDelete(data: DiscordGatewayPayload) { - eventHandlers.integrationDelete?.(data.d as IntegrationDelete); +export function handleIntegrationDelete(bot: Bot, data: DiscordGatewayPayload) { + const payload = data.d as SnakeCasedPropertiesDeep; + + bot.events.integrationDelete(bot, { + id: bot.transformers.snowflake(payload.id), + guildId: bot.transformers.snowflake(payload.guild_id), + applicationId: payload.application_id ? bot.transformers.snowflake(payload.application_id) : undefined, + }); } diff --git a/src/handlers/integrations/INTEGRATION_UPDATE.ts b/src/handlers/integrations/INTEGRATION_UPDATE.ts index 1680110be..a4391cb8b 100644 --- a/src/handlers/integrations/INTEGRATION_UPDATE.ts +++ b/src/handlers/integrations/INTEGRATION_UPDATE.ts @@ -1,7 +1,11 @@ -import { eventHandlers } from "../../bot.ts"; +import { Bot } from "../../bot.ts"; import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import type { IntegrationCreateUpdate } from "../../types/integrations/integration_create_update.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; -export function handleIntegrationUpdate(data: DiscordGatewayPayload) { - eventHandlers.integrationUpdate?.(data.d as IntegrationCreateUpdate); +export function handleIntegrationUpdate(bot: Bot, data: DiscordGatewayPayload) { + bot.events.integrationsUpdate( + bot, + bot.transformers.integration(bot, data.d as SnakeCasedPropertiesDeep) + ); } diff --git a/src/handlers/interactions/INTERACTION_CREATE.ts b/src/handlers/interactions/INTERACTION_CREATE.ts index bead22709..56aac522b 100644 --- a/src/handlers/interactions/INTERACTION_CREATE.ts +++ b/src/handlers/interactions/INTERACTION_CREATE.ts @@ -1,52 +1,8 @@ -import { eventHandlers } from "../../bot.ts"; -import { cacheHandlers } from "../../cache.ts"; -import { createDiscordenoMessage, DiscordenoMessage } from "../../structures/message.ts"; -import { structures } from "../../structures/mod.ts"; +import { Bot } from "../../bot.ts"; import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; -import type { BigInteraction, Interaction } from "../../types/interactions/interaction.ts"; -import type { GuildMemberWithUser } from "../../types/members/guild_member.ts"; -import { snowflakeToBigint } from "../../util/bigint.ts"; +import type { Interaction } from "../../types/interactions/interaction.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; -export async function handleInteractionCreate(data: DiscordGatewayPayload) { - const basePayload = data.d as Interaction; - - const payload = { - ...basePayload, - id: snowflakeToBigint(basePayload.id), - applicationId: snowflakeToBigint(basePayload.applicationId), - guildId: basePayload.guildId ? snowflakeToBigint(basePayload.guildId) : undefined, - channelId: basePayload.channelId ? snowflakeToBigint(basePayload.channelId) : undefined, - member: basePayload.member - ? { - ...basePayload.member, - roles: basePayload.member.roles.map((id) => snowflakeToBigint(id)), - user: { - ...basePayload.member.user, - id: snowflakeToBigint(basePayload.member.user.id), - }, - } - : undefined, - user: basePayload.user - ? { - ...basePayload.user, - id: snowflakeToBigint(basePayload.user.id), - } - : undefined, - message: basePayload.message ? createDiscordenoMessage(basePayload.message) : undefined, - } as BigInteraction; - - if (payload.member) payload.user = payload.member.user; - - const discordenoMember = payload.guildId && basePayload.member - ? await structures.createDiscordenoMember(basePayload.member, payload.guildId) - : undefined; - - if (discordenoMember) { - await cacheHandlers.set("members", discordenoMember.id, discordenoMember); - eventHandlers.interactionGuildCreate?.(payload, discordenoMember); - } else { - eventHandlers.interactionDMCreate?.(payload); - } - - eventHandlers.interactionCreate?.(payload, discordenoMember); +export async function handleInteractionCreate(bot: Bot, data: DiscordGatewayPayload) { + bot.events.interactionCreate(bot, bot.transformers.interaction(bot, data.d as SnakeCasedPropertiesDeep)); } diff --git a/src/transformers/integration.ts b/src/transformers/integration.ts new file mode 100644 index 000000000..654903767 --- /dev/null +++ b/src/transformers/integration.ts @@ -0,0 +1,91 @@ +import { Bot } from "../bot.ts"; +import { IntegrationCreateUpdate } from "../types/integrations/integration_create_update.ts"; +import { DiscordIntegrationExpireBehaviors } from "../types/mod.ts"; +import { SnakeCasedPropertiesDeep } from "../types/util.ts"; +import { DiscordenoUser } from "./member.ts"; + +export function transformIntegration(bot: Bot, payload: SnakeCasedPropertiesDeep) { + return { + guildId: bot.transformers.snowflake(payload.guild_id), + id: bot.transformers.snowflake(payload.id), + name: payload.name, + type: payload.type, + enabled: payload.enabled, + syncing: payload.syncing, + roleId: payload.role_id ? bot.transformers.snowflake(payload.role_id) : undefined, + enableEmoticons: payload.enable_emoticons, + expireBehavior: payload.expire_behavior, + expireGracePeriod: payload.expire_grace_period, + user: payload.user ? bot.transformers.user(bot, payload.user) : undefined, + account: { + id: bot.transformers.snowflake(payload.account.id), + name: payload.account.name, + }, + syncedAt: payload.synced_at ? Date.parse(payload.synced_at) : undefined, + subscriberCount: payload.subscriber_count, + revoked: payload.revoked, + application: payload.application + ? { + id: bot.transformers.snowflake(payload.application.id), + name: payload.application.name, + icon: payload.application.icon ? bot.utils.iconHashToBigInt(payload.application.icon) : undefined, + description: payload.application.description, + summary: payload.application.summary, + bot: payload.application.bot ? bot.transformers.user(bot, payload.application.bot) : undefined, + } + : undefined, + }; +} + +export interface DiscordenoIntegration { + /** The guild id for where this integration is location. */ + guildId: bigint; + /** Integration Id */ + id: bigint; + /** Integration name */ + name: string; + /** Integration type (twitch, youtube or discord) */ + type: "twitch" | "youtube" | "discord"; + /** Is this integration enabled */ + enabled: boolean; + /** Is this integration syncing */ + syncing?: boolean; + /** Role Id that this integration uses for "subscribers" */ + roleId?: bigint; + /** Whether emoticons should be synced for this integration (twitch only currently) */ + enableEmoticons?: boolean; + /** The behavior of expiring subscribers */ + expireBehavior?: DiscordIntegrationExpireBehaviors; + /** The grace period (in days) before expiring subscribers */ + expireGracePeriod?: number; + /** User for this integration */ + user?: DiscordenoUser; + /** Integration account information */ + account: { + /** Id of the account */ + id: bigint; + /** Name of the account */ + name: string; + }; + /** When this integration was last synced */ + syncedAt?: number; + /** How many subscribers this integration has */ + subscriberCount?: number; + /** Has this integration been revoked */ + revoked?: boolean; + /** The bot/OAuth2 application for discord integrations */ + application?: { + /** The id of the app */ + id: bigint; + /** The name of the app */ + name: string; + /** the icon hash of the app */ + icon?: bigint; + /** The description of the app */ + description: string; + /** The summary of the app */ + summary: string; + /** The bot associated with this application */ + bot?: DiscordenoUser; + }; +} diff --git a/src/transformers/interaction.ts b/src/transformers/interaction.ts new file mode 100644 index 000000000..5a1b29263 --- /dev/null +++ b/src/transformers/interaction.ts @@ -0,0 +1,43 @@ +import { Bot } from "../bot.ts"; +import { Interaction, Role } from "../types/mod.ts"; +import { SnakeCasedPropertiesDeep } from "../types/util.ts"; +import { DiscordenoMember, DiscordenoUser } from "./member.ts"; +import { DiscordenoMessage } from "./message.ts"; + +export function transformInteraction(bot: Bot, payload: SnakeCasedPropertiesDeep): DiscordenoInteraction { + const guildId = payload.guild_id ? bot.transformers.snowflake(payload.guild_id) : undefined; + + return { + // UNTRANSFORMED STUFF HERE + type: payload.type, + token: payload.token, + version: payload.version, + + // TRANSFORMED STUFF BELOW + guildId, + id: bot.transformers.snowflake(payload.id), + applicationId: bot.transformers.snowflake(payload.application_id), + user: bot.transformers.user(bot, payload.member?.user || payload.user!), + message: payload.message ? bot.transformers.message(bot, payload.message) : undefined, + channelId: payload.channel_id ? bot.transformers.snowflake(payload.channel_id) : undefined, + member: payload.member && guildId ? bot.transformers.member(bot, payload.member, guildId) : undefined, + }; +} + +export interface DiscordenoInteraction + extends Omit { + /** Id of the interaction */ + id: bigint; + /** Id of the application this interaction is for */ + applicationId: bigint; + /** The guild it was sent from */ + guildId?: bigint; + /** The channel it was sent from */ + channelId?: bigint; + /** Guild member data for the invoking user, including permissions */ + member?: DiscordenoMember; + /** User object for the invoking user, if invoked in a DM */ + user: DiscordenoUser; + /** For the message the button was attached to */ + message?: DiscordenoMessage; +} diff --git a/src/transformers/message.ts b/src/transformers/message.ts index e441ab264..90ad8dc36 100644 --- a/src/transformers/message.ts +++ b/src/transformers/message.ts @@ -3,7 +3,7 @@ import { Message } from "../types/messages/message.ts"; import { CHANNEL_MENTION_REGEX } from "../util/constants.ts"; import { SnakeCasedPropertiesDeep } from "../types/util.ts"; -export function transformMessage(bot: Bot, data: SnakeCasedPropertiesDeep) { +export function transformMessage(bot: Bot, data: SnakeCasedPropertiesDeep): DiscordenoMessage { return { // UNTRANSFORMED STUFF HERE content: data.content || "", @@ -71,6 +71,9 @@ export interface DiscordenoMessage | "author" | "applicationId" | "thread" + | "tts" + | "pinned" + | "mentionEveryone" > { id: bigint; /** Whether or not this message was sent by a bot */ @@ -83,7 +86,7 @@ export interface DiscordenoMessage // For better user experience /** Id of the guild which the massage has been send in. "0n" if it a DM */ - guildId: bigint; + guildId?: bigint; /** id of the channel the message was sent in */ channelId: bigint; /** If the message is generated by a webhook, this is the webhook's id */ From 39658ddc7aca584bca79404b8550f57cd6278c46 Mon Sep 17 00:00:00 2001 From: Skillz4Killz Date: Thu, 21 Oct 2021 18:01:58 +0000 Subject: [PATCH 2/2] change: prettier code --- src/bot.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bot.ts b/src/bot.ts index 5f2c8d4a0..8e7c25655 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -476,7 +476,7 @@ export interface Transformers { role: typeof transformRole; voiceState: typeof transformVoiceState; interaction: typeof transformInteraction; - integration: typeof transformIntegration, + integration: typeof transformIntegration; } export function createTransformers(options: Partial) {