2020-12-10 06:55:52 +00:00
|
|
|
import { Guild } from '../structures/guild.ts'
|
|
|
|
import { Interaction } from '../structures/slash.ts'
|
|
|
|
import {
|
|
|
|
APPLICATION_COMMAND,
|
|
|
|
APPLICATION_COMMANDS,
|
|
|
|
APPLICATION_GUILD_COMMAND,
|
|
|
|
APPLICATION_GUILD_COMMANDS
|
|
|
|
} from '../types/endpoint.ts'
|
|
|
|
import {
|
2020-12-12 12:27:35 +00:00
|
|
|
InteractionType,
|
2020-12-10 06:55:52 +00:00
|
|
|
SlashCommandOption,
|
|
|
|
SlashCommandPartial,
|
|
|
|
SlashCommandPayload
|
|
|
|
} from '../types/slash.ts'
|
|
|
|
import { Collection } from '../utils/collection.ts'
|
|
|
|
import { Client } from './client.ts'
|
2020-12-20 09:45:49 +00:00
|
|
|
import { RESTManager } from './rest.ts'
|
2020-12-10 06:55:52 +00:00
|
|
|
|
|
|
|
export class SlashCommand {
|
2020-12-10 09:10:00 +00:00
|
|
|
slash: SlashCommandsManager
|
2020-12-10 06:55:52 +00:00
|
|
|
id: string
|
|
|
|
applicationID: string
|
|
|
|
name: string
|
|
|
|
description: string
|
|
|
|
options: SlashCommandOption[]
|
2020-12-10 09:10:00 +00:00
|
|
|
_guild?: string
|
2020-12-10 06:55:52 +00:00
|
|
|
|
2020-12-10 09:10:00 +00:00
|
|
|
constructor(manager: SlashCommandsManager, data: SlashCommandPayload) {
|
|
|
|
this.slash = manager
|
2020-12-10 06:55:52 +00:00
|
|
|
this.id = data.id
|
|
|
|
this.applicationID = data.application_id
|
|
|
|
this.name = data.name
|
|
|
|
this.description = data.description
|
2020-12-16 10:42:52 +00:00
|
|
|
this.options = data.options ?? []
|
2020-12-10 06:55:52 +00:00
|
|
|
}
|
2020-12-10 09:10:00 +00:00
|
|
|
|
|
|
|
async delete(): Promise<void> {
|
|
|
|
await this.slash.delete(this.id, this._guild)
|
|
|
|
}
|
|
|
|
|
|
|
|
async edit(data: SlashCommandPartial): Promise<void> {
|
|
|
|
await this.slash.edit(this.id, data, this._guild)
|
|
|
|
}
|
2020-12-10 06:55:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 09:10:00 +00:00
|
|
|
export class SlashCommandsManager {
|
2020-12-10 06:55:52 +00:00
|
|
|
slash: SlashClient
|
|
|
|
|
2020-12-20 09:45:49 +00:00
|
|
|
get rest(): RESTManager {
|
|
|
|
return this.slash.rest
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(client: SlashClient) {
|
|
|
|
this.slash = client
|
2020-12-10 06:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Get all Global Slash Commands */
|
|
|
|
async all(): Promise<Collection<string, SlashCommand>> {
|
|
|
|
const col = new Collection<string, SlashCommand>()
|
|
|
|
|
2020-12-20 09:45:49 +00:00
|
|
|
const res = (await this.rest.get(
|
|
|
|
APPLICATION_COMMANDS(this.slash.getID())
|
2020-12-10 06:55:52 +00:00
|
|
|
)) as SlashCommandPayload[]
|
|
|
|
if (!Array.isArray(res)) return col
|
|
|
|
|
|
|
|
for (const raw of res) {
|
2020-12-10 09:10:00 +00:00
|
|
|
const cmd = new SlashCommand(this, raw)
|
|
|
|
col.set(raw.id, cmd)
|
2020-12-10 06:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return col
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get a Guild's Slash Commands */
|
|
|
|
async guild(
|
|
|
|
guild: Guild | string
|
|
|
|
): Promise<Collection<string, SlashCommand>> {
|
|
|
|
const col = new Collection<string, SlashCommand>()
|
|
|
|
|
2020-12-20 09:45:49 +00:00
|
|
|
const res = (await this.rest.get(
|
2020-12-10 06:55:52 +00:00
|
|
|
APPLICATION_GUILD_COMMANDS(
|
2020-12-20 09:45:49 +00:00
|
|
|
this.slash.getID(),
|
2020-12-10 06:55:52 +00:00
|
|
|
typeof guild === 'string' ? guild : guild.id
|
|
|
|
)
|
|
|
|
)) as SlashCommandPayload[]
|
|
|
|
if (!Array.isArray(res)) return col
|
|
|
|
|
|
|
|
for (const raw of res) {
|
2020-12-10 09:10:00 +00:00
|
|
|
const cmd = new SlashCommand(this, raw)
|
|
|
|
cmd._guild = typeof guild === 'string' ? guild : guild.id
|
|
|
|
col.set(raw.id, cmd)
|
2020-12-10 06:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return col
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create a Slash Command (global or Guild) */
|
|
|
|
async create(
|
|
|
|
data: SlashCommandPartial,
|
|
|
|
guild?: Guild | string
|
|
|
|
): Promise<SlashCommand> {
|
2020-12-20 09:45:49 +00:00
|
|
|
const payload = await this.rest.post(
|
2020-12-10 06:55:52 +00:00
|
|
|
guild === undefined
|
2020-12-20 09:45:49 +00:00
|
|
|
? APPLICATION_COMMANDS(this.slash.getID())
|
2020-12-10 06:55:52 +00:00
|
|
|
: APPLICATION_GUILD_COMMANDS(
|
2020-12-20 09:45:49 +00:00
|
|
|
this.slash.getID(),
|
2020-12-10 06:55:52 +00:00
|
|
|
typeof guild === 'string' ? guild : guild.id
|
|
|
|
),
|
|
|
|
data
|
|
|
|
)
|
|
|
|
|
2020-12-10 09:10:00 +00:00
|
|
|
const cmd = new SlashCommand(this, payload)
|
|
|
|
cmd._guild =
|
|
|
|
typeof guild === 'string' || guild === undefined ? guild : guild.id
|
|
|
|
|
|
|
|
return cmd
|
2020-12-10 06:55:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 09:10:00 +00:00
|
|
|
/** Edit a Slash Command (global or Guild) */
|
2020-12-10 06:55:52 +00:00
|
|
|
async edit(
|
|
|
|
id: string,
|
2020-12-10 09:10:00 +00:00
|
|
|
data: SlashCommandPartial,
|
|
|
|
guild?: Guild | string
|
|
|
|
): Promise<SlashCommandsManager> {
|
2020-12-20 09:45:49 +00:00
|
|
|
await this.rest.patch(
|
2020-12-10 06:55:52 +00:00
|
|
|
guild === undefined
|
2020-12-20 09:45:49 +00:00
|
|
|
? APPLICATION_COMMAND(this.slash.getID(), id)
|
2020-12-10 06:55:52 +00:00
|
|
|
: APPLICATION_GUILD_COMMAND(
|
2020-12-20 09:45:49 +00:00
|
|
|
this.slash.getID(),
|
2020-12-10 06:55:52 +00:00
|
|
|
typeof guild === 'string' ? guild : guild.id,
|
|
|
|
id
|
|
|
|
),
|
|
|
|
data
|
|
|
|
)
|
|
|
|
return this
|
|
|
|
}
|
2020-12-10 09:10:00 +00:00
|
|
|
|
|
|
|
/** Delete a Slash Command (global or Guild) */
|
|
|
|
async delete(
|
|
|
|
id: string,
|
|
|
|
guild?: Guild | string
|
|
|
|
): Promise<SlashCommandsManager> {
|
2020-12-20 09:45:49 +00:00
|
|
|
await this.rest.delete(
|
2020-12-10 09:10:00 +00:00
|
|
|
guild === undefined
|
2020-12-20 09:45:49 +00:00
|
|
|
? APPLICATION_COMMAND(this.slash.getID(), id)
|
2020-12-10 09:10:00 +00:00
|
|
|
: APPLICATION_GUILD_COMMAND(
|
2020-12-20 09:45:49 +00:00
|
|
|
this.slash.getID(),
|
2020-12-10 09:10:00 +00:00
|
|
|
typeof guild === 'string' ? guild : guild.id,
|
|
|
|
id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return this
|
|
|
|
}
|
2020-12-20 09:45:49 +00:00
|
|
|
|
|
|
|
/** Get a Slash Command (global or Guild) */
|
|
|
|
async get(id: string, guild?: Guild | string): Promise<SlashCommand> {
|
|
|
|
const data = await this.rest.get(
|
|
|
|
guild === undefined
|
|
|
|
? APPLICATION_COMMAND(this.slash.getID(), id)
|
|
|
|
: APPLICATION_GUILD_COMMAND(
|
|
|
|
this.slash.getID(),
|
|
|
|
typeof guild === 'string' ? guild : guild.id,
|
|
|
|
id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return new SlashCommand(this, data)
|
|
|
|
}
|
2020-12-10 09:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type SlashCommandHandlerCallback = (interaction: Interaction) => any
|
|
|
|
export interface SlashCommandHandler {
|
|
|
|
name: string
|
|
|
|
guild?: string
|
2020-12-16 10:00:13 +00:00
|
|
|
parent?: string
|
2020-12-16 13:05:26 +00:00
|
|
|
group?: string
|
2020-12-10 09:10:00 +00:00
|
|
|
handler: SlashCommandHandlerCallback
|
2020-12-10 06:55:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 09:45:49 +00:00
|
|
|
export interface SlashOptions {
|
|
|
|
id?: string | (() => string)
|
|
|
|
client?: Client
|
|
|
|
enabled?: boolean
|
|
|
|
token?: string
|
|
|
|
rest?: RESTManager
|
|
|
|
}
|
|
|
|
|
2020-12-10 06:55:52 +00:00
|
|
|
export class SlashClient {
|
2020-12-20 09:45:49 +00:00
|
|
|
id: string | (() => string)
|
|
|
|
client?: Client
|
|
|
|
token?: string
|
2020-12-10 06:55:52 +00:00
|
|
|
enabled: boolean = true
|
2020-12-10 09:10:00 +00:00
|
|
|
commands: SlashCommandsManager
|
|
|
|
handlers: SlashCommandHandler[] = []
|
2020-12-20 09:45:49 +00:00
|
|
|
rest: RESTManager
|
|
|
|
|
|
|
|
constructor(options: SlashOptions) {
|
|
|
|
let id = options.id
|
|
|
|
if (options.token !== undefined) id = atob(options.token?.split('.')[0])
|
|
|
|
if (id === undefined)
|
|
|
|
throw new Error('ID could not be found. Pass at least client or token')
|
|
|
|
this.id = id
|
|
|
|
this.client = options.client
|
|
|
|
this.token = options.token
|
|
|
|
this.commands = new SlashCommandsManager(this)
|
2020-12-10 06:55:52 +00:00
|
|
|
|
|
|
|
if (options !== undefined) {
|
|
|
|
this.enabled = options.enabled ?? true
|
|
|
|
}
|
|
|
|
|
2020-12-20 09:45:49 +00:00
|
|
|
if (this.client?._decoratedSlash !== undefined) {
|
2020-12-10 09:10:00 +00:00
|
|
|
this.client._decoratedSlash.forEach((e) => {
|
|
|
|
this.handlers.push(e)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-20 09:45:49 +00:00
|
|
|
this.rest =
|
|
|
|
options.client === undefined
|
|
|
|
? options.rest === undefined
|
|
|
|
? new RESTManager({
|
|
|
|
token: this.token
|
|
|
|
})
|
|
|
|
: options.rest
|
|
|
|
: options.client.rest
|
|
|
|
|
|
|
|
this.client?.on('interactionCreate', (interaction) =>
|
2020-12-16 13:05:26 +00:00
|
|
|
this._process(interaction)
|
2020-12-10 06:55:52 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-20 09:45:49 +00:00
|
|
|
getID(): string {
|
|
|
|
return typeof this.id === 'string' ? this.id : this.id()
|
|
|
|
}
|
|
|
|
|
2020-12-10 09:10:00 +00:00
|
|
|
/** Adds a new Slash Command Handler */
|
2020-12-16 13:11:01 +00:00
|
|
|
handle(handler: SlashCommandHandler): SlashClient {
|
|
|
|
this.handlers.push(handler)
|
2020-12-10 06:55:52 +00:00
|
|
|
return this
|
|
|
|
}
|
2020-12-10 09:10:00 +00:00
|
|
|
|
2020-12-16 13:05:26 +00:00
|
|
|
private _getCommand(i: Interaction): SlashCommandHandler | undefined {
|
|
|
|
return this.handlers.find((e) => {
|
|
|
|
const hasGroupOrParent = e.group !== undefined || e.parent !== undefined
|
|
|
|
const groupMatched =
|
|
|
|
e.group !== undefined && e.parent !== undefined
|
|
|
|
? i.options
|
|
|
|
.find((o) => o.name === e.group)
|
|
|
|
?.options?.find((o) => o.name === e.name) !== undefined
|
|
|
|
: true
|
|
|
|
const subMatched =
|
|
|
|
e.group === undefined && e.parent !== undefined
|
|
|
|
? i.options.find((o) => o.name === e.name) !== undefined
|
|
|
|
: true
|
|
|
|
const nameMatched1 = e.name === i.name
|
|
|
|
const parentMatched = hasGroupOrParent ? e.parent === i.name : true
|
|
|
|
const nameMatched = hasGroupOrParent ? parentMatched : nameMatched1
|
|
|
|
|
|
|
|
const matched = groupMatched && subMatched && nameMatched
|
|
|
|
return matched
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-12 12:27:35 +00:00
|
|
|
/** Process an incoming Slash Command (interaction) */
|
2020-12-16 13:05:26 +00:00
|
|
|
private _process(interaction: Interaction): void {
|
2020-12-10 09:10:00 +00:00
|
|
|
if (!this.enabled) return
|
|
|
|
|
2020-12-12 12:27:35 +00:00
|
|
|
if (interaction.type !== InteractionType.APPLICATION_COMMAND) return
|
|
|
|
|
2020-12-16 13:05:26 +00:00
|
|
|
const cmd = this._getCommand(interaction)
|
2020-12-10 09:10:00 +00:00
|
|
|
|
|
|
|
if (cmd === undefined) return
|
|
|
|
|
|
|
|
cmd.handler(interaction)
|
|
|
|
}
|
2020-12-10 06:55:52 +00:00
|
|
|
}
|