Skip to content
This repository was archived by the owner on May 8, 2023. It is now read-only.

Commit 9679565

Browse files
committed
bug fixes, prefix updates, added enable state to modules, updated tests
The ability to use multiple prefix's as been removed as its unneeded. As for modules, every module type has an enabled state that can be set, thus no longer requiring to make the file end with .disabled or something similar. Various bug fixes, typos, etc.
1 parent 6b6150b commit 9679565

File tree

12 files changed

+84
-23
lines changed

12 files changed

+84
-23
lines changed

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ declare module 'discord-akairo' {
481481

482482
export interface AkairoModuleOptions {
483483
category?: string;
484+
enabled?: boolean;
484485
}
485486

486487
export interface AkairoOptions {

src/struct/AkairoHandler.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class AkairoHandler extends EventEmitter {
9999
}
100100

101101
/**
102-
* Deregisters a module.
102+
* De-registers a module.
103103
* @param {AkairoModule} mod - Module to use.
104104
* @returns {void}
105105
*/
@@ -136,9 +136,15 @@ class AkairoHandler extends EventEmitter {
136136

137137
if (this.modules.has(mod.id)) throw new AkairoError('ALREADY_LOADED', this.classToHandle.name, mod.id);
138138

139-
this.register(mod, isClass ? null : thing);
140-
this.emit(AkairoHandlerEvents.LOAD, mod, isReload);
141-
return mod;
139+
140+
if (mod.enabled) {
141+
this.register(mod, isClass ? null : thing);
142+
this.emit(AkairoHandlerEvents.LOAD, mod, isReload);
143+
return mod;
144+
} else {
145+
this.emit(AkairoHandlerEvents.DISABLED, mod, isReload);
146+
return null;
147+
}
142148
}
143149

144150
/**

src/struct/AkairoModule.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
* @param {AkairoModuleOptions} [options={}] - Options.
55
*/
66
class AkairoModule {
7-
constructor(id, { category = 'default' } = {}) {
7+
constructor(id, { category = 'default', enabled = true } = {}) {
88
/**
99
* ID of the module.
1010
* @type {string}
1111
*/
1212
this.id = id;
1313

14+
/**
15+
* Determines if module is enabled.
16+
* @type {boolean}
17+
*/
18+
this.enabled = enabled;
19+
1420
/**
1521
* ID of the category this belongs to.
1622
* @type {string}

src/struct/ClientUtil.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ class ClientUtil {
360360
}
361361

362362
/**
363-
* Combination of `<Client>.fetchUser()` and `<Guild>.fetchMember()`.
363+
* Combination of `<Client>.users.fetch()` and `<Guild>.members.fetch()`.
364364
* @param {Guild} guild - Guild to fetch in.
365365
* @param {string} id - ID of the user.
366366
* @param {boolean} cache - Whether or not to add to cache.

src/struct/commands/Command.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const ContentParser = require('./ContentParser');
1212
*/
1313
class Command extends AkairoModule {
1414
constructor(id, options = {}) {
15-
super(id, { category: options.category });
15+
super(id, { category: options.category, enabled: options.enabled });
1616

1717
const {
1818
aliases = [],

src/struct/commands/CommandHandler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class CommandHandler extends AkairoHandler {
5656
this.resolver = new TypeResolver(this);
5757

5858
/**
59-
* Collecion of command aliases.
59+
* Collection of command aliases.
6060
* @type {Collection<string, string>}
6161
*/
6262
this.aliases = new Collection();
@@ -191,7 +191,7 @@ class CommandHandler extends AkairoHandler {
191191
* The prefix(es) for command parsing.
192192
* @type {string|string[]|PrefixSupplier}
193193
*/
194-
this.prefix = typeof prefix === 'function' ? prefix.bind(this) : prefix;
194+
this.prefix = typeof prefix === 'function' ? prefix.bind(this)() : prefix;
195195

196196
/**
197197
* Whether or not mentions are allowed for prefixing.
@@ -294,7 +294,7 @@ class CommandHandler extends AkairoHandler {
294294
}
295295

296296
/**
297-
* Deregisters a module.
297+
* De-registers a module.
298298
* @param {Command} command - Module to use.
299299
* @returns {void}
300300
*/

src/struct/inhibitors/Inhibitor.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ class Inhibitor extends AkairoModule {
1212
category,
1313
reason = '',
1414
type = 'post',
15-
priority = 0
15+
priority = 0,
16+
enabled
1617
} = {}) {
17-
super(id, { category });
18+
super(id, { category, enabled });
1819

1920
/**
2021
* Reason emitted when command is inhibited.

src/struct/listeners/Listener.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ class Listener extends AkairoModule {
1212
category,
1313
emitter,
1414
event,
15-
type = 'on'
15+
type = 'on',
16+
enabled
1617
} = {}) {
17-
super(id, { category });
18+
super(id, { category, enabled });
1819

1920
/**
2021
* The event emitter.

src/util/Constants.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ module.exports = {
6161
},
6262
AkairoHandlerEvents: {
6363
LOAD: 'load',
64-
REMOVE: 'remove'
64+
REMOVE: 'remove',
65+
DISABLED: 'disabled'
6566
},
6667
CommandHandlerEvents: {
6768
MESSAGE_BLOCKED: 'messageBlocked',
@@ -82,5 +83,7 @@ module.exports = {
8283
OWNER: 'owner',
8384
GUILD: 'guild',
8485
DM: 'dm'
86+
value: 1
87+
}
8588
}
8689
};

test/commands/enabled.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* eslint-disable no-console */
2+
3+
const { Command } = require('../..');
4+
const sleep = require('util').promisify(setTimeout);
5+
6+
class LockCommand extends Command {
7+
constructor() {
8+
super('enabled', {
9+
enabled: false
10+
});
11+
}
12+
13+
exec(message) {
14+
return [0, 1, 2, 3, 4].reduce((promise, num) => promise.then(() => sleep(1000)).then(() => message.util.send(num)), Promise.resolve());
15+
}
16+
}
17+
18+
module.exports = LockCommand;

test/listeners/disabled.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* eslint-disable no-console */
2+
3+
const { Listener } = require('../..');
4+
5+
class DisabledCommandListener extends Listener {
6+
constructor() {
7+
super('command-load', {
8+
emitter: 'commandHandler',
9+
event: 'disabled',
10+
category: 'commandHandler'
11+
});
12+
}
13+
14+
exec(command) {
15+
console.log(`Command disabled: ${command.id}`);
16+
}
17+
}
18+
19+
module.exports = DisabledCommandListener;

test/struct/TestClient.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { AkairoClient, CommandHandler, InhibitorHandler, ListenerHandler, SQLiteProvider } = require('../../src/index');
22
const sqlite = require('sqlite');
3+
const sqlite3 = require('sqlite3');
34

45
class TestClient extends AkairoClient {
56
constructor() {
@@ -11,7 +12,7 @@ class TestClient extends AkairoClient {
1112
directory: './test/commands/',
1213
ignoreCooldownID: ['132266422679240704'],
1314
aliasReplacement: /-/g,
14-
prefix: '!!',
15+
prefix: '!',
1516
allowMention: true,
1617
commandUtil: true,
1718
commandUtilLifetime: 10000,
@@ -33,17 +34,22 @@ class TestClient extends AkairoClient {
3334
}
3435
});
3536

36-
this.inhibitorHandler = new InhibitorHandler(this, {
37-
directory: './test/inhibitors/'
38-
});
39-
4037
this.listenerHandler = new ListenerHandler(this, {
4138
directory: './test/listeners/'
4239
});
4340

44-
const db = sqlite.open('./test/db.sqlite')
41+
this.inhibitorHandler = new InhibitorHandler(this, {
42+
directory: './test/inhibitors/'
43+
});
44+
45+
const db = sqlite.open({
46+
filename: './test/db.sqlite',
47+
driver: sqlite3.Database
48+
})
4549
.then(d => d.run('CREATE TABLE IF NOT EXISTS guilds (id TEXT NOT NULL UNIQUE, settings TEXT)').then(() => d));
46-
this.settings = new SQLiteProvider(db, 'guilds', { dataColumn: 'settings' });
50+
this.settings = new SQLiteProvider(db, 'guilds', {
51+
dataColumn: 'settings'
52+
});
4753

4854
this.setup();
4955
}
@@ -58,9 +64,9 @@ class TestClient extends AkairoClient {
5864
listenerHandler: this.listenerHandler
5965
});
6066

67+
this.listenerHandler.loadAll();
6168
this.commandHandler.loadAll();
6269
this.inhibitorHandler.loadAll();
63-
this.listenerHandler.loadAll();
6470

6571
const resolver = this.commandHandler.resolver;
6672
resolver.addType('1-10', (message, phrase) => {

0 commit comments

Comments
 (0)