From 87c15a9283ae5fd23e74e005fd9d7744968f1c05 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 27 Jan 2021 01:45:47 +0530 Subject: [PATCH] add APPLICATION_COMMAND_* events --- .../handlers/applicationCommandCreate.ts | 15 +++++++++ .../handlers/applicationCommandDelete.ts | 15 +++++++++ .../handlers/applicationCommandUpdate.ts | 15 +++++++++ src/gateway/handlers/index.ts | 11 ++++++- src/gateway/handlers/ready.ts | 6 ++++ src/models/client.ts | 3 ++ src/models/slashClient.ts | 33 ++++++++++++++++--- src/types/gateway.ts | 11 ++++++- 8 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 src/gateway/handlers/applicationCommandCreate.ts create mode 100644 src/gateway/handlers/applicationCommandDelete.ts create mode 100644 src/gateway/handlers/applicationCommandUpdate.ts diff --git a/src/gateway/handlers/applicationCommandCreate.ts b/src/gateway/handlers/applicationCommandCreate.ts new file mode 100644 index 0000000..6e38f62 --- /dev/null +++ b/src/gateway/handlers/applicationCommandCreate.ts @@ -0,0 +1,15 @@ +import { SlashCommand } from '../../models/slashClient.ts' +import { ApplicationCommandPayload } from '../../types/gateway.ts' +import { Gateway, GatewayEventHandler } from '../index.ts' + +export const applicationCommandCreate: GatewayEventHandler = async ( + gateway: Gateway, + d: ApplicationCommandPayload +) => { + const guild = + d.guild_id === undefined + ? undefined + : await gateway.client.guilds.get(d.guild_id) + const cmd = new SlashCommand(gateway.client.slash.commands, d, guild) + gateway.client.emit('slashCommandCreate', cmd) +} diff --git a/src/gateway/handlers/applicationCommandDelete.ts b/src/gateway/handlers/applicationCommandDelete.ts new file mode 100644 index 0000000..564f4fa --- /dev/null +++ b/src/gateway/handlers/applicationCommandDelete.ts @@ -0,0 +1,15 @@ +import { SlashCommand } from '../../models/slashClient.ts' +import { ApplicationCommandPayload } from '../../types/gateway.ts' +import { Gateway, GatewayEventHandler } from '../index.ts' + +export const applicationCommandDelete: GatewayEventHandler = async ( + gateway: Gateway, + d: ApplicationCommandPayload +) => { + const guild = + d.guild_id === undefined + ? undefined + : await gateway.client.guilds.get(d.guild_id) + const cmd = new SlashCommand(gateway.client.slash.commands, d, guild) + gateway.client.emit('slashCommandDelete', cmd) +} diff --git a/src/gateway/handlers/applicationCommandUpdate.ts b/src/gateway/handlers/applicationCommandUpdate.ts new file mode 100644 index 0000000..cdb352e --- /dev/null +++ b/src/gateway/handlers/applicationCommandUpdate.ts @@ -0,0 +1,15 @@ +import { SlashCommand } from '../../models/slashClient.ts' +import { ApplicationCommandPayload } from '../../types/gateway.ts' +import { Gateway, GatewayEventHandler } from '../index.ts' + +export const applicationCommandUpdate: GatewayEventHandler = async ( + gateway: Gateway, + d: ApplicationCommandPayload +) => { + const guild = + d.guild_id === undefined + ? undefined + : await gateway.client.guilds.get(d.guild_id) + const cmd = new SlashCommand(gateway.client.slash.commands, d, guild) + gateway.client.emit('slashCommandUpdate', cmd) +} diff --git a/src/gateway/handlers/index.ts b/src/gateway/handlers/index.ts index 1f4e326..9cc7cd4 100644 --- a/src/gateway/handlers/index.ts +++ b/src/gateway/handlers/index.ts @@ -63,11 +63,18 @@ import { CommandContext } from '../../models/command.ts' import { RequestMethods } from '../../models/rest.ts' import { PartialInvitePayload } from '../../types/invite.ts' import { GuildChannels } from '../../types/guild.ts' +import { applicationCommandCreate } from './applicationCommandCreate.ts' +import { applicationCommandDelete } from './applicationCommandDelete.ts' +import { applicationCommandUpdate } from './applicationCommandUpdate.ts' +import { SlashCommand } from '../../models/slashClient.ts' export const gatewayHandlers: { [eventCode in GatewayEvents]: GatewayEventHandler | undefined } = { READY: ready, + APPLICATION_COMMAND_CREATE: applicationCommandCreate, + APPLICATION_COMMAND_DELETE: applicationCommandDelete, + APPLICATION_COMMAND_UPDATE: applicationCommandUpdate, RECONNECT: reconnect, RESUMED: resume, CHANNEL_CREATE: channelCreate, @@ -394,7 +401,9 @@ export type ClientEvents = { guildMemberUpdateUncached: [member: Member] guildMemberRemoveUncached: [member: Member] channelUpdateUncached: [channel: GuildChannels] - + slashCommandCreate: [cmd: SlashCommand] + slashCommandUpdate: [cmd: SlashCommand] + slashCommandDelete: [cmd: SlashCommand] commandOwnerOnly: [ctx: CommandContext] commandGuildOnly: [ctx: CommandContext] commandDmOnly: [ctx: CommandContext] diff --git a/src/gateway/handlers/ready.ts b/src/gateway/handlers/ready.ts index 9e2b727..69bc857 100644 --- a/src/gateway/handlers/ready.ts +++ b/src/gateway/handlers/ready.ts @@ -8,6 +8,12 @@ export const ready: GatewayEventHandler = async ( d: Ready ) => { gateway.client.upSince = new Date() + + if ('application' in d) { + gateway.client.applicationID = d.application.id + gateway.client.applicationFlags = d.application.flags + } + await gateway.client.guilds.flush() await gateway.client.users.set(d.user.id, d.user) diff --git a/src/models/client.ts b/src/models/client.ts index b835586..6c8b872 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -154,6 +154,9 @@ export class Client extends HarmonyEventEmitter { return this.shards.list.get('0') as Gateway } + applicationID?: string + applicationFlags?: number + constructor(options: ClientOptions = {}) { super() this._id = options.id diff --git a/src/models/slashClient.ts b/src/models/slashClient.ts index 150c83b..4a64eae 100644 --- a/src/models/slashClient.ts +++ b/src/models/slashClient.ts @@ -27,15 +27,21 @@ export class SlashCommand { name: string description: string options: SlashCommandOption[] + guild?: Guild _guild?: string - constructor(manager: SlashCommandsManager, data: SlashCommandPayload) { + constructor( + manager: SlashCommandsManager, + data: SlashCommandPayload, + guild?: Guild + ) { this.slash = manager this.id = data.id this.applicationID = data.application_id this.name = data.name this.description = data.description this.options = data.options ?? [] + this.guild = guild } async delete(): Promise { @@ -236,8 +242,13 @@ export class SlashCommandsManager { ].commands.get()) as SlashCommandPayload[] if (!Array.isArray(res)) return col + const _guild = + typeof guild === 'object' + ? guild + : await this.slash.client?.guilds.get(guild) + for (const raw of res) { - const cmd = new SlashCommand(this, raw) + const cmd = new SlashCommand(this, raw, _guild) cmd._guild = typeof guild === 'string' ? guild : guild.id col.set(raw.id, cmd) } @@ -259,7 +270,14 @@ export class SlashCommandsManager { const payload = await route.post(data) - const cmd = new SlashCommand(this, payload) + const _guild = + typeof guild === 'object' + ? guild + : guild === undefined + ? undefined + : await this.slash.client?.guilds.get(guild) + + const cmd = new SlashCommand(this, payload, _guild) cmd._guild = typeof guild === 'string' || guild === undefined ? guild : guild.id @@ -310,7 +328,14 @@ export class SlashCommandsManager { const data = await route.get() - return new SlashCommand(this, data) + const _guild = + typeof guild === 'object' + ? guild + : guild === undefined + ? undefined + : await this.slash.client?.guilds.get(guild) + + return new SlashCommand(this, data, _guild) } /** Bulk Edit Global or Guild Slash Commands */ diff --git a/src/types/gateway.ts b/src/types/gateway.ts index bbd2e48..937a41b 100644 --- a/src/types/gateway.ts +++ b/src/types/gateway.ts @@ -11,6 +11,7 @@ import { ClientStatus } from './presence.ts' import { RolePayload } from './role.ts' +import { SlashCommandPayload } from './slash.ts' import { UserPayload } from './user.ts' /** @@ -106,7 +107,10 @@ export enum GatewayEvents { Voice_Server_Update = 'VOICE_SERVER_UPDATE', Voice_State_Update = 'VOICE_STATE_UPDATE', Webhooks_Update = 'WEBHOOKS_UPDATE', - Interaction_Create = 'INTERACTION_CREATE' + Interaction_Create = 'INTERACTION_CREATE', + Application_Command_Create = 'APPLICATION_COMMAND_CREATE', + Application_Command_Update = 'APPLICATION_COMMAND_UPDATE', + Application_Command_Delete = 'APPLICATION_COMMAND_DELETE' } export interface IdentityPayload { @@ -168,6 +172,7 @@ export interface Ready { guilds: [] session_id: string shard?: number[] + application: { id: string; flags: number } } export interface ChannelPinsUpdatePayload { @@ -344,3 +349,7 @@ export interface TypingStartGuildData { guild: Guild member: Member } + +export interface ApplicationCommandPayload extends SlashCommandPayload { + guild_id?: string +}