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 {
|
|
|
|
InteractionApplicationCommandResolved,
|
2021-04-22 03:48:45 +00:00
|
|
|
SlashCommandInteraction
|
2021-02-12 11:37:38 +00:00
|
|
|
} from '../../structures/slash.ts'
|
2021-04-22 03:48:45 +00:00
|
|
|
import {
|
|
|
|
Interaction,
|
|
|
|
InteractionChannel
|
|
|
|
} from '../../structures/interactions.ts'
|
2021-03-19 15:39:14 +00:00
|
|
|
import { GuildTextBasedChannel } from '../../structures/guildTextChannel.ts'
|
2021-04-22 03:48:45 +00:00
|
|
|
import {
|
|
|
|
InteractionPayload,
|
|
|
|
InteractionType
|
|
|
|
} from '../../types/interactions.ts'
|
2021-02-12 11:37:38 +00:00
|
|
|
import { UserPayload } from '../../types/user.ts'
|
|
|
|
import { Permissions } from '../../utils/permissions.ts'
|
2021-04-04 05:42:15 +00:00
|
|
|
import type { Gateway, GatewayEventHandler } from '../mod.ts'
|
2021-03-26 07:08:32 +00:00
|
|
|
import { User } from '../../structures/user.ts'
|
|
|
|
import { Role } from '../../structures/role.ts'
|
2021-04-23 05:37:55 +00:00
|
|
|
import { RolePayload } from '../../types/role.ts'
|
|
|
|
import { InteractionChannelPayload } from '../../types/slashCommands.ts'
|
|
|
|
import { Message } from '../../structures/message.ts'
|
|
|
|
import { TextChannel } from '../../structures/textChannel.ts'
|
2020-12-10 04:36:36 +00:00
|
|
|
|
|
|
|
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
|
2021-04-23 05:37:55 +00:00
|
|
|
: (await gateway.client.guilds.get(d.guild_id)) ??
|
|
|
|
new Guild(gateway.client, { unavailable: true, id: d.guild_id } as any)
|
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-04-23 05:37:55 +00:00
|
|
|
? (await guild?.members.get(d.member.user.id))! ??
|
|
|
|
new Member(
|
|
|
|
gateway.client,
|
|
|
|
d.member!,
|
|
|
|
new User(gateway.client, d.member.user),
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
|
|
guild!,
|
|
|
|
new Permissions(d.member.permissions)
|
|
|
|
)
|
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
|
|
|
|
2021-04-23 05:37:55 +00:00
|
|
|
const channel = await gateway.client.channels.get<GuildTextBasedChannel>(
|
|
|
|
d.channel_id
|
|
|
|
)
|
2020-12-10 06:55:52 +00:00
|
|
|
|
2021-02-12 11:37:38 +00:00
|
|
|
const resolved: InteractionApplicationCommandResolved = {
|
|
|
|
users: {},
|
|
|
|
channels: {},
|
|
|
|
members: {},
|
|
|
|
roles: {}
|
|
|
|
}
|
|
|
|
|
2021-04-23 05:37:55 +00:00
|
|
|
if ((d.data as any)?.resolved !== undefined) {
|
|
|
|
for (const [id, data] of Object.entries(
|
|
|
|
(d.data as any)?.resolved.users ?? {}
|
|
|
|
)) {
|
|
|
|
await gateway.client.users.set(id, data as UserPayload)
|
2021-02-12 11:37:38 +00:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2021-04-23 05:37:55 +00:00
|
|
|
for (const [id, data] of Object.entries(
|
|
|
|
(d.data as any)?.resolved.members ?? {}
|
|
|
|
)) {
|
2021-02-12 11:37:38 +00:00
|
|
|
const roles = await guild?.roles.array()
|
|
|
|
let permissions = new Permissions(Permissions.DEFAULT)
|
|
|
|
if (roles !== undefined) {
|
|
|
|
const mRoles = roles.filter(
|
2021-04-23 05:37:55 +00:00
|
|
|
(r) =>
|
|
|
|
((data as any)?.roles?.includes(r.id) as boolean) ||
|
|
|
|
r.id === guild?.id
|
2021-02-12 11:37:38 +00:00
|
|
|
)
|
|
|
|
permissions = new Permissions(mRoles.map((r) => r.permissions))
|
|
|
|
}
|
2021-04-23 05:37:55 +00:00
|
|
|
;(data as any).user = ((d.data as any).resolved.users?.[
|
|
|
|
id
|
|
|
|
] as unknown) as UserPayload
|
2021-02-12 11:37:38 +00:00
|
|
|
resolved.members[id] = new Member(
|
|
|
|
gateway.client,
|
2021-04-23 05:37:55 +00:00
|
|
|
data as any,
|
2021-02-12 11:37:38 +00:00
|
|
|
resolved.users[id],
|
|
|
|
guild as Guild,
|
|
|
|
permissions
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-23 05:37:55 +00:00
|
|
|
for (const [id, data] of Object.entries(
|
|
|
|
(d.data as any).resolved.roles ?? {}
|
|
|
|
)) {
|
2021-02-12 11:37:38 +00:00
|
|
|
if (guild !== undefined) {
|
2021-04-23 05:37:55 +00:00
|
|
|
await guild.roles.set(id, data as RolePayload)
|
2021-02-12 11:37:38 +00:00
|
|
|
resolved.roles[id] = ((await guild.roles.get(id)) as unknown) as Role
|
|
|
|
} else {
|
|
|
|
resolved.roles[id] = new Role(
|
|
|
|
gateway.client,
|
2021-04-23 05:37:55 +00:00
|
|
|
data as any,
|
2021-02-12 11:37:38 +00:00
|
|
|
(guild as unknown) as Guild
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 05:37:55 +00:00
|
|
|
for (const [id, data] of Object.entries(
|
|
|
|
(d.data as any).resolved.channels ?? {}
|
|
|
|
)) {
|
|
|
|
resolved.channels[id] = new InteractionChannel(
|
|
|
|
gateway.client,
|
|
|
|
data as InteractionChannelPayload
|
|
|
|
)
|
2021-02-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 05:37:55 +00:00
|
|
|
let message: Message | undefined
|
|
|
|
if (d.message !== undefined) {
|
|
|
|
const channel = (await gateway.client.channels.get<TextChannel>(
|
|
|
|
d.message.channel_id
|
|
|
|
))!
|
|
|
|
message = new Message(
|
|
|
|
gateway.client,
|
|
|
|
d.message,
|
|
|
|
channel,
|
|
|
|
new User(gateway.client, d.message.author)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-22 03:48:45 +00:00
|
|
|
let interaction
|
|
|
|
if (d.type === InteractionType.APPLICATION_COMMAND) {
|
|
|
|
interaction = new SlashCommandInteraction(gateway.client, d, {
|
|
|
|
member,
|
|
|
|
guild,
|
|
|
|
channel,
|
|
|
|
user,
|
|
|
|
resolved
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
interaction = new Interaction(gateway.client, d, {
|
|
|
|
member,
|
|
|
|
guild,
|
|
|
|
channel,
|
2021-04-23 05:37:55 +00:00
|
|
|
user,
|
|
|
|
message
|
2021-04-22 03:48:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-10 04:36:36 +00:00
|
|
|
gateway.client.emit('interactionCreate', interaction)
|
|
|
|
}
|