From dc226975756b379c36b5eeb8effa8fc57cc22a8f Mon Sep 17 00:00:00 2001 From: Skillz Date: Sat, 7 Mar 2020 11:59:35 -0500 Subject: [PATCH] event api --- module/client.ts | 48 +++++++++++++++++++++++++++++++++++++++++++----- types/discord.ts | 25 +++++++++++++++++++++++++ types/options.ts | 10 ++++++---- 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/module/client.ts b/module/client.ts index 0b8669bf2..21076dfa2 100644 --- a/module/client.ts +++ b/module/client.ts @@ -7,7 +7,8 @@ import { GatewayOpcode, Webhook_Update_Payload, Presence_Update_Payload, - Typing_Start_Payload + Typing_Start_Payload, + Voice_State_Update_Payload } from "../types/discord.ts" import { spawnShards } from "./sharding-manager.ts" import { @@ -211,8 +212,13 @@ class Client { guild.voice_states().forEach(vs => { if (vs.channel_id !== options.id) return - this.event_handlers.voice_channel_leave?.(vs) + + const member = guild.members.get(vs.user_id) + if (!member) return + + this.event_handlers.voice_channel_leave?.(member, vs.channel_id) }) + cache.guilds.set(guild.id(), { ...guild, voice_states: () => [...guild.voice_states().filter(vs => vs.channel_id !== options.id)] @@ -472,15 +478,15 @@ class Client { return this.event_handlers.reaction_remove_emoji?.(data.d as Message_Reaction_Remove_Emoji_Payload) } - if (data.t === 'PRESENCE_UPDATE') { + if (data.t === "PRESENCE_UPDATE") { return this.event_handlers.presence_update?.(data.d as Presence_Update_Payload) } - if (data.t === 'TYPING_START') { + if (data.t === "TYPING_START") { return this.event_handlers.typing_start?.(data.d as Typing_Start_Payload) } - if (data.t === 'USER_UPDATE') { + if (data.t === "USER_UPDATE") { const user_data = data.d as User_Payload const cached_user = cache.users.get(this.bot_id) const user = create_user(user_data) @@ -488,6 +494,38 @@ class Client { return this.event_handlers.bot_update?.(user, cached_user) } + if (data.t === "VOICE_STATE_UPDATE") { + const payload = data.d as Voice_State_Update_Payload + if (!payload.guild_id) return + + const guild = cache.guilds.get(payload.guild_id) + if (!guild) return + + const member = guild.members.get(payload.user_id) + if (!member) return + + const cached_state = guild.voice_states().find(state => state.user_id === payload.user_id) + // No cached state before so lets make one for em + if (!cached_state) return (guild.voice_states = () => [...guild.voice_states(), payload]) + + if (cached_state.channel_id !== payload.channel_id) { + // Either joined or moved channels + if (payload.channel_id) { + cached_state.channel_id + ? // Was in a channel before + this.event_handlers.voice_channel_switch?.(member, payload.channel_id, cached_state.channel_id) + : // Was not in a channel before so user just joined + this.event_handlers.voice_channel_join?.(member, payload.channel_id) + } + // Left the channel + else if (cached_state.channel_id) { + this.event_handlers.voice_channel_leave?.(member, cached_state.channel_id) + } + } + + return this.event_handlers.voice_state_update?.(member, payload) + } + if (data.t === "WEBHOOKS_UPDATE") { const options = data.d as Webhook_Update_Payload return this.event_handlers.webhooks_update?.(options.channel_id, options.guild_id) diff --git a/types/discord.ts b/types/discord.ts index 7e99c5932..1725ccf2e 100644 --- a/types/discord.ts +++ b/types/discord.ts @@ -230,3 +230,28 @@ export interface Typing_Start_Payload { /** The member who started typing if this happened in a guild */ member?: Member_Create_Payload } + +export interface Voice_State_Update_Payload { + /** The guild id this voice state is for */ + guild_id?: string + /** The channel id this user is connected to */ + channel_id: string | null + /** The user id this voice state is for */ + user_id: string + /** The guild member this voice state is for */ + member?: Member_Create_Payload + /** The session id for this voice state */ + session_id: string + /** Whether this user is deafened by the server */ + deaf: boolean + /** Whether this user is muted by the server */ + mute: boolean + /** Whether this user is locally deafened */ + self_deaf: boolean + /** Whether this user is locally muted */ + self_mute: boolean + /** Whether this user is streaming using Go Live */ + self_stream?: boolean + /** Whether this user is muted by the bot */ + suppress: boolean +} diff --git a/types/options.ts b/types/options.ts index 6a9da94c0..1636077c6 100644 --- a/types/options.ts +++ b/types/options.ts @@ -1,6 +1,5 @@ -import { Properties, Emoji, DiscordPayload, Presence_Update_Payload, Typing_Start_Payload } from "./discord.ts" +import { Properties, Emoji, DiscordPayload, Presence_Update_Payload, Typing_Start_Payload, Voice_State_Update_Payload } from "./discord.ts" import { Channel, Guild } from "./return-type.ts" -import { Voice_State } from "./guild.ts" import { User } from "../structures/user.ts" import { Member } from "./member.ts" import { Role } from "../structures/role.ts" @@ -42,6 +41,7 @@ export interface Event_Handlers { message_delete?: (message: Message | Partial_Message) => unknown nickname_update?: (guild: Guild, member: Member, nickname: string, old_nickname?: string) => unknown presence_update?: (data: Presence_Update_Payload) => unknown + raw?: (data: DiscordPayload) => unknown ready?: () => unknown reaction_add?: (message: Message | Message_Reaction_Payload, emoji: Reaction_Payload, user_id: string) => unknown reaction_remove?: (message: Message | Message_Reaction_Payload, emoji: Reaction_Payload, user_id: string) => unknown @@ -52,9 +52,11 @@ export interface Event_Handlers { role_update?: (guild: Guild, role: Role, cached_role: Role) => unknown role_gained?: (guild: Guild, member: Member, role_id: string) => unknown role_lost?: (guild: Guild, member: Member, role_id: string) => unknown - voice_channel_leave?: (voice_state: Voice_State) => unknown - raw?: (data: DiscordPayload) => unknown typing_start?: (data: Typing_Start_Payload) => unknown + voice_channel_join?: (member: Member, channel_id: string) => unknown + voice_channel_leave?: (member: Member, channel_id: string) => unknown + voice_channel_switch?: (member: Member, channel_id: string, old_channel_id: string) => unknown + voice_state_update?: (member: Member, voice_state: Voice_State_Update_Payload) => unknown webhooks_update?: (channel_id: string, guild_id: string) => unknown }