necord 4.1.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,10 +4,12 @@ import { NecordContextType } from './necord-execution-context';
4
4
  export interface INecordArgumentsHost extends ArgumentsHost {
5
5
  getContext<T = any>(): T;
6
6
  getOptions<T = any>(): T;
7
+ getInfo<T = any>(): T;
7
8
  }
8
9
  export declare class NecordArgumentsHost extends ExecutionContextHost implements INecordArgumentsHost {
9
10
  static create(context: ArgumentsHost): NecordArgumentsHost;
10
11
  getType<TContext extends string = NecordContextType>(): TContext;
11
12
  getContext<T = any>(): T;
12
13
  getOptions<T = any>(): T;
14
+ getInfo<T = any>(): T;
13
15
  }
@@ -18,5 +18,8 @@ class NecordArgumentsHost extends execution_context_host_1.ExecutionContextHost
18
18
  getOptions() {
19
19
  return this.getArgByIndex(1);
20
20
  }
21
+ getInfo() {
22
+ return this.getArgByIndex(2);
23
+ }
21
24
  }
22
25
  exports.NecordArgumentsHost = NecordArgumentsHost;
@@ -2,7 +2,8 @@ import { ParamData } from '@nestjs/common';
2
2
  import { ParamsFactory } from '@nestjs/core/helpers/external-context-creator';
3
3
  export declare enum NecordParamType {
4
4
  CONTEXT = 0,
5
- OPTIONS = 1
5
+ OPTIONS = 1,
6
+ INFO = 2
6
7
  }
7
8
  export declare class NecordParamsFactory implements ParamsFactory {
8
9
  exchangeKeyForValue(type: number, data: ParamData, args: unknown): any;
@@ -5,6 +5,7 @@ var NecordParamType;
5
5
  (function (NecordParamType) {
6
6
  NecordParamType[NecordParamType["CONTEXT"] = 0] = "CONTEXT";
7
7
  NecordParamType[NecordParamType["OPTIONS"] = 1] = "OPTIONS";
8
+ NecordParamType[NecordParamType["INFO"] = 2] = "INFO";
8
9
  })(NecordParamType = exports.NecordParamType || (exports.NecordParamType = {}));
9
10
  class NecordParamsFactory {
10
11
  exchangeKeyForValue(type, data, args) {
@@ -14,7 +15,9 @@ class NecordParamsFactory {
14
15
  case NecordParamType.CONTEXT:
15
16
  return args[0];
16
17
  case NecordParamType.OPTIONS:
17
- return data ? args[1][data] : args[1];
18
+ return data && args[1] ? args[1][data] : args[1];
19
+ case NecordParamType.INFO:
20
+ return args[2];
18
21
  default:
19
22
  return null;
20
23
  }
@@ -1,3 +1,2 @@
1
- import { ApplicationCommandPermissionData, Snowflake } from 'discord.js';
1
+ import { Snowflake } from 'discord.js';
2
2
  export declare const Guilds: (guildIds: Snowflake[]) => ClassDecorator & MethodDecorator;
3
- export declare const Permissions: (permissions: ApplicationCommandPermissionData[]) => ClassDecorator & MethodDecorator;
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Permissions = exports.Guilds = void 0;
3
+ exports.Guilds = void 0;
4
4
  const common_1 = require("@nestjs/common");
5
5
  const necord_constants_1 = require("../necord.constants");
6
6
  const Guilds = (guildIds) => (0, common_1.SetMetadata)(necord_constants_1.GUILDS_METADATA, guildIds);
7
7
  exports.Guilds = Guilds;
8
- const Permissions = (permissions) => (0, common_1.SetMetadata)(necord_constants_1.PERMISSIONS_METADATA, permissions);
9
- exports.Permissions = Permissions;
@@ -5,3 +5,4 @@ export * from './guilds.decorator';
5
5
  export * from './listeners.decorator';
6
6
  export * from './options.decorator';
7
7
  export * from './params.decorator';
8
+ export * from './permissions.decorator';
@@ -17,3 +17,4 @@ __exportStar(require("./guilds.decorator"), exports);
17
17
  __exportStar(require("./listeners.decorator"), exports);
18
18
  __exportStar(require("./options.decorator"), exports);
19
19
  __exportStar(require("./params.decorator"), exports);
20
+ __exportStar(require("./permissions.decorator"), exports);
@@ -1,4 +1,5 @@
1
1
  export declare const Context: (...pipes: (import("@nestjs/common").PipeTransform<any, any> | import("@nestjs/common").Type<import("@nestjs/common").PipeTransform<any, any>>)[]) => ParameterDecorator;
2
2
  export declare const Options: (data?: any, ...pipes: (import("@nestjs/common").PipeTransform<any, any> | import("@nestjs/common").Type<import("@nestjs/common").PipeTransform<any, any>>)[]) => ParameterDecorator;
3
+ export declare const Info: (data?: any, ...pipes: (import("@nestjs/common").PipeTransform<any, any> | import("@nestjs/common").Type<import("@nestjs/common").PipeTransform<any, any>>)[]) => ParameterDecorator;
3
4
  export declare const Ctx: (...pipes: (import("@nestjs/common").PipeTransform<any, any> | import("@nestjs/common").Type<import("@nestjs/common").PipeTransform<any, any>>)[]) => ParameterDecorator;
4
5
  export declare const Opts: (data?: any, ...pipes: (import("@nestjs/common").PipeTransform<any, any> | import("@nestjs/common").Type<import("@nestjs/common").PipeTransform<any, any>>)[]) => ParameterDecorator;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Opts = exports.Ctx = exports.Options = exports.Context = void 0;
3
+ exports.Opts = exports.Ctx = exports.Info = exports.Options = exports.Context = void 0;
4
4
  const context_1 = require("../context");
5
5
  const necord_constants_1 = require("../necord.constants");
6
6
  const params_util_1 = require("./params.util");
@@ -12,5 +12,6 @@ exports.Options = (0, params_util_1.createNecordPipesParamDecorator)(context_1.N
12
12
  Reflect.defineMetadata(necord_constants_1.OPTIONS_METADATA, options, target[propertyKey]);
13
13
  }
14
14
  ]);
15
+ exports.Info = (0, params_util_1.createNecordPipesParamDecorator)(context_1.NecordParamType.INFO);
15
16
  exports.Ctx = exports.Context;
16
17
  exports.Opts = exports.Options;
@@ -0,0 +1,6 @@
1
+ import { ApplicationCommandPermissionData } from 'discord.js';
2
+ export declare const Permissions: (...permissions: TypedPermissions<unknown>) => ClassDecorator & MethodDecorator;
3
+ export declare const UserPermissions: (...permissions: TypedPermissions<string>) => ClassDecorator & MethodDecorator;
4
+ export declare const RolePermissions: (...permissions: TypedPermissions<string>) => ClassDecorator & MethodDecorator;
5
+ declare type TypedPermissions<T> = Omit<ApplicationCommandPermissionData, T extends string ? 'type' : never>[];
6
+ export {};
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RolePermissions = exports.UserPermissions = exports.Permissions = void 0;
4
+ const necord_constants_1 = require("../necord.constants");
5
+ exports.Permissions = createNecordPermissionsDecorator();
6
+ exports.UserPermissions = createNecordPermissionsDecorator('USER');
7
+ exports.RolePermissions = createNecordPermissionsDecorator('ROLE');
8
+ function createNecordPermissionsDecorator(type) {
9
+ return (...permissions) => (target, propertyKey) => {
10
+ var _a, _b;
11
+ const metadataTarget = (_a = target[propertyKey]) !== null && _a !== void 0 ? _a : target;
12
+ const existing = (_b = Reflect.getMetadata(necord_constants_1.PERMISSIONS_METADATA, metadataTarget)) !== null && _b !== void 0 ? _b : [];
13
+ Reflect.defineMetadata(necord_constants_1.PERMISSIONS_METADATA, existing
14
+ .concat(permissions)
15
+ .map(permission => { var _a; return (Object.assign(Object.assign({}, permission), { type: (_a = permission.type) !== null && _a !== void 0 ? _a : type })); }), metadataTarget);
16
+ };
17
+ }
@@ -1,5 +1,9 @@
1
- import { ClientEvents, DMChannel, Guild, GuildFeatures, GuildMember, Message, NonThreadGuildBasedChannel, PartialMessage, PermissionOverwriteManager, Permissions, PremiumTier, PresenceStatus, Role, ThreadChannel, User, UserFlags, VoiceBasedChannel, VoiceChannel } from 'discord.js';
1
+ import { ClientEvents, CommandInteraction, DMChannel, Guild, GuildFeatures, GuildMember, Message, MessageContextMenuInteraction, NonThreadGuildBasedChannel, PartialMessage, PermissionOverwriteManager, Permissions, PremiumTier, PresenceStatus, Role, ThreadChannel, User, UserContextMenuInteraction, UserFlags, VoiceBasedChannel, VoiceChannel } from 'discord.js';
2
2
  export interface NecordEvents extends ClientEvents {
3
+ textCommand: [Message];
4
+ slashCommand: [CommandInteraction];
5
+ messageContextCommand: [MessageContextMenuInteraction];
6
+ userContextCommand: [UserContextMenuInteraction];
3
7
  guildChannelPermissionsUpdate: [
4
8
  channel: DMChannel | NonThreadGuildBasedChannel,
5
9
  oldPermissions: PermissionOverwriteManager,
@@ -48,6 +48,10 @@ let NecordExplorer = class NecordExplorer {
48
48
  {
49
49
  key: necord_constants_1.GUILDS_METADATA,
50
50
  fn: (key, targets) => this.reflector.getAllAndOverride(key, targets)
51
+ },
52
+ {
53
+ key: necord_constants_1.PERMISSIONS_METADATA,
54
+ fn: (key, targets) => this.reflector.getAllAndMerge(key, targets)
51
55
  }
52
56
  ];
53
57
  for (const command of this.filterProperties(wrapper, metadataKey, optionalKeys)) {
@@ -61,7 +65,7 @@ let NecordExplorer = class NecordExplorer {
61
65
  : subCommands.push(command);
62
66
  }
63
67
  if (commandGroup) {
64
- commandGroup.metadata = this.extractOptionalMetadata([necord_constants_1.GUILDS_METADATA], wrapper.instance);
68
+ commandGroup.metadata = this.extractOptionalMetadata([necord_constants_1.GUILDS_METADATA, necord_constants_1.PERMISSIONS_METADATA], wrapper.instance);
65
69
  commandGroup.options = [...subGroups.values(), ...subCommands];
66
70
  }
67
71
  return commands.concat(commandGroup);
@@ -28,7 +28,6 @@ const decorators_1 = require("./decorators");
28
28
  const necord_registry_1 = require("./necord-registry");
29
29
  const necord_constants_1 = require("./necord.constants");
30
30
  const core_1 = require("@nestjs/core");
31
- const constants_1 = require("@nestjs/core/injector/constants");
32
31
  let NecordInteractionUpdate = NecordInteractionUpdate_1 = class NecordInteractionUpdate {
33
32
  constructor(registry, options) {
34
33
  this.registry = registry;
@@ -41,16 +40,40 @@ let NecordInteractionUpdate = NecordInteractionUpdate_1 = class NecordInteractio
41
40
  if (client.application.partial) {
42
41
  yield client.application.fetch();
43
42
  }
44
- const commands = new Map([[undefined, []]]);
43
+ const clientCommands = client.application.commands;
44
+ const commandsByGuildMap = new Map([[undefined, []]]);
45
45
  for (const command of this.registry.getApplicationCommands()) {
46
46
  const guilds = (_a = command.metadata[necord_constants_1.GUILDS_METADATA]) !== null && _a !== void 0 ? _a : [].concat(this.options.development);
47
47
  for (const guild of guilds) {
48
- const cmds = (_b = commands.get(guild)) !== null && _b !== void 0 ? _b : [];
49
- commands.set(guild, cmds.concat(command));
48
+ const visitedCommands = (_b = commandsByGuildMap.get(guild)) !== null && _b !== void 0 ? _b : [];
49
+ commandsByGuildMap.set(guild, visitedCommands.concat(command));
50
50
  }
51
51
  }
52
52
  this.logger.log(`Started refreshing application commands.`);
53
- yield Promise.all([...commands.entries()].map(([key, cmds]) => client.application.commands.set(cmds, key)));
53
+ for (const [guild, commands] of commandsByGuildMap.entries()) {
54
+ const registeredCommands = yield clientCommands.set(commands, guild);
55
+ const fullPermissions = commands.map(command => {
56
+ const applicationCommand = registeredCommands.find(x => x.name === command.name);
57
+ return {
58
+ id: applicationCommand.id,
59
+ permissions: command.metadata[necord_constants_1.PERMISSIONS_METADATA]
60
+ };
61
+ });
62
+ if (guild) {
63
+ yield clientCommands.permissions.set({
64
+ guild,
65
+ fullPermissions
66
+ });
67
+ continue;
68
+ }
69
+ for (const perm of fullPermissions) {
70
+ yield clientCommands.permissions.set({
71
+ guild,
72
+ command: perm.id,
73
+ permissions: perm.permissions
74
+ });
75
+ }
76
+ }
54
77
  this.logger.log(`Successfully reloaded application commands.`);
55
78
  });
56
79
  }
@@ -59,7 +82,7 @@ let NecordInteractionUpdate = NecordInteractionUpdate_1 = class NecordInteractio
59
82
  return __awaiter(this, void 0, void 0, function* () {
60
83
  if (interaction.isMessageComponent()) {
61
84
  return (_a = this.registry
62
- .getMessageComponent(interaction.componentType, interaction.customId)) === null || _a === void 0 ? void 0 : _a.metadata.execute([interaction], interaction.isSelectMenu() ? interaction.values : undefined);
85
+ .getMessageComponent(interaction.componentType, interaction.customId)) === null || _a === void 0 ? void 0 : _a.metadata.execute([interaction], interaction.isSelectMenu() ? interaction.values : undefined, { type: 'messageComponent' });
63
86
  }
64
87
  if (interaction.isContextMenu()) {
65
88
  const options = interaction.isUserContextMenu()
@@ -69,7 +92,9 @@ let NecordInteractionUpdate = NecordInteractionUpdate_1 = class NecordInteractio
69
92
  }
70
93
  : { message: interaction.options.getMessage('message', false) };
71
94
  return (_b = this.registry
72
- .getContextMenu(interaction.targetType, interaction.commandName)) === null || _b === void 0 ? void 0 : _b.metadata.execute([interaction], options);
95
+ .getContextMenu(interaction.targetType, interaction.commandName)) === null || _b === void 0 ? void 0 : _b.metadata.execute([interaction], options, {
96
+ type: 'contextMenu'
97
+ });
73
98
  }
74
99
  if (interaction.isCommand() || interaction.isAutocomplete()) {
75
100
  const rootCommand = interaction.commandName;
@@ -79,17 +104,26 @@ let NecordInteractionUpdate = NecordInteractionUpdate_1 = class NecordInteractio
79
104
  if (!command)
80
105
  return;
81
106
  if (interaction.isCommand()) {
82
- return command === null || command === void 0 ? void 0 : command.metadata.execute([interaction], this.transformOptions(command, interaction));
107
+ return command.metadata.execute([interaction], this.transformOptions(command, interaction), { type: 'slashCommands' });
83
108
  }
84
109
  const module = command.metadata.host;
85
110
  const autocompleteMetadata = command.metadata[necord_constants_1.AUTOCOMPLETE_METADATA];
86
111
  const { instance: moduleRef } = module.getProviderByKey(core_1.ModuleRef);
87
112
  if (!module || !autocompleteMetadata || !moduleRef)
88
113
  return;
89
- const autocomplete = yield moduleRef
90
- .resolve(autocompleteMetadata, constants_1.STATIC_CONTEXT, { strict: true })
91
- .catch(() => moduleRef.create(autocompleteMetadata));
92
- const options = yield (autocomplete === null || autocomplete === void 0 ? void 0 : autocomplete.transformOptions(interaction, interaction.options.getFocused(true)));
114
+ const getAutocomplete = () => __awaiter(this, void 0, void 0, function* () {
115
+ const provider = module.getProviderByKey(autocompleteMetadata);
116
+ if (provider) {
117
+ return provider.instance;
118
+ }
119
+ module.addProvider({
120
+ provide: autocompleteMetadata,
121
+ useValue: yield moduleRef.create(autocompleteMetadata)
122
+ });
123
+ return getAutocomplete();
124
+ });
125
+ let autocomplete = yield getAutocomplete();
126
+ const options = yield autocomplete.transformOptions(interaction, interaction.options.getFocused(true));
93
127
  return interaction.respond(options !== null && options !== void 0 ? options : []);
94
128
  }
95
129
  });
@@ -23,7 +23,7 @@ let NecordRegistry = NecordRegistry_1 = class NecordRegistry {
23
23
  }
24
24
  registerListeners(listeners) {
25
25
  listeners.forEach(listener => {
26
- this.client[listener.type](listener.event, (...args) => listener.metadata.execute(args));
26
+ this.client[listener.type](listener.event, (...args) => listener.metadata.execute(args, null, { type: 'event' }));
27
27
  });
28
28
  }
29
29
  addTextCommands(textCommands) {
@@ -38,7 +38,7 @@ let NecordUpdate = class NecordUpdate {
38
38
  onMessageCreate([message]) {
39
39
  var _a, _b;
40
40
  return __awaiter(this, void 0, void 0, function* () {
41
- if (!message || !((_a = message.content) === null || _a === void 0 ? void 0 : _a.length))
41
+ if (!message || !((_a = message.content) === null || _a === void 0 ? void 0 : _a.length) || message.webhookId || message.author.bot)
42
42
  return;
43
43
  const content = message.content.toLowerCase();
44
44
  const prefix = typeof this.prefix !== 'function' ? this.prefix : yield this.prefix(message);
@@ -48,7 +48,8 @@ let NecordUpdate = class NecordUpdate {
48
48
  const cmd = args.shift();
49
49
  if (!cmd)
50
50
  return;
51
- return (_b = this.registry.getTextCommand(cmd)) === null || _b === void 0 ? void 0 : _b.metadata.execute([message], args);
51
+ return (_b = this.registry
52
+ .getTextCommand(cmd)) === null || _b === void 0 ? void 0 : _b.metadata.execute([message], args, { type: 'textCommand' });
52
53
  });
53
54
  }
54
55
  emit(event, ...args) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "necord",
3
3
  "description": "A module for creating Discord bots using NestJS, based on Discord.js.",
4
- "version": "4.1.0",
4
+ "version": "4.2.0",
5
5
  "scripts": {
6
6
  "build": "rimraf -rf dist && tsc -p tsconfig.json",
7
7
  "prepublish:npm": "npm run build",
@@ -31,7 +31,7 @@
31
31
  "bot framework"
32
32
  ],
33
33
  "license": "MIT",
34
- "homepage": "https://github.com/SocketSomeone/necord/wiki",
34
+ "homepage": "https://necord.org",
35
35
  "repository": {
36
36
  "type": "git",
37
37
  "url": "https://github.com/SocketSomeone/necord"
@@ -50,7 +50,7 @@
50
50
  "@favware/npm-deprecate": "1.0.4",
51
51
  "@nestjs/common": "8.2.6",
52
52
  "@nestjs/core": "8.2.6",
53
- "@types/node": "17.0.10",
53
+ "@types/node": "17.0.13",
54
54
  "@typescript-eslint/eslint-plugin": "4.33.0",
55
55
  "@typescript-eslint/parser": "4.33.0",
56
56
  "discord.js": "13.6.0",
@@ -60,10 +60,10 @@
60
60
  "husky": "7.0.4",
61
61
  "prettier": "2.5.1",
62
62
  "reflect-metadata": "0.1.13",
63
- "release-it": "14.12.3",
63
+ "release-it": "14.12.4",
64
64
  "rimraf": "3.0.2",
65
65
  "rxjs": "7.5.2",
66
- "ts-node": "^10.4.0",
66
+ "ts-node": "10.4.0",
67
67
  "typescript": "4.5.5"
68
68
  },
69
69
  "peerDependencies": {