feat(VoiceState): add methods for fetching voice state (#10442)

* feat(VoiceState): add methods for fetching voice state

* fix: links to new methods

* chore: remove unused import

* chore: use member id

* chore: requested changes

* chore: '@me' as fetch param

* chore: add ediUserVoiceState return type

* refactor: redirect function calls to VoiceAPI

---------

Co-authored-by: Almeida <almeidx@pm.me>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Naiyar
2024-08-20 15:32:17 +05:30
committed by GitHub
parent 9b707f2b83
commit 9907ff915e
6 changed files with 125 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
'use strict';
const { Routes } = require('discord-api-types/v10');
const CachedManager = require('./CachedManager');
const VoiceState = require('../structures/VoiceState');
@@ -32,6 +33,27 @@ class VoiceStateManager extends CachedManager {
if (cache) this.cache.set(data.user_id, entry);
return entry;
}
/**
* Obtains a user's voice state from discord or from the cache if it's already available.
* @param {GuildMemberResolvable|'@me'} member The member whose voice state is to be fetched
* @param {BaseFetchOptions} [options] Additional options for this fetch
* @returns {Promise<VoiceState>}
* @example
* // Fetch a member's voice state
* guild.voiceStates.fetch("66564597481480192")
* .then(console.log)
* .catch(console.error);
*/
async fetch(member, { cache = true, force = false } = {}) {
const id = member === '@me' ? member : this.guild.members.resolveId(member);
if (!force) {
const existing = this.cache.get(id === '@me' ? this.client.user.id : id);
if (existing) return existing;
}
const data = await this.client.rest.get(Routes.guildVoiceState(this.guild.id, id));
return this._add(data, cache);
}
}
module.exports = VoiceStateManager;

View File

@@ -250,6 +250,15 @@ class VoiceState extends Base {
return this;
}
/**
* Fetches this voice state.
* @param {boolean} [force=true] Whether to skip the cache check and request the API
* @returns {Promise<VoiceState>}
*/
fetch(force = true) {
return this.guild.voiceStates.fetch(this.id, { force });
}
/**
* Toggles the request to speak in the channel.
* Only applicable for stage channels and for the client's own voice state.

View File

@@ -3625,6 +3625,7 @@ export class VoiceState extends Base {
public setRequestToSpeak(request?: boolean): Promise<this>;
public setSuppressed(suppressed?: boolean): Promise<this>;
public edit(options: VoiceStateEditOptions): Promise<this>;
public fetch(force?: boolean): Promise<VoiceState>;
}
// tslint:disable-next-line no-empty-interface
@@ -4627,6 +4628,7 @@ export class UserManager extends CachedManager<Snowflake, User, UserResolvable>
export class VoiceStateManager extends CachedManager<Snowflake, VoiceState, typeof VoiceState> {
private constructor(guild: Guild, iterable?: Iterable<RawVoiceStateData>);
public guild: Guild;
public fetch(member: GuildMemberResolvable | '@me', options?: BaseFetchOptions): Promise<VoiceState>;
}
//#endregion

View File

@@ -47,6 +47,7 @@ import {
APIUnavailableGuild,
APIUser,
APIVoiceRegion,
APIVoiceState,
APIWebhook,
GatewayActivity,
GatewayActivityAssets,
@@ -62,7 +63,6 @@ import {
GatewayPresenceUpdate,
GatewayReadyDispatchData,
GatewayTypingStartDispatchData,
GatewayVoiceState,
RESTAPIPartialCurrentUserGuild,
RESTGetAPIWebhookWithTokenResult,
RESTPatchAPIChannelMessageJSONBody,
@@ -195,7 +195,7 @@ export type RawUserData =
export type RawVoiceRegionData = APIVoiceRegion;
export type RawVoiceStateData = GatewayVoiceState | Omit<GatewayVoiceState, 'guild_id'>;
export type RawVoiceStateData = APIVoiceState | Omit<APIVoiceState, 'guild_id'>;
export type RawWebhookData =
| APIWebhook