@pikokr/command.ts 3.0.5-dev.3776bc1 → 3.0.6-dev.92a9148

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.
@@ -8,7 +8,7 @@ export declare class BuiltinCommandConverters extends BuiltInModule {
8
8
  message(msg: Message): Message;
9
9
  string(msg: Message, arg: string): string;
10
10
  getUserIDByMention(mention: string): `${bigint}` | undefined;
11
- user(value: string): User | null;
12
- member(value: string, msg: Message): GuildMember | undefined;
13
- number(value: string): number | undefined;
11
+ user(msg: Message, value: string): User | null;
12
+ member(msg: Message, value: string): GuildMember | undefined;
13
+ number(msg: Message, value: string): number | undefined;
14
14
  }
@@ -36,14 +36,14 @@ class BuiltinCommandConverters extends BuiltInModule_1.BuiltInModule {
36
36
  return mention;
37
37
  }
38
38
  }
39
- user(value) {
39
+ user(msg, value) {
40
40
  const id = this.getUserIDByMention(value);
41
41
  if (!id)
42
42
  return null;
43
43
  const user = this.client.users.cache.get(id);
44
44
  return user || null;
45
45
  }
46
- member(value, msg) {
46
+ member(msg, value) {
47
47
  var _a;
48
48
  const id = this.getUserIDByMention(value);
49
49
  if (!id)
@@ -51,7 +51,7 @@ class BuiltinCommandConverters extends BuiltInModule_1.BuiltInModule {
51
51
  const user = (_a = msg.guild) === null || _a === void 0 ? void 0 : _a.members.cache.get(id);
52
52
  return user || undefined;
53
53
  }
54
- number(value) {
54
+ number(msg, value) {
55
55
  const n = Number(value);
56
56
  return isNaN(n) ? undefined : n;
57
57
  }
@@ -71,19 +71,19 @@ __decorate([
71
71
  __decorate([
72
72
  command_1.argumentConverter(discord_js_1.User),
73
73
  __metadata("design:type", Function),
74
- __metadata("design:paramtypes", [String]),
74
+ __metadata("design:paramtypes", [discord_js_1.Message, String]),
75
75
  __metadata("design:returntype", Object)
76
76
  ], BuiltinCommandConverters.prototype, "user", null);
77
77
  __decorate([
78
78
  command_1.argumentConverter(discord_js_1.GuildMember),
79
79
  __metadata("design:type", Function),
80
- __metadata("design:paramtypes", [String, discord_js_1.Message]),
80
+ __metadata("design:paramtypes", [discord_js_1.Message, String]),
81
81
  __metadata("design:returntype", Object)
82
82
  ], BuiltinCommandConverters.prototype, "member", null);
83
83
  __decorate([
84
84
  command_1.argumentConverter(Number),
85
85
  __metadata("design:type", Function),
86
- __metadata("design:paramtypes", [String]),
86
+ __metadata("design:paramtypes", [discord_js_1.Message, String]),
87
87
  __metadata("design:returntype", void 0)
88
88
  ], BuiltinCommandConverters.prototype, "number", null);
89
89
  exports.BuiltinCommandConverters = BuiltinCommandConverters;
@@ -95,8 +95,11 @@ class CommandHandler extends BuiltInModule_1.BuiltInModule {
95
95
  }
96
96
  if (!converter)
97
97
  return error(new error_1.ArgumentConverterNotFound(argType, msg));
98
+ const converterModule = this.registry.modules.find((x) => x.argumentConverters.includes(converter));
99
+ if (!converterModule)
100
+ return error(new error_1.ArgumentConverterNotFound(argType, msg));
98
101
  if (converter.withoutParameter) {
99
- argList.push(yield converter.execute(module, msg));
102
+ argList.push(yield converter.execute(converterModule, msg));
100
103
  continue;
101
104
  }
102
105
  const arg = args.shift();
@@ -106,7 +109,7 @@ class CommandHandler extends BuiltInModule_1.BuiltInModule {
106
109
  if (!arg) {
107
110
  return error(new error_1.ArgumentNotProvided(i, cmd, msg));
108
111
  }
109
- const executed = yield converter.execute(module, msg, arg);
112
+ const executed = yield converter.execute(converterModule, msg, arg);
110
113
  if (!executed === undefined) {
111
114
  return error(new error_1.ArgumentNotProvided(i, cmd, msg));
112
115
  }
@@ -75,9 +75,33 @@ const rest = (target, propertyKey, parameterIndex) => {
75
75
  Reflect.defineMetadata(constants_1.KRest, parameterIndex, target, propertyKey);
76
76
  };
77
77
  exports.rest = rest;
78
- exports.ownerOnly = utils_2.createCheckDecorator((msg) => msg.data.cts.owners.includes(msg.author.id), (i) => i.data.cts.owners.includes(i.user.id));
79
- exports.guildOnly = utils_2.createCheckDecorator((msg) => !!msg.guild, (i) => !!i.guildId);
80
- exports.dmOnly = utils_2.createCheckDecorator((msg) => !msg.guild, (i) => !i.guildId);
78
+ exports.ownerOnly = utils_2.createCheckDecorator((msg) => {
79
+ if (msg.data.cts.owners.includes(msg.author.id))
80
+ return true;
81
+ throw new error_1.OwnerOnlyCommandError();
82
+ }, (i) => {
83
+ if (i.data.cts.owners.includes(i.user.id))
84
+ return true;
85
+ throw new error_1.OwnerOnlyCommandError();
86
+ });
87
+ exports.guildOnly = utils_2.createCheckDecorator((msg) => {
88
+ if (!!msg.guild)
89
+ return true;
90
+ throw new error_1.GuildOnlyCommandError();
91
+ }, (i) => {
92
+ if (!!i.guildId)
93
+ return true;
94
+ throw new error_1.GuildOnlyCommandError();
95
+ });
96
+ exports.dmOnly = utils_2.createCheckDecorator((msg) => {
97
+ if (!msg.guild)
98
+ return true;
99
+ throw new error_1.DMOnlyCommandError();
100
+ }, (i) => {
101
+ if (!i.guildId)
102
+ return true;
103
+ throw new error_1.DMOnlyCommandError();
104
+ });
81
105
  const requireUserPermissions = (permission) => utils_2.createCheckDecorator((msg) => {
82
106
  if (!msg.guild || !msg.member)
83
107
  throw new Error('This command must be used in guild.');
@@ -1,5 +1,6 @@
1
1
  export declare class ModuleLoadError extends Error {
2
- constructor(file: string);
2
+ error: Error;
3
+ constructor(file: string, error: Error);
3
4
  }
4
5
  export declare class InvalidModuleError extends Error {
5
6
  }
@@ -2,8 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InvalidModuleError = exports.ModuleLoadError = void 0;
4
4
  class ModuleLoadError extends Error {
5
- constructor(file) {
5
+ constructor(file, error) {
6
6
  super('Failed to load module ' + file);
7
+ this.error = error;
7
8
  }
8
9
  }
9
10
  exports.ModuleLoadError = ModuleLoadError;
@@ -0,0 +1,3 @@
1
+ export declare class DMOnlyCommandError extends Error {
2
+ constructor();
3
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DMOnlyCommandError = void 0;
4
+ class DMOnlyCommandError extends Error {
5
+ constructor() {
6
+ super();
7
+ }
8
+ }
9
+ exports.DMOnlyCommandError = DMOnlyCommandError;
@@ -0,0 +1,3 @@
1
+ export declare class GuildOnlyCommandError extends Error {
2
+ constructor();
3
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GuildOnlyCommandError = void 0;
4
+ class GuildOnlyCommandError extends Error {
5
+ constructor() {
6
+ super();
7
+ }
8
+ }
9
+ exports.GuildOnlyCommandError = GuildOnlyCommandError;
@@ -0,0 +1,3 @@
1
+ export declare class OwnerOnlyCommandError extends Error {
2
+ constructor();
3
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OwnerOnlyCommandError = void 0;
4
+ class OwnerOnlyCommandError extends Error {
5
+ constructor() {
6
+ super();
7
+ }
8
+ }
9
+ exports.OwnerOnlyCommandError = OwnerOnlyCommandError;
@@ -0,0 +1,3 @@
1
+ export * from './OwnerOnlyCommand';
2
+ export * from './GuildOnlyCommand';
3
+ export * from './DMOnlyCommand';
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./OwnerOnlyCommand"), exports);
14
+ __exportStar(require("./GuildOnlyCommand"), exports);
15
+ __exportStar(require("./DMOnlyCommand"), exports);
@@ -4,3 +4,4 @@ export * from './ArgumentNotProvided';
4
4
  export * from './ArgumentConverterNotFound';
5
5
  export * from './CommandCheckFailed';
6
6
  export * from './PermissionRequired';
7
+ export * from './checks';
@@ -16,3 +16,4 @@ __exportStar(require("./ArgumentNotProvided"), exports);
16
16
  __exportStar(require("./ArgumentConverterNotFound"), exports);
17
17
  __exportStar(require("./CommandCheckFailed"), exports);
18
18
  __exportStar(require("./PermissionRequired"), exports);
19
+ __exportStar(require("./checks"), exports);
@@ -102,7 +102,7 @@ class Registry {
102
102
  m = require(p);
103
103
  }
104
104
  catch (e) {
105
- throw new error_1.ModuleLoadError(p);
105
+ throw new error_1.ModuleLoadError(p, e);
106
106
  }
107
107
  if (m.loaded)
108
108
  throw new Error('MODULE_ALREADY_LOADED');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pikokr/command.ts",
3
3
  "description": "Discord.js command framework for typescript.",
4
- "version": "3.0.5-dev.3776bc1",
4
+ "version": "3.0.6-dev.92a9148",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "license": "MIT",
@@ -33,7 +33,7 @@ export class BuiltinCommandConverters extends BuiltInModule {
33
33
  }
34
34
 
35
35
  @argumentConverter(User)
36
- user(value: string): User | null {
36
+ user(msg: Message, value: string): User | null {
37
37
  const id = this.getUserIDByMention(value)
38
38
  if (!id) return null
39
39
  const user = this.client.users.cache.get(id)
@@ -41,7 +41,7 @@ export class BuiltinCommandConverters extends BuiltInModule {
41
41
  }
42
42
 
43
43
  @argumentConverter(GuildMember)
44
- member(value: string, msg: Message): GuildMember | undefined {
44
+ member(msg: Message, value: string): GuildMember | undefined {
45
45
  const id = this.getUserIDByMention(value)
46
46
  if (!id) return
47
47
  const user = msg.guild?.members.cache.get(id)
@@ -49,7 +49,7 @@ export class BuiltinCommandConverters extends BuiltInModule {
49
49
  }
50
50
 
51
51
  @argumentConverter(Number)
52
- number(value: string) {
52
+ number(msg: Message, value: string) {
53
53
  const n = Number(value)
54
54
  return isNaN(n) ? undefined : n
55
55
  }
@@ -90,8 +90,12 @@ export class CommandHandler extends BuiltInModule {
90
90
 
91
91
  if (!converter) return error(new ArgumentConverterNotFound(argType, msg))
92
92
 
93
+ const converterModule = this.registry.modules.find((x) => x.argumentConverters.includes(converter))
94
+
95
+ if (!converterModule) return error(new ArgumentConverterNotFound(argType, msg))
96
+
93
97
  if (converter.withoutParameter) {
94
- argList.push(await converter.execute(module, msg))
98
+ argList.push(await converter.execute(converterModule, msg))
95
99
  continue
96
100
  }
97
101
  const arg = args.shift()
@@ -101,7 +105,7 @@ export class CommandHandler extends BuiltInModule {
101
105
  if (!arg) {
102
106
  return error(new ArgumentNotProvided(i, cmd, msg))
103
107
  }
104
- const executed = await converter.execute(module, msg, arg)
108
+ const executed = await converter.execute(converterModule, msg, arg)
105
109
  if (!executed === undefined) {
106
110
  return error(new ArgumentNotProvided(i, cmd, msg))
107
111
  }
@@ -1,4 +1,4 @@
1
- import { Collection, Guild, GuildChannel, GuildMember, Message, Role, TextChannel, ThreadChannel, User } from 'discord.js'
1
+ import { Collection } from 'discord.js'
2
2
 
3
3
  export interface CoolDownAdapter {
4
4
  get(id: string): Promise<number | undefined>
@@ -5,7 +5,7 @@ import { ArgumentConverter, SlashArgumentConverter } from './ArgumentConverter'
5
5
  import { Module } from '../structures'
6
6
  import { createCheckDecorator } from './utils'
7
7
  import { GuildMember, Message, PermissionResolvable, Permissions, TextChannel } from 'discord.js'
8
- import { ClientPermissionRequired, UserPermissionRequired } from '../error'
8
+ import { ClientPermissionRequired, DMOnlyCommandError, GuildOnlyCommandError, OwnerOnlyCommandError, UserPermissionRequired } from '../error'
9
9
 
10
10
  type CommandOptions = {
11
11
  name: string
@@ -111,18 +111,36 @@ export const rest: ParameterDecorator = (target, propertyKey, parameterIndex) =>
111
111
  }
112
112
 
113
113
  export const ownerOnly = createCheckDecorator(
114
- (msg) => msg.data.cts.owners.includes(msg.author.id),
115
- (i) => i.data.cts.owners.includes(i.user.id),
114
+ (msg) => {
115
+ if (msg.data.cts.owners.includes(msg.author.id)) return true
116
+ throw new OwnerOnlyCommandError()
117
+ },
118
+ (i) => {
119
+ if (i.data.cts.owners.includes(i.user.id)) return true
120
+ throw new OwnerOnlyCommandError()
121
+ },
116
122
  )
117
123
 
118
124
  export const guildOnly = createCheckDecorator(
119
- (msg) => !!msg.guild,
120
- (i) => !!i.guildId,
125
+ (msg) => {
126
+ if (!!msg.guild) return true
127
+ throw new GuildOnlyCommandError()
128
+ },
129
+ (i) => {
130
+ if (!!i.guildId) return true
131
+ throw new GuildOnlyCommandError()
132
+ },
121
133
  )
122
134
 
123
135
  export const dmOnly = createCheckDecorator(
124
- (msg) => !msg.guild,
125
- (i) => !i.guildId,
136
+ (msg) => {
137
+ if (!msg.guild) return true
138
+ throw new DMOnlyCommandError()
139
+ },
140
+ (i) => {
141
+ if (!i.guildId) return true
142
+ throw new DMOnlyCommandError()
143
+ },
126
144
  )
127
145
 
128
146
  export const requireUserPermissions = (permission: PermissionResolvable) =>
@@ -1,5 +1,5 @@
1
1
  export class ModuleLoadError extends Error {
2
- constructor(file: string) {
2
+ constructor(file: string, public error: Error) {
3
3
  super('Failed to load module ' + file)
4
4
  }
5
5
  }
@@ -0,0 +1,5 @@
1
+ export class DMOnlyCommandError extends Error {
2
+ constructor() {
3
+ super()
4
+ }
5
+ }
@@ -0,0 +1,5 @@
1
+ export class GuildOnlyCommandError extends Error {
2
+ constructor() {
3
+ super()
4
+ }
5
+ }
@@ -0,0 +1,5 @@
1
+ export class OwnerOnlyCommandError extends Error {
2
+ constructor() {
3
+ super()
4
+ }
5
+ }
@@ -0,0 +1,3 @@
1
+ export * from './OwnerOnlyCommand'
2
+ export * from './GuildOnlyCommand'
3
+ export * from './DMOnlyCommand'
@@ -4,3 +4,4 @@ export * from './ArgumentNotProvided'
4
4
  export * from './ArgumentConverterNotFound'
5
5
  export * from './CommandCheckFailed'
6
6
  export * from './PermissionRequired'
7
+ export * from './checks'
@@ -95,7 +95,7 @@ export class Registry {
95
95
  try {
96
96
  m = require(p)
97
97
  } catch (e: any) {
98
- throw new ModuleLoadError(p)
98
+ throw new ModuleLoadError(p, e)
99
99
  }
100
100
 
101
101
  if (m.loaded) throw new Error('MODULE_ALREADY_LOADED')