Merge pull request #96 from DjDeveloperr/slash

add APPLICATION_COMMAND_* events
This commit is contained in:
DjDeveloper 2021-01-27 18:52:44 +05:30 committed by GitHub
commit be3a793fa1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
}