Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
feature(role for user)
Browse files Browse the repository at this point in the history
  • Loading branch information
owengombas committed Jun 7, 2021
1 parent 740134a commit de5b622
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 22 deletions.
22 changes: 14 additions & 8 deletions docs/src/decorators/permission.md
Original file line number Diff line number Diff line change
@@ -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()`

Expand All @@ -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(
) {
Expand All @@ -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(
Expand All @@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/decorators/classes/DDiscord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DIService,
DOn
} from "../..";
import { PermissionType } from "../../types";

export class DDiscord extends Decorator {
private _guards: DGuard[] = [];
Expand All @@ -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() {
Expand Down
11 changes: 5 additions & 6 deletions src/decorators/classes/DSlash.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
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 {
private _description: string;
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;
Expand Down Expand Up @@ -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,
}));
}

Expand Down
16 changes: 10 additions & 6 deletions src/decorators/decorators/Permission.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,12 +17,12 @@ export function Permission(...roleIDs: string[]) {
MetadataStorage.instance.addModifier(
Modifier.create<DSlash | DDiscord>(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)
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./core/ChoicesType";
export * from "./public/GuardFunction";
export * from "./public/CommandType";
export * from "./public/ArgsOf";
export * from "./public/PermissionType";
1 change: 1 addition & 0 deletions src/types/public/PermissionType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type PermissionType = "USER" | "ROLE";

0 comments on commit de5b622

Please sign in to comment.