Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/plugins/PermissionsViewer/perms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface Description {
interface PermissionSpecification {
title: string;
flag: bigint;
description: ((locale: string) => Description) | string[];
description: ((locale: string) => Description) | string[] | string;
}

interface PermissionSpecCategory {
Expand Down Expand Up @@ -62,7 +62,7 @@ export function getDefinitions(guildIdOrGuild?: string | Guild): PermissionCateg
permissions: category.permissions.map(perm => ({
id: Object.keys(DiscordPermissions).find(key => DiscordPermissions[key as keyof IDiscordPermissions] === perm.flag) ?? "",
name: perm.title,
description: typeof perm.description === "function" ? perm.description(BdApi.Webpack.Stores.LocaleStore.locale).ast[0] : perm.description[0]
description: typeof perm.description === "function" ? perm.description(BdApi.Webpack.Stores.LocaleStore.locale).ast[0] : (Array.isArray(perm.description) ? perm.description[0] : (perm.description || ""))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current fallback logic (perm.description || "") is good, but it doesn't protect against cases where perm.description is a function that returns an empty ast array, or when perm.description is an empty array itself. In those cases, ast[0] or description[0] will be undefined, which violates the PermissionDefinition interface requiring description to be a string. Using the nullish coalescing operator (?? "") at the end of the entire ternary expression ensures that a string is always returned in all scenarios.

Suggested change
description: typeof perm.description === "function" ? perm.description(BdApi.Webpack.Stores.LocaleStore.locale).ast[0] : (Array.isArray(perm.description) ? perm.description[0] : (perm.description || ""))
description: (typeof perm.description === "function" ? perm.description(BdApi.Webpack.Stores.LocaleStore.locale).ast[0] : (Array.isArray(perm.description) ? perm.description[0] : perm.description)) ?? ""

}))
}));
}
Expand Down
Loading