feat: http voice states (#235)

* feat: http voice states

* fix: ups

* fix: typo

* fix: structure
This commit is contained in:
Marcos Susaña 2024-08-07 20:26:47 -04:00 committed by GitHub
parent 571ab18f4f
commit d6ff9767a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 50 additions and 20 deletions

View File

@ -15,6 +15,7 @@ import type {
RESTGetAPIAuditLogResult,
RESTGetAPIAutoModerationRuleResult,
RESTGetAPIAutoModerationRulesResult,
RESTGetAPICurrentUserVoiceState,
RESTGetAPIGuildBanResult,
RESTGetAPIGuildBansQuery,
RESTGetAPIGuildBansResult,
@ -53,6 +54,7 @@ import type {
RESTGetAPIGuildWidgetJSONResult,
RESTGetAPIGuildWidgetSettingsResult,
RESTGetAPITemplateResult,
RESTGetAPIUserVoiceState,
RESTPatchAPIAutoModerationRuleJSONBody,
RESTPatchAPIAutoModerationRuleResult,
RESTPatchAPICurrentGuildMemberJSONBody,
@ -313,13 +315,15 @@ export interface GuildRoutes {
patch(
args: RestArguments<ProxyRequestMethod.Patch, RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody>,
): Promise<RESTPatchAPIGuildVoiceStateCurrentMemberResult>;
get(args?: RestArguments<ProxyRequestMethod.Get>): Promise<RESTGetAPICurrentUserVoiceState>;
};
(
id: string,
userId: string,
): {
patch(
args: RestArguments<ProxyRequestMethod.Patch, RESTPatchAPIGuildVoiceStateUserJSONBody>,
): Promise<RESTPatchAPIGuildVoiceStateUserResult>;
get(args?: RestArguments<ProxyRequestMethod.Get>): Promise<RESTGetAPIUserVoiceState>;
};
};
stickers: {

View File

@ -1,14 +1,14 @@
import type { GatewayVoiceState } from '../../types';
import type { APIVoiceState } from '../../types';
import type { ReturnCache } from '../..';
import { fakePromise } from '../../common';
import { GuildBasedResource } from './default/guild-based';
import { Transformers, type VoiceStateStructure } from '../../client/transformers';
export class VoiceStates extends GuildBasedResource<any, GatewayVoiceState> {
export class VoiceStates extends GuildBasedResource<any, APIVoiceState> {
namespace = 'voice_state';
//@ts-expect-error
filter(data: GatewayVoiceState, id: string, guild_id: string) {
filter(data: APIVoiceState, id: string, guild_id: string) {
return true;
}
@ -23,7 +23,7 @@ export class VoiceStates extends GuildBasedResource<any, GatewayVoiceState> {
);
}
raw(memberId: string, guildId: string): ReturnCache<GatewayVoiceState | undefined> {
raw(memberId: string, guildId: string): ReturnCache<APIVoiceState | undefined> {
return super.get(memberId, guildId);
}
@ -36,7 +36,7 @@ export class VoiceStates extends GuildBasedResource<any, GatewayVoiceState> {
);
}
bulkRaw(ids: string[], guild: string): ReturnCache<GatewayVoiceState[]> {
bulkRaw(ids: string[], guild: string): ReturnCache<APIVoiceState[]> {
return super.bulk(ids, guild);
}
@ -46,9 +46,9 @@ export class VoiceStates extends GuildBasedResource<any, GatewayVoiceState> {
);
}
valuesRaw(guildId: string): ReturnCache<GatewayVoiceState[]> {
valuesRaw(guildId: string): ReturnCache<APIVoiceState[]> {
return super.values(guildId);
}
}
export type VoiceStateResource = Omit<GatewayVoiceState, 'member'> & { guild_id: string };
export type VoiceStateResource = Omit<APIVoiceState, 'member'> & { guild_id: string };

View File

@ -10,7 +10,7 @@ import {
import { PermissionsBitField } from '../../structures/extra/Permissions';
import type { GuildMemberResolvable } from '../types/resolvables';
import { BaseShorter } from './base';
import { Transformers } from '../../client/transformers';
import { Transformers, type VoiceStateStructure } from '../../client/transformers';
export class MemberShorter extends BaseShorter {
/**
@ -222,8 +222,17 @@ export class MemberShorter extends BaseShorter {
return this.client.cache.presences?.get(memberId);
}
voice(guildId: string, memberId: string) {
return this.client.cache.voiceStates?.get(memberId, guildId);
async voice(guildId: string, memberId: '@me', force?: boolean): Promise<VoiceStateStructure>;
async voice(guildId: string, memberId: string, force?: boolean): Promise<VoiceStateStructure>;
async voice(guildId: string, memberId: string | '@me', force = false) {
if (!force) {
const state = await this.client.cache.voiceStates?.get(memberId, guildId);
if (state) return state;
}
const state = await this.client.proxy.guilds(guildId)['voice-states'](memberId).get();
await this.client.cache.voiceStates?.set(memberId, guildId, state);
return Transformers.VoiceState(this.client, state);
}
/**

View File

@ -74,8 +74,8 @@ export class BaseGuildMember extends DiscordBase {
return this.client.members.presence(this.id);
}
voice() {
return this.client.members.voice(this.guildId, this.id);
voice(force = false) {
return this.client.members.voice(this.guildId, this.id, force);
}
toString() {

View File

@ -2,14 +2,14 @@ import type { UsingClient } from '../';
import type { VoiceStateResource } from '../cache/resources/voice-states';
import { type GuildMemberStructure, Transformers } from '../client/transformers';
import type { ObjectToLower } from '../common';
import type { GatewayVoiceState } from '../types';
import type { APIVoiceState } from '../types';
import { Base } from './extra/Base';
export interface VoiceState extends Base, ObjectToLower<Omit<VoiceStateResource, 'member'>> {}
export class VoiceState extends Base {
protected withMember?: GuildMemberStructure;
constructor(client: UsingClient, data: GatewayVoiceState) {
constructor(client: UsingClient, data: APIVoiceState) {
super(client);
const { member, ...rest } = data;
this.__patchThis(rest);
@ -68,6 +68,11 @@ export class VoiceState extends Base {
return this.setChannel(null, reason);
}
async fetch(force = false) {
const member = this.withMember ?? (await this.member(force));
return this.client.members.voice(this.guildId, member.id, force);
}
async setChannel(channel_id: null | string, reason?: string) {
const member = await this.client.members.edit(this.guildId, this.userId, { channel_id }, reason);
this.channelId = channel_id;

View File

@ -26,7 +26,7 @@ import type {
GatewayPresenceUpdate as RawGatewayPresenceUpdate,
GatewayThreadListSync as RawGatewayThreadListSync,
GatewayThreadMembersUpdate as RawGatewayThreadMembersUpdate,
GatewayVoiceState,
APIVoiceState,
InviteTargetType,
PresenceUpdateStatus,
AutoModerationRuleTriggerType,
@ -554,7 +554,7 @@ export interface GatewayGuildCreateDispatchData extends APIGuild {
*
* See https://discord.com/developers/docs/resources/voice#voice-state-object
*/
voice_states: Omit<GatewayVoiceState, 'guild_id'>[];
voice_states: Omit<APIVoiceState, 'guild_id'>[];
/**
* Users in the guild
*
@ -1517,7 +1517,7 @@ export type GatewayVoiceStateUpdateDispatch = DataPayload<
/**
* https://discord.com/developers/docs/topics/gateway-events#voice-state-update
*/
export type GatewayVoiceStateUpdateDispatchData = GatewayVoiceState;
export type GatewayVoiceStateUpdateDispatchData = APIVoiceState;
/**
* https://discord.com/developers/docs/topics/gateway-events#voice-server-update

View File

@ -681,6 +681,8 @@ export enum MessageType {
GuildIncidentAlertModeDisabled,
GuildIncidentReportRaid,
GuildIncidentReportFalseAlarm,
PurchaseNotification = 44,
PollResult = 46,
}
/**

View File

@ -8,7 +8,7 @@ import type { APIGuildMember } from './guild';
/**
* https://discord.com/developers/docs/resources/voice#voice-state-object
*/
export interface GatewayVoiceState {
export interface APIVoiceState {
/**
* The guild id this voice state is for
*/

View File

@ -1,10 +1,20 @@
import type { APIVoiceRegion } from '../payloads';
import type { APIVoiceRegion, APIVoiceState } from '../payloads';
/**
* https://discord.com/developers/docs/resources/voice#list-voice-regions
*/
export type RESTGetAPIVoiceRegionsResult = APIVoiceRegion[];
/**
* https://discord.com/developers/docs/resources/voice#get-current-user-voice-state
*/
export type RESTGetAPICurrentUserVoiceState = RESTGetAPIUserVoiceState;
/**
* https://discord.com/developers/docs/resources/voice#get-user-voice-state
*/
export type RESTGetAPIUserVoiceState = APIVoiceState;
/**
* @deprecated This was exported with the wrong name, use `RESTGetAPIVoiceRegionsResult` instead
*/