harmony/src/gateway/handlers/interactionCreate.ts

111 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-02-12 11:37:38 +00:00
import { Guild } from '../../structures/guild.ts'
2020-12-10 06:55:52 +00:00
import { Member } from '../../structures/member.ts'
2021-02-12 11:37:38 +00:00
import { Role } from '../../structures/role.ts'
import {
Interaction,
InteractionApplicationCommandResolved,
InteractionChannel
} from '../../structures/slash.ts'
2020-12-10 06:55:52 +00:00
import { GuildTextChannel } from '../../structures/textChannel.ts'
2021-02-12 11:37:38 +00:00
import { User } from '../../structures/user.ts'
2020-12-10 04:36:36 +00:00
import { InteractionPayload } from '../../types/slash.ts'
2021-02-12 11:37:38 +00:00
import { UserPayload } from '../../types/user.ts'
import { Permissions } from '../../utils/permissions.ts'
2020-12-10 04:36:36 +00:00
import { Gateway, GatewayEventHandler } from '../index.ts'
export const interactionCreate: GatewayEventHandler = async (
gateway: Gateway,
d: InteractionPayload
) => {
2021-02-10 12:29:21 +00:00
// NOTE(DjDeveloperr): Mason once mentioned that channel_id can be optional in Interaction.
// This case can be seen in future proofing Interactions, and one he mentioned was
// that bots will be able to add custom context menus. In that case, Interaction will not have it.
// Ref: https://github.com/discord/discord-api-docs/pull/2568/files#r569025697
if (d.channel_id === undefined) return
const guild =
d.guild_id === undefined
? undefined
: await gateway.client.guilds.get(d.guild_id)
2020-12-10 06:55:52 +00:00
2021-02-10 12:29:21 +00:00
if (d.member !== undefined)
2021-02-12 11:37:38 +00:00
await guild?.members.set(d.member.user.id, d.member)
2021-02-10 12:29:21 +00:00
const member =
d.member !== undefined
2021-02-12 11:37:38 +00:00
? (((await guild?.members.get(d.member.user.id)) as unknown) as Member)
2021-02-10 12:29:21 +00:00
: undefined
if (d.user !== undefined) await gateway.client.users.set(d.user.id, d.user)
const dmUser =
d.user !== undefined ? await gateway.client.users.get(d.user.id) : undefined
const user = member !== undefined ? member.user : dmUser
if (user === undefined) return
2020-12-10 06:55:52 +00:00
const channel =
(await gateway.client.channels.get<GuildTextChannel>(d.channel_id)) ??
(await gateway.client.channels.fetch<GuildTextChannel>(d.channel_id))
2021-02-12 11:37:38 +00:00
const resolved: InteractionApplicationCommandResolved = {
users: {},
channels: {},
members: {},
roles: {}
}
if (d.data?.resolved !== undefined) {
for (const [id, data] of Object.entries(d.data.resolved.users ?? {})) {
await gateway.client.users.set(id, data)
resolved.users[id] = ((await gateway.client.users.get(
id
)) as unknown) as User
if (resolved.members[id] !== undefined)
resolved.users[id].member = resolved.members[id]
}
for (const [id, data] of Object.entries(d.data.resolved.members ?? {})) {
const roles = await guild?.roles.array()
let permissions = new Permissions(Permissions.DEFAULT)
if (roles !== undefined) {
const mRoles = roles.filter(
(r) => (data?.roles?.includes(r.id) as boolean) || r.id === guild?.id
)
permissions = new Permissions(mRoles.map((r) => r.permissions))
}
data.user = (d.data.resolved.users?.[id] as unknown) as UserPayload
resolved.members[id] = new Member(
gateway.client,
data,
resolved.users[id],
guild as Guild,
permissions
)
}
for (const [id, data] of Object.entries(d.data.resolved.roles ?? {})) {
if (guild !== undefined) {
await guild.roles.set(id, data)
resolved.roles[id] = ((await guild.roles.get(id)) as unknown) as Role
} else {
resolved.roles[id] = new Role(
gateway.client,
data,
(guild as unknown) as Guild
)
}
}
for (const [id, data] of Object.entries(d.data.resolved.channels ?? {})) {
resolved.channels[id] = new InteractionChannel(gateway.client, data)
}
}
2020-12-10 06:55:52 +00:00
const interaction = new Interaction(gateway.client, d, {
member,
guild,
2021-02-10 12:29:21 +00:00
channel,
2021-02-12 11:37:38 +00:00
user,
resolved
2020-12-10 06:55:52 +00:00
})
2020-12-10 04:36:36 +00:00
gateway.client.emit('interactionCreate', interaction)
}