harmony/src/structures/slash.ts

85 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-12-10 04:36:36 +00:00
import { Client } from '../models/client.ts'
import { INTERACTION_CALLBACK } from '../types/endpoint.ts'
import {
InteractionData,
InteractionPayload,
InteractionResponsePayload,
InteractionResponseType
} from '../types/slash.ts'
import { Embed } from './embed.ts'
2020-12-10 06:55:52 +00:00
import { Guild } from './guild.ts'
import { Member } from './member.ts'
import { GuildTextChannel } from './textChannel.ts'
import { User } from './user.ts'
2020-12-10 04:36:36 +00:00
export interface InteractionResponse {
type?: InteractionResponseType
content?: string
embeds?: Embed[]
tts?: boolean
flags?: number
2020-12-10 10:42:03 +00:00
temp?: boolean
2020-12-10 04:36:36 +00:00
}
export class Interaction {
client: Client
type: number
token: string
id: string
data: InteractionData
2020-12-10 06:55:52 +00:00
channel: GuildTextChannel
guild: Guild
member: Member
2020-12-10 04:36:36 +00:00
2020-12-10 06:55:52 +00:00
constructor(
client: Client,
data: InteractionPayload,
others: {
channel: GuildTextChannel
guild: Guild
member: Member
}
) {
2020-12-10 04:36:36 +00:00
this.client = client
this.type = data.type
this.token = data.token
2020-12-10 06:55:52 +00:00
this.member = others.member
2020-12-10 04:36:36 +00:00
this.id = data.id
this.data = data.data
2020-12-10 06:55:52 +00:00
this.guild = others.guild
this.channel = others.channel
}
get user(): User {
return this.member.user
}
get name(): string {
return this.data.name
2020-12-10 04:36:36 +00:00
}
async respond(data: InteractionResponse): Promise<Interaction> {
const payload: InteractionResponsePayload = {
type: data.type ?? InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data:
data.type === undefined ||
data.type === InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE ||
data.type === InteractionResponseType.CHANNEL_MESSAGE
? {
content: data.content ?? '',
embeds: data.embeds,
tts: data.tts ?? false,
2020-12-10 10:42:03 +00:00
flags: data.temp === true ? 64 : data.flags ?? undefined
2020-12-10 04:36:36 +00:00
}
: undefined
}
await this.client.rest.post(
INTERACTION_CALLBACK(this.id, this.token),
payload
)
return this
}
}