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

@@ -67,7 +67,6 @@ import {
type RESTPatchAPIGuildTemplateJSONBody,
type RESTPatchAPIGuildTemplateResult,
type RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody,
type RESTPatchAPIGuildVoiceStateCurrentMemberResult,
type RESTPatchAPIGuildVoiceStateUserJSONBody,
type RESTPatchAPIGuildWelcomeScreenJSONBody,
type RESTPatchAPIGuildWelcomeScreenResult,
@@ -102,6 +101,7 @@ import {
type RESTPutAPIGuildTemplateSyncResult,
type Snowflake,
} from 'discord-api-types/v10';
import { VoiceAPI } from './voice';
export class GuildsAPI {
public constructor(private readonly rest: REST) {}
@@ -687,11 +687,12 @@ export class GuildsAPI {
/**
* Edits a user's voice state in a guild
*
* @see {@link https://discord.com/developers/docs/resources/guild#modify-user-voice-state}
* @see {@link https://discord.com/developers/docs/resources/voice#modify-user-voice-state}
* @param guildId - The id of the guild to edit the current user's voice state in
* @param userId - The id of the user to edit the voice state for
* @param body - The data for editing the voice state
* @param options - The options for editing the voice state
* @deprecated Use {@link VoiceAPI.editUserVoiceState} instead
*/
public async editUserVoiceState(
guildId: Snowflake,
@@ -699,7 +700,7 @@ export class GuildsAPI {
body: RESTPatchAPIGuildVoiceStateUserJSONBody,
{ reason, signal }: Pick<RequestData, 'reason' | 'signal'> = {},
) {
await this.rest.patch(Routes.guildVoiceState(guildId, userId), { reason, body, signal });
return new VoiceAPI(this.rest).editUserVoiceState(guildId, userId, body, { reason, signal });
}
/**
@@ -1298,14 +1299,18 @@ export class GuildsAPI {
/**
* Sets the voice state for the current user
*
* @see {@link https://discord.com/developers/docs/resources/guild#modify-current-user-voice-state}
* @see {@link https://discord.com/developers/docs/resources/voice#modify-current-user-voice-state}
* @param guildId - The id of the guild
* @param body - The options for setting the voice state
* @param body - The data for setting the voice state
* @param options - The options for setting the voice state
* @deprecated Use {@link VoiceAPI.editVoiceState} instead
*/
public async setVoiceState(guildId: Snowflake, body: RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody = {}) {
return this.rest.patch(Routes.guildVoiceState(guildId, '@me'), {
body,
}) as Promise<RESTPatchAPIGuildVoiceStateCurrentMemberResult>;
public async setVoiceState(
guildId: Snowflake,
body: RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody = {},
{ signal }: Pick<RequestData, 'signal'> = {},
) {
return new VoiceAPI(this.rest).editVoiceState(guildId, body, { signal });
}
/**

View File

@@ -1,7 +1,17 @@
/* eslint-disable jsdoc/check-param-names */
import type { RequestData, REST } from '@discordjs/rest';
import { Routes, type RESTGetAPIVoiceRegionsResult } from 'discord-api-types/v10';
import {
Routes,
type Snowflake,
type RESTGetAPIVoiceRegionsResult,
type RESTGetAPIGuildVoiceStateUserResult,
type RESTGetAPIGuildVoiceStateCurrentMemberResult,
type RESTPatchAPIGuildVoiceStateUserJSONBody,
type RESTPatchAPIGuildVoiceStateCurrentMemberResult,
type RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody,
type RESTPatchAPIGuildVoiceStateUserResult,
} from 'discord-api-types/v10';
export class VoiceAPI {
public constructor(private readonly rest: REST) {}
@@ -15,4 +25,69 @@ export class VoiceAPI {
public async getVoiceRegions({ signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.voiceRegions(), { signal }) as Promise<RESTGetAPIVoiceRegionsResult>;
}
/**
* Fetches voice state of a user by their id
*
* @see {@link https://discord.com/developers/docs/resources/voice#get-user-voice-state}
* @param options - The options for fetching user voice state
*/
public async getUserVoiceState(guildId: Snowflake, userId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.guildVoiceState(guildId, userId), {
signal,
}) as Promise<RESTGetAPIGuildVoiceStateUserResult>;
}
/**
* Fetches the current user's voice state
*
* @see {@link https://discord.com/developers/docs/resources/voice#get-current-user-voice-state}
* @param options - The options for fetching user voice state
*/
public async getVoiceState(guildId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.guildVoiceState(guildId, '@me'), {
signal,
}) as Promise<RESTGetAPIGuildVoiceStateCurrentMemberResult>;
}
/**
* Edits a user's voice state in a guild
*
* @see {@link https://discord.com/developers/docs/resources/voice#modify-user-voice-state}
* @param guildId - The id of the guild to edit the current user's voice state in
* @param userId - The id of the user to edit the voice state for
* @param body - The data for editing the voice state
* @param options - The options for editing the voice state
*/
public async editUserVoiceState(
guildId: Snowflake,
userId: Snowflake,
body: RESTPatchAPIGuildVoiceStateUserJSONBody,
{ reason, signal }: Pick<RequestData, 'reason' | 'signal'> = {},
) {
return this.rest.patch(Routes.guildVoiceState(guildId, userId), {
reason,
body,
signal,
}) as Promise<RESTPatchAPIGuildVoiceStateUserResult>;
}
/**
* Edits the voice state for the current user
*
* @see {@link https://discord.com/developers/docs/resources/voice#modify-current-user-voice-state}
* @param guildId - The id of the guild
* @param body - The data for editing the voice state
* @param options - The options for editing the voice state
*/
public async editVoiceState(
guildId: Snowflake,
body: RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody = {},
{ signal }: Pick<RequestData, 'signal'> = {},
) {
return this.rest.patch(Routes.guildVoiceState(guildId, '@me'), {
body,
signal,
}) as Promise<RESTPatchAPIGuildVoiceStateCurrentMemberResult>;
}
}