harmony/src/structures/slash.ts

76 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-04-04 09:29:56 +00:00
import type { Client } from '../client/mod.ts'
2021-04-22 03:48:45 +00:00
import { InteractionPayload } from '../types/interactions.ts'
2020-12-10 04:36:36 +00:00
import {
2021-02-10 12:29:21 +00:00
InteractionApplicationCommandData,
InteractionApplicationCommandOption,
2021-02-12 11:37:38 +00:00
SlashCommandOptionType
2021-04-22 03:48:45 +00:00
} from '../types/slashCommands.ts'
2021-04-04 09:29:56 +00:00
import type { Dict } from '../utils/dict.ts'
import type { Guild } from './guild.ts'
import type { GuildTextChannel } from './guildTextChannel.ts'
import type { Member } from './member.ts'
import type { Role } from './role.ts'
import type { TextChannel } from './textChannel.ts'
2020-12-10 06:55:52 +00:00
import { User } from './user.ts'
2021-04-22 03:48:45 +00:00
import {
InteractionUser,
InteractionChannel,
Interaction
} from './interactions.ts'
2021-02-12 11:37:38 +00:00
export interface InteractionApplicationCommandResolved {
users: Dict<InteractionUser>
members: Dict<Member>
channels: Dict<InteractionChannel>
roles: Dict<Role>
}
2021-04-22 03:48:45 +00:00
export class SlashCommandInteraction extends Interaction {
2021-02-17 08:55:04 +00:00
/** Data sent with Interaction. Only applies to Application Command */
2021-04-22 03:48:45 +00:00
data: InteractionApplicationCommandData
2021-02-17 08:55:04 +00:00
/** Resolved data for Snowflakes in Slash Command Arguments */
2021-02-12 11:37:38 +00:00
resolved: InteractionApplicationCommandResolved
2020-12-10 04:36:36 +00:00
2020-12-10 06:55:52 +00:00
constructor(
client: Client,
data: InteractionPayload,
others: {
2021-02-10 12:29:21 +00:00
channel?: TextChannel | GuildTextChannel
guild?: Guild
member?: Member
user: User
2021-02-12 11:37:38 +00:00
resolved: InteractionApplicationCommandResolved
2020-12-10 06:55:52 +00:00
}
) {
2021-04-22 03:48:45 +00:00
super(client, data, others)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
this.data = data.data as InteractionApplicationCommandData
2021-02-12 11:37:38 +00:00
this.resolved = others.resolved
2020-12-10 06:55:52 +00:00
}
2021-02-10 12:29:21 +00:00
/** Name of the Command Used (may change with future additions to Interactions!) */
2021-04-22 03:48:45 +00:00
get name(): string {
return this.data.name
2020-12-10 04:36:36 +00:00
}
2021-02-10 12:29:21 +00:00
get options(): InteractionApplicationCommandOption[] {
2021-04-22 03:48:45 +00:00
return this.data.options ?? []
2020-12-16 10:42:52 +00:00
}
2021-02-10 12:29:21 +00:00
/** Get an option by name */
2021-02-12 11:37:38 +00:00
option<T>(name: string): T {
const op = this.options.find((e) => e.name === name)
if (op === undefined || op.value === undefined) return undefined as any
2021-03-12 09:35:43 +00:00
if (op.type === SlashCommandOptionType.USER) {
2021-03-12 09:31:14 +00:00
const u: InteractionUser = this.resolved.users[op.value] as any
2021-03-26 06:58:06 +00:00
if (this.resolved.members[op.value] !== undefined)
u.member = this.resolved.members[op.value]
2021-03-12 09:42:38 +00:00
return u as any
2021-03-12 09:35:43 +00:00
} else if (op.type === SlashCommandOptionType.ROLE)
2021-02-12 11:37:38 +00:00
return this.resolved.roles[op.value] as any
else if (op.type === SlashCommandOptionType.CHANNEL)
return this.resolved.channels[op.value] as any
else return op.value
2020-12-11 10:17:05 +00:00
}
2020-12-10 04:36:36 +00:00
}