mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 19:43:29 +01:00
refactor(commands): Use Locale over LocaleString (#10624)
* refactor: use `Locale` * test: update `LocaleString` tests
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
|||||||
ApplicationIntegrationType,
|
ApplicationIntegrationType,
|
||||||
ChannelType,
|
ChannelType,
|
||||||
InteractionContextType,
|
InteractionContextType,
|
||||||
|
Locale,
|
||||||
PermissionFlagsBits,
|
PermissionFlagsBits,
|
||||||
} from 'discord-api-types/v10';
|
} from 'discord-api-types/v10';
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
@@ -376,15 +377,15 @@ describe('ChatInput Commands', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('ChatInput command localizations', () => {
|
describe('ChatInput command localizations', () => {
|
||||||
const expectedSingleLocale = { 'en-US': 'foobar' };
|
const expectedSingleLocale = { [Locale.EnglishUS]: 'foobar' };
|
||||||
const expectedMultipleLocales = {
|
const expectedMultipleLocales = {
|
||||||
...expectedSingleLocale,
|
...expectedSingleLocale,
|
||||||
bg: 'test',
|
bg: 'test',
|
||||||
};
|
};
|
||||||
|
|
||||||
test('GIVEN valid name localizations THEN does not throw error', () => {
|
test('GIVEN valid name localizations THEN does not throw error', () => {
|
||||||
expect(() => getBuilder().setNameLocalization('en-US', 'foobar')).not.toThrowError();
|
expect(() => getBuilder().setNameLocalization(Locale.EnglishUS, 'foobar')).not.toThrowError();
|
||||||
expect(() => getBuilder().setNameLocalizations({ 'en-US': 'foobar' })).not.toThrowError();
|
expect(() => getBuilder().setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })).not.toThrowError();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('GIVEN invalid name localizations THEN does throw error', () => {
|
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', () => {
|
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,
|
expectedSingleLocale,
|
||||||
);
|
);
|
||||||
expect(
|
expect(
|
||||||
getNamedBuilder().setNameLocalizations({ 'en-US': 'foobar', bg: 'test' }).toJSON().name_localizations,
|
getNamedBuilder()
|
||||||
|
.setNameLocalizations({ [Locale.EnglishUS]: 'foobar', bg: 'test' })
|
||||||
|
.toJSON().name_localizations,
|
||||||
).toEqual(expectedMultipleLocales);
|
).toEqual(expectedMultipleLocales);
|
||||||
expect(getNamedBuilder().clearNameLocalizations().toJSON().name_localizations).toBeUndefined();
|
expect(getNamedBuilder().clearNameLocalizations().toJSON().name_localizations).toBeUndefined();
|
||||||
expect(getNamedBuilder().clearNameLocalization('en-US').toJSON().name_localizations).toEqual({
|
expect(getNamedBuilder().clearNameLocalization(Locale.EnglishUS).toJSON().name_localizations).toEqual({
|
||||||
'en-US': undefined,
|
[Locale.EnglishUS]: undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('GIVEN valid description localizations THEN does not throw error', () => {
|
test('GIVEN valid description localizations THEN does not throw error', () => {
|
||||||
expect(() => getNamedBuilder().setDescriptionLocalization('en-US', 'foobar').toJSON()).not.toThrowError();
|
expect(() =>
|
||||||
expect(() => getNamedBuilder().setDescriptionLocalizations({ 'en-US': 'foobar' }).toJSON()).not.toThrowError();
|
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', () => {
|
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', () => {
|
test('GIVEN valid description localizations THEN valid data is stored', () => {
|
||||||
expect(
|
expect(
|
||||||
getNamedBuilder().setDescriptionLocalization('en-US', 'foobar').toJSON(false).description_localizations,
|
getNamedBuilder().setDescriptionLocalization(Locale.EnglishUS, 'foobar').toJSON(false)
|
||||||
|
.description_localizations,
|
||||||
).toEqual(expectedSingleLocale);
|
).toEqual(expectedSingleLocale);
|
||||||
expect(
|
expect(
|
||||||
getNamedBuilder().setDescriptionLocalizations({ 'en-US': 'foobar', bg: 'test' }).toJSON(false)
|
getNamedBuilder()
|
||||||
.description_localizations,
|
.setDescriptionLocalizations({ [Locale.EnglishUS]: 'foobar', bg: 'test' })
|
||||||
|
.toJSON(false).description_localizations,
|
||||||
).toEqual(expectedMultipleLocales);
|
).toEqual(expectedMultipleLocales);
|
||||||
expect(
|
expect(
|
||||||
getNamedBuilder().clearDescriptionLocalizations().toJSON(false).description_localizations,
|
getNamedBuilder().clearDescriptionLocalizations().toJSON(false).description_localizations,
|
||||||
).toBeUndefined();
|
).toBeUndefined();
|
||||||
expect(getNamedBuilder().clearDescriptionLocalization('en-US').toJSON(false).description_localizations).toEqual(
|
expect(
|
||||||
{
|
getNamedBuilder().clearDescriptionLocalization(Locale.EnglishUS).toJSON(false).description_localizations,
|
||||||
'en-US': undefined,
|
).toEqual({
|
||||||
},
|
[Locale.EnglishUS]: undefined,
|
||||||
);
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import {
|
|||||||
ApplicationCommandType,
|
ApplicationCommandType,
|
||||||
ApplicationIntegrationType,
|
ApplicationIntegrationType,
|
||||||
InteractionContextType,
|
InteractionContextType,
|
||||||
|
Locale,
|
||||||
PermissionFlagsBits,
|
PermissionFlagsBits,
|
||||||
} from 'discord-api-types/v10';
|
} from 'discord-api-types/v10';
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
@@ -36,15 +37,15 @@ describe('Context Menu Commands', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('Context menu command localizations', () => {
|
describe('Context menu command localizations', () => {
|
||||||
const expectedSingleLocale = { 'en-US': 'foobar' };
|
const expectedSingleLocale = { [Locale.EnglishUS]: 'foobar' };
|
||||||
const expectedMultipleLocales = {
|
const expectedMultipleLocales = {
|
||||||
...expectedSingleLocale,
|
...expectedSingleLocale,
|
||||||
bg: 'test',
|
bg: 'test',
|
||||||
};
|
};
|
||||||
|
|
||||||
test('GIVEN valid name localizations THEN does not throw error', () => {
|
test('GIVEN valid name localizations THEN does not throw error', () => {
|
||||||
expect(() => getBuilder().setNameLocalization('en-US', 'foobar')).not.toThrowError();
|
expect(() => getBuilder().setNameLocalization(Locale.EnglishUS, 'foobar')).not.toThrowError();
|
||||||
expect(() => getBuilder().setNameLocalizations({ 'en-US': 'foobar' })).not.toThrowError();
|
expect(() => getBuilder().setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })).not.toThrowError();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('GIVEN invalid name localizations THEN does throw error', () => {
|
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', () => {
|
test('GIVEN valid name localizations THEN valid data is stored', () => {
|
||||||
expect(getBuilder().setName('hi').setNameLocalization('en-US', 'foobar').toJSON().name_localizations).toEqual(
|
|
||||||
expectedSingleLocale,
|
|
||||||
);
|
|
||||||
expect(
|
expect(
|
||||||
getBuilder().setName('hi').setNameLocalizations({ 'en-US': 'foobar', bg: 'test' }).toJSON()
|
getBuilder().setName('hi').setNameLocalization(Locale.EnglishUS, 'foobar').toJSON().name_localizations,
|
||||||
.name_localizations,
|
).toEqual(expectedSingleLocale);
|
||||||
|
expect(
|
||||||
|
getBuilder()
|
||||||
|
.setName('hi')
|
||||||
|
.setNameLocalizations({ [Locale.EnglishUS]: 'foobar', bg: 'test' })
|
||||||
|
.toJSON().name_localizations,
|
||||||
).toEqual(expectedMultipleLocales);
|
).toEqual(expectedMultipleLocales);
|
||||||
expect(getBuilder().setName('hi').clearNameLocalizations().toJSON().name_localizations).toBeUndefined();
|
expect(getBuilder().setName('hi').clearNameLocalizations().toJSON().name_localizations).toBeUndefined();
|
||||||
expect(getBuilder().setName('hi').clearNameLocalization('en-US').toJSON().name_localizations).toEqual({
|
expect(getBuilder().setName('hi').clearNameLocalization(Locale.EnglishUS).toJSON().name_localizations).toEqual({
|
||||||
'en-US': undefined,
|
[Locale.EnglishUS]: undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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
|
export interface SharedNameData
|
||||||
extends Partial<Pick<RESTPostAPIApplicationCommandsJSONBody, 'name_localizations' | 'name'>> {}
|
extends Partial<Pick<RESTPostAPIApplicationCommandsJSONBody, 'name_localizations' | 'name'>> {}
|
||||||
@@ -25,7 +25,7 @@ export class SharedName {
|
|||||||
* @param locale - The locale to set
|
* @param locale - The locale to set
|
||||||
* @param localizedName - The localized name for the given `locale`
|
* @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 ??= {};
|
||||||
this.data.name_localizations[locale] = localizedName;
|
this.data.name_localizations[locale] = localizedName;
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ export class SharedName {
|
|||||||
*
|
*
|
||||||
* @param locale - The locale to clear
|
* @param locale - The locale to clear
|
||||||
*/
|
*/
|
||||||
public clearNameLocalization(locale: LocaleString) {
|
public clearNameLocalization(locale: Locale) {
|
||||||
this.data.name_localizations ??= {};
|
this.data.name_localizations ??= {};
|
||||||
this.data.name_localizations[locale] = undefined;
|
this.data.name_localizations[locale] = undefined;
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ export class SharedName {
|
|||||||
*
|
*
|
||||||
* @param localizedNames - The object of localized names to set
|
* @param localizedNames - The object of localized names to set
|
||||||
*/
|
*/
|
||||||
public setNameLocalizations(localizedNames: Partial<Record<LocaleString, string>>) {
|
public setNameLocalizations(localizedNames: Partial<Record<Locale, string>>) {
|
||||||
this.data.name_localizations = structuredClone(localizedNames);
|
this.data.name_localizations = structuredClone(localizedNames);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 type { SharedNameData } from './SharedName.js';
|
||||||
import { SharedName } from './SharedName.js';
|
import { SharedName } from './SharedName.js';
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ export class SharedNameAndDescription extends SharedName {
|
|||||||
* @param locale - The locale to set
|
* @param locale - The locale to set
|
||||||
* @param localizedDescription - The localized description for the given `locale`
|
* @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 ??= {};
|
||||||
this.data.description_localizations[locale] = localizedDescription;
|
this.data.description_localizations[locale] = localizedDescription;
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ export class SharedNameAndDescription extends SharedName {
|
|||||||
*
|
*
|
||||||
* @param locale - The locale to clear
|
* @param locale - The locale to clear
|
||||||
*/
|
*/
|
||||||
public clearDescriptionLocalization(locale: LocaleString) {
|
public clearDescriptionLocalization(locale: Locale) {
|
||||||
this.data.description_localizations ??= {};
|
this.data.description_localizations ??= {};
|
||||||
this.data.description_localizations[locale] = undefined;
|
this.data.description_localizations[locale] = undefined;
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ export class SharedNameAndDescription extends SharedName {
|
|||||||
*
|
*
|
||||||
* @param localizedDescriptions - The object of localized descriptions to set
|
* @param localizedDescriptions - The object of localized descriptions to set
|
||||||
*/
|
*/
|
||||||
public setDescriptionLocalizations(localizedDescriptions: Partial<Record<LocaleString, string>>) {
|
public setDescriptionLocalizations(localizedDescriptions: Partial<Record<Locale, string>>) {
|
||||||
this.data.description_localizations = structuredClone(localizedDescriptions);
|
this.data.description_localizations = structuredClone(localizedDescriptions);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user