Skip to content

Commit

Permalink
refactor: perms-v2 (#483)
Browse files Browse the repository at this point in the history
* refactor: perms-v2 (I am not vladdy)

* chore: comput diffrence correctly

* test: add new field tests

* chore: i'll explain this when i'm sober .. or revert it

permalink: http://whatthecommit.com/f3c558c06e32d9edff83daa4a6487208

Co-authored-by: Jeroen Claassens <support@favware.tech>
  • Loading branch information
r-priyam and favna committed Jul 19, 2022
1 parent 5bc8a0d commit 72b48bb
Show file tree
Hide file tree
Showing 5 changed files with 686 additions and 920 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@
"@sapphire/prettier-config": "^1.4.3",
"@sapphire/ts-config": "^3.3.4",
"@types/jest": "^28.1.6",
"@types/node": "^18.0.0",
"@types/node": "^18.0.6",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.30.6",
"@typescript-eslint/parser": "^5.30.6",
"@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7",
"cz-conventional-changelog": "^3.3.0",
"discord.js": "^13.8.1",
"discord.js": "^13.9.0",
"eslint": "^8.20.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
Expand All @@ -68,7 +68,7 @@
"rollup": "^2.77.0",
"rollup-plugin-version-injector": "^1.3.3",
"ts-jest": "^28.0.7",
"typedoc": "^0.23.7",
"typedoc": "^0.23.8",
"typedoc-plugin-djs-links": "^1.2.0",
"typedoc-plugin-mdn-links": "^2.0.0",
"typescript": "^4.7.4"
Expand Down
36 changes: 36 additions & 0 deletions src/lib/utils/application-commands/computeDifferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ export function getCommandDifferences(existingCommand: RESTPostAPIApplicationCom
});
}

// Check dmPermission
if ((existingCommand.dm_permission ?? true) !== (casted.dm_permission ?? true)) {
differences.push({
key: 'dmPermission',
original: String(existingCommand.dm_permission ?? true),
expected: String(casted.dm_permission ?? true)
});
}

// Check defaultMemberPermissions
if (existingCommand.default_member_permissions !== casted.default_member_permissions) {
differences.push({
key: 'defaultMemberPermissions',
original: String(existingCommand.default_member_permissions),
expected: String(casted.default_member_permissions)
});
}

// Check localized names
const originalLocalizedNames = existingCommand.name_localizations;
const expectedLocalizedNames = casted.name_localizations;
Expand Down Expand Up @@ -128,6 +146,24 @@ export function getCommandDifferences(existingCommand: RESTPostAPIApplicationCom
});
}

// Check dmPermission
if ((existingCommand.dm_permission ?? true) !== (casted.dm_permission ?? true)) {
differences.push({
key: 'dmPermission',
original: String(existingCommand.dm_permission ?? true),
expected: String(casted.dm_permission ?? true)
});
}

// Check defaultMemberPermissions
if (existingCommand.default_member_permissions !== casted.default_member_permissions) {
differences.push({
key: 'defaultMemberPermissions',
original: String(existingCommand.default_member_permissions),
expected: String(casted.default_member_permissions)
});
}

// Check description
if (existingCommand.description !== casted.description) {
differences.push({
Expand Down
24 changes: 18 additions & 6 deletions src/lib/utils/application-commands/normalizeInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ export function normalizeChatInputCommand(
description: command.description,
description_localizations: command.descriptionLocalizations,
default_permission: command.defaultPermission,
type: ApplicationCommandType.ChatInput
// TODO: once command perms v2 drops, add the fields here
type: ApplicationCommandType.ChatInput,
dm_permission: command.dmPermission
};

if (command.defaultMemberPermissions) {
finalObject.default_member_permissions = String(command.defaultMemberPermissions);
}

if (command.options?.length) {
finalObject.options = command.options.map((option) => ApplicationCommand['transformOption'](option) as APIApplicationCommandOption);
}
Expand Down Expand Up @@ -103,21 +107,29 @@ export function normalizeContextMenuCommand(
name: command.name,
name_localizations: command.nameLocalizations,
type,
default_permission: command.defaultPermission
// TODO: once command perms v2 drops, add the fields here
default_permission: command.defaultPermission,
dm_permission: command.dmPermission
};

if (command.defaultMemberPermissions) {
finalObject.default_member_permissions = String(command.defaultMemberPermissions);
}

return finalObject;
}

export function convertApplicationCommandToApiData(command: ApplicationCommand): RESTPostAPIApplicationCommandsJSONBody {
const returnData = {
name: command.name,
name_localizations: command.nameLocalizations,
default_permission: command.defaultPermission
// TODO: once command perms v2 drops, add the fields here
default_permission: command.defaultPermission,
dm_permission: command.dmPermission
} as RESTPostAPIApplicationCommandsJSONBody;

if (command.defaultMemberPermissions) {
returnData.default_member_permissions = String(command.defaultMemberPermissions);
}

if (command.type === 'CHAT_INPUT') {
returnData.type = ApplicationCommandType.ChatInput;
(returnData as RESTPostAPIChatInputApplicationCommandsJSONBody).description = command.description;
Expand Down
Loading

0 comments on commit 72b48bb

Please sign in to comment.