diff --git a/docs/src/decorators/permission.md b/docs/src/decorators/permission.md index 4630805f..bbaa6feb 100644 --- a/docs/src/decorators/permission.md +++ b/docs/src/decorators/permission.md @@ -1,7 +1,7 @@ # @Permission You can set some permissions to your Slash commands -The permissions are based on a **role id** that you specify on the @Permission decorator +The permissions are based on a **role id** or **user id** that you specify on the @Permission decorator The permissions will be set when you call `client.initSlashes()` @@ -13,12 +13,13 @@ Permissions are only available for Guild specific Slash commands > You can manage it by yourself using your own the Slashes `Client` API and creating your own `client.initSlashes()` implementation ## Setup permissions -You just decorate your parameter with one or multiple @Choice ! +You just decorate your parameter with one or multiple @Permission ! ```ts @Discord() class DiscordBot { - @Permission("ROLE_ID", "ROLE_ID2") // Only the role that has this ROLE_ID can use this command + @Permission("USER_ID", "USER") // Only the role that has this USER_ID can use this command + @Permission("ROLE_ID", "ROLE") // Only the role that has this ROLE_ID can use this command @Slash("hello") private hello( ) { @@ -31,7 +32,8 @@ class DiscordBot { You can set the permissions for all @Slash inside the class by decorating the class with @Permission ```ts @Discord() -@Permission("ROLE_ID", "ROLE_ID2") +@Permission("USER_ID", "USER") // Only the role that has this USER_ID can use this command +@Permission("ROLE_ID", "ROLE") // Only the role that has this ROLE_ID can use this command class DiscordBot { @Slash("hello") // Only the role that has this ROLE_ID can use this command private hello( @@ -48,8 +50,12 @@ class DiscordBot { ``` ## Params -`@Permission(...roleIDs: string[])` +`@Permission(id: string, type: "USER" | "ROLE")` -### roleIDs -`string[]` -The roles IDs list +### id +`string` +The id if the user or role + +### type +`"ROLE" | "USER"` +It specify if the permission is given to a user or a role diff --git a/package.json b/package.json index 937e79e8..356cc40e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@typeit/discord", - "version": "5.0.12", + "version": "5.0.13", "description": "Create your discord bot by using TypeScript and decorators!", "main": "index.js", "types": "index.d.ts", diff --git a/src/decorators/classes/DDiscord.ts b/src/decorators/classes/DDiscord.ts index 0c9fb0e2..2a45b472 100644 --- a/src/decorators/classes/DDiscord.ts +++ b/src/decorators/classes/DDiscord.ts @@ -5,6 +5,7 @@ import { DIService, DOn } from "../.."; +import { PermissionType } from "../../types"; export class DDiscord extends Decorator { private _guards: DGuard[] = []; @@ -13,7 +14,7 @@ export class DDiscord extends Decorator { private _description: string; private _name: string; private _defaultPermission: boolean = true; - private _permissions: string[] = []; + private _permissions: { id: string, type: PermissionType }[] = []; private _guilds: string[] = []; get permissions() { diff --git a/src/decorators/classes/DSlash.ts b/src/decorators/classes/DSlash.ts index f550da7b..f9663185 100644 --- a/src/decorators/classes/DSlash.ts +++ b/src/decorators/classes/DSlash.ts @@ -1,12 +1,11 @@ import { ApplicationCommandData, - ApplicationCommandOptionData, ApplicationCommandPermissionData, - Collection, CommandInteraction, CommandInteractionOption, + Snowflake, } from "discord.js"; -import { DOption, Client, SubValueType } from "../.."; +import { DOption, Client, SubValueType, PermissionType } from "../.."; import { Method } from "./Method"; export class DSlash extends Method { @@ -14,7 +13,7 @@ export class DSlash extends Method { private _name: string; private _defaultPermission: boolean = true; private _options: DOption[] = []; - private _permissions: string[] = []; + private _permissions: { id: string, type: PermissionType }[] = []; private _guilds: string[]; private _group: string; private _subgroup: string; @@ -126,8 +125,8 @@ export class DSlash extends Method { getPermissions(): ApplicationCommandPermissionData[] { return this.permissions.map((permission) => ({ permission: true, - id: permission as any, - type: 1, + id: permission.id as Snowflake, + type: permission.type, })); } diff --git a/src/decorators/decorators/Permission.ts b/src/decorators/decorators/Permission.ts index 37faca3f..5dbc2f7f 100644 --- a/src/decorators/decorators/Permission.ts +++ b/src/decorators/decorators/Permission.ts @@ -1,10 +1,14 @@ -import { MetadataStorage, DChoice, DOption, Modifier } from "../.."; +import { MetadataStorage, Modifier, PermissionType } from "../.."; import { DDiscord } from "../classes/DDiscord"; import { DSlash } from "../classes/DSlash"; -export function Permission(roleID: string); -export function Permission(...roleID: string[]); -export function Permission(...roleIDs: string[]) { +export function Permission(id: string, type: PermissionType); +export function Permission(id: string, type: PermissionType) { + const permission = { + id, + type + }; + return async ( target: Object, key: string, @@ -13,12 +17,12 @@ export function Permission(...roleIDs: string[]) { MetadataStorage.instance.addModifier( Modifier.create(async (original) => { original.defaultPermission = false; - original.permissions = [...original.permissions, ...roleIDs]; + original.permissions = [...original.permissions, permission]; if (original instanceof DDiscord) { original.slashes.map((slash) => { slash.defaultPermission = false; - slash.permissions = [...slash.permissions, ...roleIDs]; + slash.permissions = [...slash.permissions, permission]; }); } }, DSlash, DDiscord).decorateUnknown(target, key, descriptor) diff --git a/src/types/index.ts b/src/types/index.ts index bf12250c..e01b2037 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -9,3 +9,4 @@ export * from "./core/ChoicesType"; export * from "./public/GuardFunction"; export * from "./public/CommandType"; export * from "./public/ArgsOf"; +export * from "./public/PermissionType"; diff --git a/src/types/public/PermissionType.ts b/src/types/public/PermissionType.ts new file mode 100644 index 00000000..7ad3164d --- /dev/null +++ b/src/types/public/PermissionType.ts @@ -0,0 +1 @@ +export type PermissionType = "USER" | "ROLE";