feat: add localizations in api-types builders and interactions

This commit is contained in:
Yuzu 2022-08-02 20:13:45 -05:00
parent d3f97b7b71
commit fd2b3bc8ce
3 changed files with 42 additions and 9 deletions

View File

@ -1309,10 +1309,12 @@ export interface DiscordInteraction {
message?: DiscordMessage; message?: DiscordMessage;
/** the command data payload */ /** the command data payload */
data?: DiscordInteractionData; data?: DiscordInteractionData;
/** The selected language of the invoking user */
locale?: string;
/** The guild's preferred locale, if invoked in a guild */ /** The guild's preferred locale, if invoked in a guild */
guild_locale?: string; guild_locale?: string;
/** Bitwise set of permissions the app or bot has within the channel the interaction was sent from */
app_permissions?: string;
/** Selected language of the invoking user */
locale?: string;
} }
/** https://discord.com/developers/docs/resources/guild#guild-member-object */ /** https://discord.com/developers/docs/resources/guild#guild-member-object */

View File

@ -1,6 +1,10 @@
import { ApplicationCommandOptionTypes, ChannelTypes, Localization } from '@biscuitland/api-types'; import type { ChannelTypes, Localization, Locales } from '@biscuitland/api-types';
import { ApplicationCommandOptionTypes } from '@biscuitland/api-types';
import { ApplicationCommandOptionChoice } from '../../structures/interactions'; import { ApplicationCommandOptionChoice } from '../../structures/interactions';
export type Localizations = typeof Locales[keyof typeof Locales] & string;
export class ChoiceBuilder { export class ChoiceBuilder {
name?: string; name?: string;
value?: string; value?: string;
@ -31,7 +35,9 @@ export class OptionBuilder {
autocomplete?: boolean; autocomplete?: boolean;
type?: ApplicationCommandOptionTypes; type?: ApplicationCommandOptionTypes;
name?: string; name?: string;
nameLocalization?: Record<Localizations, string>;
description?: string; description?: string;
descriptionLocalization?: Record<Localizations, string>;
constructor(type?: ApplicationCommandOptionTypes, name?: string, description?: string) { constructor(type?: ApplicationCommandOptionTypes, name?: string, description?: string) {
this.type = type; this.type = type;
@ -43,12 +49,18 @@ export class OptionBuilder {
return (this.type = type), this; return (this.type = type), this;
} }
setName(name: string): this { setName(name: string, localization?: Record<Localizations, string>): this {
return (this.name = name), this; this.name = name;
this.nameLocalization = localization;
return this;
} }
setDescription(description: string): this { setDescription(description: string, localization?: Record<Localizations, string>): this {
return (this.description = description), this; this.description = description;
this.descriptionLocalization = localization;
return this;
} }
setRequired(required: boolean): this { setRequired(required: boolean): this {
@ -65,7 +77,9 @@ export class OptionBuilder {
const applicationCommandOption: ApplicationCommandOption = { const applicationCommandOption: ApplicationCommandOption = {
type: this.type, type: this.type,
name: this.name, name: this.name,
name_localizations: this.nameLocalization,
description: this.description, description: this.description,
description_localizations: this.descriptionLocalization,
required: this.required ? true : false, required: this.required ? true : false,
}; };

View File

@ -79,9 +79,9 @@ export abstract class BaseInteraction implements Model {
this.channelId = data.channel_id; this.channelId = data.channel_id;
this.applicationId = data.application_id; this.applicationId = data.application_id;
this.version = data.version; this.version = data.version;
this.locale = data.locale;
// @ts-expect-error: vendor error const perms = data.app_permissions;
const perms = data.app_permissions as string;
if (perms) { if (perms) {
this.appPermissions = new Permissions(BigInt(perms)); this.appPermissions = new Permissions(BigInt(perms));
@ -106,6 +106,11 @@ export abstract class BaseInteraction implements Model {
member?: Member; member?: Member;
appPermissions?: Permissions; appPermissions?: Permissions;
/**
* @virtual
*/
locale?: string;
readonly version: 1; readonly version: 1;
responded = false; responded = false;
@ -323,6 +328,7 @@ export class AutoCompleteInteraction extends BaseInteraction implements Model {
this.commandName = data.data!.name; this.commandName = data.data!.name;
this.commandType = data.data!.type; this.commandType = data.data!.type;
this.commandGuildId = data.data!.guild_id; this.commandGuildId = data.data!.guild_id;
this.locale = super.locale!;
} }
override type: InteractionTypes.ApplicationCommandAutocomplete; override type: InteractionTypes.ApplicationCommandAutocomplete;
@ -330,6 +336,7 @@ export class AutoCompleteInteraction extends BaseInteraction implements Model {
commandName: string; commandName: string;
commandType: ApplicationCommandTypes; commandType: ApplicationCommandTypes;
commandGuildId?: Snowflake; commandGuildId?: Snowflake;
override locale: string;
async respondWithChoices( async respondWithChoices(
choices: ApplicationCommandOptionChoice[] choices: ApplicationCommandOptionChoice[]
@ -415,6 +422,8 @@ export class CommandInteraction extends BaseInteraction implements Model {
this.resolved.messages.set(id, new Message(session, m)); this.resolved.messages.set(id, new Message(session, m));
} }
} }
this.locale = super.locale!;
} }
override type: InteractionTypes.ApplicationCommand; override type: InteractionTypes.ApplicationCommand;
@ -424,6 +433,7 @@ export class CommandInteraction extends BaseInteraction implements Model {
commandGuildId?: Snowflake; commandGuildId?: Snowflake;
resolved: CommandInteractionDataResolved; resolved: CommandInteractionDataResolved;
options: CommandInteractionOptionResolver; options: CommandInteractionOptionResolver;
override locale: string;
} }
export type ModalInMessage = ModalSubmitInteraction & { export type ModalInMessage = ModalSubmitInteraction & {
@ -446,6 +456,8 @@ export class ModalSubmitInteraction extends BaseInteraction implements Model {
if (data.message) { if (data.message) {
this.message = new Message(session, data.message); this.message = new Message(session, data.message);
} }
this.locale = super.locale!;
} }
override type: InteractionTypes.MessageComponent; override type: InteractionTypes.MessageComponent;
@ -455,6 +467,7 @@ export class ModalSubmitInteraction extends BaseInteraction implements Model {
values?: string[]; values?: string[];
message?: Message; message?: Message;
components; components;
override locale: string;
static transformComponent(component: DiscordMessageComponents[number]) { static transformComponent(component: DiscordMessageComponents[number]) {
return { return {
@ -482,6 +495,7 @@ export class PingInteraction extends BaseInteraction implements Model {
this.commandName = data.data!.name; this.commandName = data.data!.name;
this.commandType = data.data!.type; this.commandType = data.data!.type;
this.commandGuildId = data.data!.guild_id; this.commandGuildId = data.data!.guild_id;
this.locale = super.locale as undefined;
} }
override type: InteractionTypes.Ping; override type: InteractionTypes.Ping;
@ -489,6 +503,7 @@ export class PingInteraction extends BaseInteraction implements Model {
commandName: string; commandName: string;
commandType: ApplicationCommandTypes; commandType: ApplicationCommandTypes;
commandGuildId?: Snowflake; commandGuildId?: Snowflake;
override locale: undefined;
async pong(): Promise<void> { async pong(): Promise<void> {
await this.session.rest.post<undefined>( await this.session.rest.post<undefined>(
@ -509,6 +524,7 @@ export class ComponentInteraction extends BaseInteraction implements Model {
this.targetId = data.data!.target_id; this.targetId = data.data!.target_id;
this.values = data.data!.values; this.values = data.data!.values;
this.message = new Message(session, data.message!); this.message = new Message(session, data.message!);
this.locale = super.locale!;
} }
override type: InteractionTypes.MessageComponent; override type: InteractionTypes.MessageComponent;
@ -517,6 +533,7 @@ export class ComponentInteraction extends BaseInteraction implements Model {
targetId?: Snowflake; targetId?: Snowflake;
values?: string[]; values?: string[];
message: Message; message: Message;
override locale: string;
isButton(): boolean { isButton(): boolean {
return this.componentType === MessageComponentTypes.Button; return this.componentType === MessageComponentTypes.Button;