Skip to content

Commit

Permalink
Merge branch 'main' into poll-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
TAEMBO authored Aug 20, 2024
2 parents d395ee6 + dd795da commit d77e3e7
Show file tree
Hide file tree
Showing 31 changed files with 4,103 additions and 687 deletions.
2 changes: 1 addition & 1 deletion packages/builders/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"@discordjs/formatters": "workspace:^",
"@discordjs/util": "workspace:^",
"@sapphire/shapeshift": "^4.0.0",
"discord-api-types": "0.37.94",
"discord-api-types": "0.37.96",
"fast-deep-equal": "^3.1.3",
"ts-mixer": "^6.0.4",
"tslib": "^2.6.2"
Expand Down
8 changes: 4 additions & 4 deletions packages/collection/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class Collection<Key, Value> extends Map<Key, Value> {
if (amount < 0) return this.last(amount * -1);
amount = Math.min(this.size, amount);
const iter = this.values();
return Array.from({ length: amount }, (): Value => iter.next().value);
return Array.from({ length: amount }, (): Value => iter.next().value!);
}

/**
Expand All @@ -104,7 +104,7 @@ export class Collection<Key, Value> extends Map<Key, Value> {
if (amount < 0) return this.lastKey(amount * -1);
amount = Math.min(this.size, amount);
const iter = this.keys();
return Array.from({ length: amount }, (): Key => iter.next().value);
return Array.from({ length: amount }, (): Key => iter.next().value!);
}

/**
Expand Down Expand Up @@ -512,7 +512,7 @@ export class Collection<Key, Value> extends Map<Key, Value> {
if (thisArg !== undefined) fn = fn.bind(thisArg);
const iter = this.entries();
return Array.from({ length: this.size }, (): NewValue => {
const [key, value] = iter.next().value;
const [key, value] = iter.next().value!;
return fn(value, key, this);
});
}
Expand Down Expand Up @@ -634,7 +634,7 @@ export class Collection<Key, Value> extends Map<Key, Value> {
const iterator = this.entries();
if (initialValue === undefined) {
if (this.size === 0) throw new TypeError('Reduce of empty collection with no initial value');
accumulator = iterator.next().value[1];
accumulator = iterator.next().value![1] as unknown as InitialValue;
} else {
accumulator = initialValue;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@discordjs/ws": "workspace:^",
"@sapphire/snowflake": "^3.5.3",
"@vladfrangu/async_event_emitter": "^2.2.4",
"discord-api-types": "0.37.94"
"discord-api-types": "0.37.96"
},
"devDependencies": {
"@discordjs/api-extractor": "workspace:^",
Expand Down
88 changes: 87 additions & 1 deletion packages/core/src/api/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import type { RequestData, REST } from '@discordjs/rest';
import {
Routes,
type RESTGetAPIApplicationEmojiResult,
type RESTGetAPIApplicationEmojisResult,
type RESTGetCurrentApplicationResult,
type RESTPatchAPIApplicationEmojiJSONBody,
type RESTPatchAPIApplicationEmojiResult,
type RESTPatchCurrentApplicationJSONBody,
type RESTPatchCurrentApplicationResult,
Routes,
type RESTPostAPIApplicationEmojiJSONBody,
type RESTPostAPIApplicationEmojiResult,
type Snowflake,
} from 'discord-api-types/v10';

export class ApplicationsAPI {
Expand Down Expand Up @@ -34,4 +41,83 @@ export class ApplicationsAPI {
signal,
}) as Promise<RESTPatchCurrentApplicationResult>;
}

/**
* Fetches all emojis of an application
*
* @see {@link https://discord.com/developers/docs/resources/emoji#list-application-emojis}
* @param applicationId - The id of the application to fetch the emojis of
* @param options - The options for fetching the emojis
*/
public async getEmojis(applicationId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.applicationEmojis(applicationId), {
signal,
}) as Promise<RESTGetAPIApplicationEmojisResult>;
}

/**
* Fetches an emoji of an application
*
* @see {@link https://discord.com/developers/docs/resources/emoji#get-application-emoji}
* @param applicationId - The id of the application to fetch the emoji of
* @param emojiId - The id of the emoji to fetch
* @param options - The options for fetching the emoji
*/
public async getEmoji(applicationId: Snowflake, emojiId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.applicationEmoji(applicationId, emojiId), {
signal,
}) as Promise<RESTGetAPIApplicationEmojiResult>;
}

/**
* Creates a new emoji of an application
*
* @see {@link https://discord.com/developers/docs/resources/emoji#create-application-emoji}
* @param applicationId - The id of the application to create the emoji of
* @param body - The data for creating the emoji
* @param options - The options for creating the emoji
*/
public async createEmoji(
applicationId: Snowflake,
body: RESTPostAPIApplicationEmojiJSONBody,
{ signal }: Pick<RequestData, 'signal'> = {},
) {
return this.rest.post(Routes.applicationEmojis(applicationId), {
body,
signal,
}) as Promise<RESTPostAPIApplicationEmojiResult>;
}

/**
* Edits an emoji of an application
*
* @see {@link https://discord.com/developers/docs/resources/emoji#modify-application-emoji}
* @param applicationId - The id of the application to edit the emoji of
* @param emojiId - The id of the emoji to edit
* @param body - The data for editing the emoji
* @param options - The options for editing the emoji
*/
public async editEmoji(
applicationId: Snowflake,
emojiId: Snowflake,
body: RESTPatchAPIApplicationEmojiJSONBody,
{ signal }: Pick<RequestData, 'signal'> = {},
) {
return this.rest.patch(Routes.applicationEmoji(applicationId, emojiId), {
body,
signal,
}) as Promise<RESTPatchAPIApplicationEmojiResult>;
}

/**
* Deletes an emoji of an application
*
* @see {@link https://discord.com/developers/docs/resources/emoji#delete-application-emoji}
* @param applicationId - The id of the application to delete the emoji of
* @param emojiId - The id of the emoji to delete
* @param options - The options for deleting the emoji
*/
public async deleteEmoji(applicationId: Snowflake, emojiId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
await this.rest.delete(Routes.applicationEmoji(applicationId, emojiId), { signal });
}
}
23 changes: 14 additions & 9 deletions packages/core/src/api/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import {
type RESTPatchAPIGuildTemplateJSONBody,
type RESTPatchAPIGuildTemplateResult,
type RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody,
type RESTPatchAPIGuildVoiceStateCurrentMemberResult,
type RESTPatchAPIGuildVoiceStateUserJSONBody,
type RESTPatchAPIGuildWelcomeScreenJSONBody,
type RESTPatchAPIGuildWelcomeScreenResult,
Expand Down Expand Up @@ -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) {}
Expand Down Expand Up @@ -687,19 +687,20 @@ 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,
userId: Snowflake,
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 });
}

/**
Expand Down Expand Up @@ -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 });
}

/**
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/api/oauth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
type RESTGetAPIOAuth2CurrentApplicationResult,
type RESTPostOAuth2AccessTokenURLEncodedData,
type RESTPostOAuth2AccessTokenResult,
type RESTPostOAuth2TokenRevocationQuery,
type Snowflake,
} from 'discord-api-types/v10';

export class OAuth2API {
Expand Down Expand Up @@ -121,4 +123,31 @@ export class OAuth2API {
signal,
}) as Promise<RESTGetAPIOAuth2CurrentAuthorizationResult>;
}

/**
* Revokes an OAuth2 token
*
* @see {@link https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-token-revocation-example}
* @param applicationId - The application id
* @param applicationSecret - The application secret
* @param body - The body of the token revocation request
* @param options - The options for the token revocation request
*/
public async revokeToken(
applicationId: Snowflake,
applicationSecret: string,
body: RESTPostOAuth2TokenRevocationQuery,
{ signal }: Pick<RequestData, 'signal'> = {},
) {
await this.rest.post(Routes.oauth2TokenRevocation(), {
body: makeURLSearchParams(body),
passThroughBody: true,
headers: {
Authorization: `Basic ${btoa(`${applicationId}:${applicationSecret}`)}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
auth: false,
signal,
});
}
}
12 changes: 12 additions & 0 deletions packages/core/src/api/sticker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { RequestData, REST } from '@discordjs/rest';
import {
Routes,
type RESTGetAPIStickerPack,
type RESTGetAPIStickerResult,
type RESTGetStickerPacksResult,
type Snowflake,
Expand All @@ -11,6 +12,17 @@ import {
export class StickersAPI {
public constructor(private readonly rest: REST) {}

/**
* Fetches a sticker pack
*
* @see {@link https://discord.com/developers/docs/resources/sticker#get-sticker-pack}
* @param packId - The id of the sticker pack
* @param options - The options for fetching the sticker pack
*/
public async getStickerPack(packId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.stickerPack(packId), { signal }) as Promise<RESTGetAPIStickerPack>;
}

/**
* Fetches all of the sticker packs
*
Expand Down
77 changes: 76 additions & 1 deletion packages/core/src/api/voice.ts
Original file line number Diff line number Diff line change
@@ -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) {}
Expand All @@ -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>;
}
}
2 changes: 1 addition & 1 deletion packages/discord.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@discordjs/util": "workspace:^",
"@discordjs/ws": "1.1.1",
"@sapphire/snowflake": "3.5.3",
"discord-api-types": "0.37.94",
"discord-api-types": "0.37.96",
"fast-deep-equal": "3.1.3",
"lodash.snakecase": "4.1.1",
"tslib": "2.6.2",
Expand Down
Loading

0 comments on commit d77e3e7

Please sign in to comment.