add APPLICATION_COMMAND_* events

This commit is contained in:
DjDeveloperr 2021-01-27 01:45:47 +05:30
parent 11fc7e1e21
commit 87c15a9283
8 changed files with 103 additions and 6 deletions

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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]

View file

@ -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)

View file

@ -154,6 +154,9 @@ export class Client extends HarmonyEventEmitter<ClientEvents> {
return this.shards.list.get('0') as Gateway
}
applicationID?: string
applicationFlags?: number
constructor(options: ClientOptions = {}) {
super()
this._id = options.id

View file

@ -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<void> {
@ -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 */

View file

@ -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
}