From 9d62ff57d697ed40e92cd2434208db9b82526afa Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Thu, 28 Nov 2024 17:33:40 +0000 Subject: [PATCH] refactor(commands): Use `Locale` over `LocaleString` (#10624) * refactor: use `Locale` * test: update `LocaleString` tests --- .../ChatInputCommands.test.ts | 45 ++++++++++++------- .../interactions/ContextMenuCommands.test.ts | 23 +++++----- .../src/interactions/commands/SharedName.ts | 8 ++-- .../commands/SharedNameAndDescription.ts | 8 ++-- 4 files changed, 49 insertions(+), 35 deletions(-) diff --git a/packages/builders/__tests__/interactions/ChatInputCommands/ChatInputCommands.test.ts b/packages/builders/__tests__/interactions/ChatInputCommands/ChatInputCommands.test.ts index 6f377958d..1b3ed6f82 100644 --- a/packages/builders/__tests__/interactions/ChatInputCommands/ChatInputCommands.test.ts +++ b/packages/builders/__tests__/interactions/ChatInputCommands/ChatInputCommands.test.ts @@ -3,6 +3,7 @@ import { ApplicationIntegrationType, ChannelType, InteractionContextType, + Locale, PermissionFlagsBits, } from 'discord-api-types/v10'; import { describe, test, expect } from 'vitest'; @@ -376,15 +377,15 @@ describe('ChatInput Commands', () => { }); describe('ChatInput command localizations', () => { - const expectedSingleLocale = { 'en-US': 'foobar' }; + const expectedSingleLocale = { [Locale.EnglishUS]: 'foobar' }; const expectedMultipleLocales = { ...expectedSingleLocale, bg: 'test', }; test('GIVEN valid name localizations THEN does not throw error', () => { - expect(() => getBuilder().setNameLocalization('en-US', 'foobar')).not.toThrowError(); - expect(() => getBuilder().setNameLocalizations({ 'en-US': 'foobar' })).not.toThrowError(); + expect(() => getBuilder().setNameLocalization(Locale.EnglishUS, 'foobar')).not.toThrowError(); + expect(() => getBuilder().setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })).not.toThrowError(); }); test('GIVEN invalid name localizations THEN does throw error', () => { @@ -395,21 +396,29 @@ describe('ChatInput Commands', () => { }); test('GIVEN valid name localizations THEN valid data is stored', () => { - expect(getNamedBuilder().setNameLocalization('en-US', 'foobar').toJSON().name_localizations).toEqual( + expect(getNamedBuilder().setNameLocalization(Locale.EnglishUS, 'foobar').toJSON().name_localizations).toEqual( expectedSingleLocale, ); expect( - getNamedBuilder().setNameLocalizations({ 'en-US': 'foobar', bg: 'test' }).toJSON().name_localizations, + getNamedBuilder() + .setNameLocalizations({ [Locale.EnglishUS]: 'foobar', bg: 'test' }) + .toJSON().name_localizations, ).toEqual(expectedMultipleLocales); expect(getNamedBuilder().clearNameLocalizations().toJSON().name_localizations).toBeUndefined(); - expect(getNamedBuilder().clearNameLocalization('en-US').toJSON().name_localizations).toEqual({ - 'en-US': undefined, + expect(getNamedBuilder().clearNameLocalization(Locale.EnglishUS).toJSON().name_localizations).toEqual({ + [Locale.EnglishUS]: undefined, }); }); test('GIVEN valid description localizations THEN does not throw error', () => { - expect(() => getNamedBuilder().setDescriptionLocalization('en-US', 'foobar').toJSON()).not.toThrowError(); - expect(() => getNamedBuilder().setDescriptionLocalizations({ 'en-US': 'foobar' }).toJSON()).not.toThrowError(); + expect(() => + getNamedBuilder().setDescriptionLocalization(Locale.EnglishUS, 'foobar').toJSON(), + ).not.toThrowError(); + expect(() => + getNamedBuilder() + .setDescriptionLocalizations({ [Locale.EnglishUS]: 'foobar' }) + .toJSON(), + ).not.toThrowError(); }); test('GIVEN invalid description localizations THEN does throw error', () => { @@ -421,20 +430,22 @@ describe('ChatInput Commands', () => { test('GIVEN valid description localizations THEN valid data is stored', () => { expect( - getNamedBuilder().setDescriptionLocalization('en-US', 'foobar').toJSON(false).description_localizations, + getNamedBuilder().setDescriptionLocalization(Locale.EnglishUS, 'foobar').toJSON(false) + .description_localizations, ).toEqual(expectedSingleLocale); expect( - getNamedBuilder().setDescriptionLocalizations({ 'en-US': 'foobar', bg: 'test' }).toJSON(false) - .description_localizations, + getNamedBuilder() + .setDescriptionLocalizations({ [Locale.EnglishUS]: 'foobar', bg: 'test' }) + .toJSON(false).description_localizations, ).toEqual(expectedMultipleLocales); expect( getNamedBuilder().clearDescriptionLocalizations().toJSON(false).description_localizations, ).toBeUndefined(); - expect(getNamedBuilder().clearDescriptionLocalization('en-US').toJSON(false).description_localizations).toEqual( - { - 'en-US': undefined, - }, - ); + expect( + getNamedBuilder().clearDescriptionLocalization(Locale.EnglishUS).toJSON(false).description_localizations, + ).toEqual({ + [Locale.EnglishUS]: undefined, + }); }); }); diff --git a/packages/builders/__tests__/interactions/ContextMenuCommands.test.ts b/packages/builders/__tests__/interactions/ContextMenuCommands.test.ts index 315816b04..c430427a6 100644 --- a/packages/builders/__tests__/interactions/ContextMenuCommands.test.ts +++ b/packages/builders/__tests__/interactions/ContextMenuCommands.test.ts @@ -2,6 +2,7 @@ import { ApplicationCommandType, ApplicationIntegrationType, InteractionContextType, + Locale, PermissionFlagsBits, } from 'discord-api-types/v10'; import { describe, test, expect } from 'vitest'; @@ -36,15 +37,15 @@ describe('Context Menu Commands', () => { }); describe('Context menu command localizations', () => { - const expectedSingleLocale = { 'en-US': 'foobar' }; + const expectedSingleLocale = { [Locale.EnglishUS]: 'foobar' }; const expectedMultipleLocales = { ...expectedSingleLocale, bg: 'test', }; test('GIVEN valid name localizations THEN does not throw error', () => { - expect(() => getBuilder().setNameLocalization('en-US', 'foobar')).not.toThrowError(); - expect(() => getBuilder().setNameLocalizations({ 'en-US': 'foobar' })).not.toThrowError(); + expect(() => getBuilder().setNameLocalization(Locale.EnglishUS, 'foobar')).not.toThrowError(); + expect(() => getBuilder().setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })).not.toThrowError(); }); test('GIVEN invalid name localizations THEN does throw error', () => { @@ -55,16 +56,18 @@ describe('Context Menu Commands', () => { }); test('GIVEN valid name localizations THEN valid data is stored', () => { - expect(getBuilder().setName('hi').setNameLocalization('en-US', 'foobar').toJSON().name_localizations).toEqual( - expectedSingleLocale, - ); expect( - getBuilder().setName('hi').setNameLocalizations({ 'en-US': 'foobar', bg: 'test' }).toJSON() - .name_localizations, + getBuilder().setName('hi').setNameLocalization(Locale.EnglishUS, 'foobar').toJSON().name_localizations, + ).toEqual(expectedSingleLocale); + expect( + getBuilder() + .setName('hi') + .setNameLocalizations({ [Locale.EnglishUS]: 'foobar', bg: 'test' }) + .toJSON().name_localizations, ).toEqual(expectedMultipleLocales); expect(getBuilder().setName('hi').clearNameLocalizations().toJSON().name_localizations).toBeUndefined(); - expect(getBuilder().setName('hi').clearNameLocalization('en-US').toJSON().name_localizations).toEqual({ - 'en-US': undefined, + expect(getBuilder().setName('hi').clearNameLocalization(Locale.EnglishUS).toJSON().name_localizations).toEqual({ + [Locale.EnglishUS]: undefined, }); }); }); diff --git a/packages/builders/src/interactions/commands/SharedName.ts b/packages/builders/src/interactions/commands/SharedName.ts index 18df41973..798a055ff 100644 --- a/packages/builders/src/interactions/commands/SharedName.ts +++ b/packages/builders/src/interactions/commands/SharedName.ts @@ -1,4 +1,4 @@ -import type { LocaleString, RESTPostAPIApplicationCommandsJSONBody } from 'discord-api-types/v10'; +import type { Locale, RESTPostAPIApplicationCommandsJSONBody } from 'discord-api-types/v10'; export interface SharedNameData extends Partial> {} @@ -25,7 +25,7 @@ export class SharedName { * @param locale - The locale to set * @param localizedName - The localized name for the given `locale` */ - public setNameLocalization(locale: LocaleString, localizedName: string) { + public setNameLocalization(locale: Locale, localizedName: string) { this.data.name_localizations ??= {}; this.data.name_localizations[locale] = localizedName; @@ -37,7 +37,7 @@ export class SharedName { * * @param locale - The locale to clear */ - public clearNameLocalization(locale: LocaleString) { + public clearNameLocalization(locale: Locale) { this.data.name_localizations ??= {}; this.data.name_localizations[locale] = undefined; @@ -49,7 +49,7 @@ export class SharedName { * * @param localizedNames - The object of localized names to set */ - public setNameLocalizations(localizedNames: Partial>) { + public setNameLocalizations(localizedNames: Partial>) { this.data.name_localizations = structuredClone(localizedNames); return this; } diff --git a/packages/builders/src/interactions/commands/SharedNameAndDescription.ts b/packages/builders/src/interactions/commands/SharedNameAndDescription.ts index 4a12202db..e6b87478e 100644 --- a/packages/builders/src/interactions/commands/SharedNameAndDescription.ts +++ b/packages/builders/src/interactions/commands/SharedNameAndDescription.ts @@ -1,4 +1,4 @@ -import type { APIApplicationCommand, LocaleString } from 'discord-api-types/v10'; +import type { APIApplicationCommand, Locale } from 'discord-api-types/v10'; import type { SharedNameData } from './SharedName.js'; import { SharedName } from './SharedName.js'; @@ -28,7 +28,7 @@ export class SharedNameAndDescription extends SharedName { * @param locale - The locale to set * @param localizedDescription - The localized description for the given `locale` */ - public setDescriptionLocalization(locale: LocaleString, localizedDescription: string) { + public setDescriptionLocalization(locale: Locale, localizedDescription: string) { this.data.description_localizations ??= {}; this.data.description_localizations[locale] = localizedDescription; @@ -40,7 +40,7 @@ export class SharedNameAndDescription extends SharedName { * * @param locale - The locale to clear */ - public clearDescriptionLocalization(locale: LocaleString) { + public clearDescriptionLocalization(locale: Locale) { this.data.description_localizations ??= {}; this.data.description_localizations[locale] = undefined; @@ -52,7 +52,7 @@ export class SharedNameAndDescription extends SharedName { * * @param localizedDescriptions - The object of localized descriptions to set */ - public setDescriptionLocalizations(localizedDescriptions: Partial>) { + public setDescriptionLocalizations(localizedDescriptions: Partial>) { this.data.description_localizations = structuredClone(localizedDescriptions); return this; }