Skip to content

Commit

Permalink
Added EventHandler ?
Browse files Browse the repository at this point in the history
  • Loading branch information
Mametaro-discord committed Jan 22, 2022
1 parent e89ebc9 commit 06133ba
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 27 deletions.
File renamed without changes.
37 changes: 37 additions & 0 deletions src/actions/InteractionCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const GenericAction = require('./Action');
const ApplicationCommandInteraction = require('../structures/ApplicationCommandInteraction');
const AutocompleteInteraction = require('../structures/AutocompleteInteraction');
const ContextMenuInteraction = require('../structures/ContextMenuCommandInteraction');
const {
ApplicationCommandTypes,
InteractionTypes
} = require('../interfaces/consts');

module.exports = class InteractionCreateAction extends Action {
handle(data) {
this.getChannel(data);

const list = [
null,
null,
[
null,
ApplicationCommandInteraction,
ContextMenuInteraction,
ContextMenuInteraction,
],
[],
undefined
];

if (data.type === InteractionTypes.APPLICATION_COMMAND) {
const Interaction = list[data.type][data.data.type];
this.client.emit('commandInteraction', new Interaction(client, data));
} else if (data.type === InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE) {
const Interaction = list[data.type]
this.client.emit('autocompleteInteraction')
};
};
};
42 changes: 39 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
'use strict';

const TypeError = require('./util/errors').makeError(TypeError);
const ApplicationCommandManager = require('./managers/ApplicationCommandManager');
const GuildApplicationCommandManager = require('./managers/GuildApplicationCommandManager');
const Util = require('./util/index');
const ErrorUtil = require('./util/errors');
const typeerror = ErrorUtil.makeError(TypeError); const TypeError = typeerror;
const WeakMap = require('./structures/extend/ExtendedWeakMap');
const InteractionCreateAction = require('./actions/InteractionCreate');
const {
Client,
MessageFlags,
Structures,
version
} = require('discord.js');


function main(client) {
if (String(version).split('.').shift() !== '12') throw new Error('The version of discord.js must be 12x');
if (!(client instanceof Client)) throw new TypeError('invalid argument', 'client', 'Client');

client.commands = new ApplicationCommandManager(client);
client.actions.register(InteractionCreateAction);

client.ws.on('INTERACTION_CREATE', client.actions.InteractionCreate.handle);
extend();

return module.exports;
};

function extend() {
Expand All @@ -36,9 +46,35 @@ function extend() {
);

const hasManager = new WeakMap();
Object.defineProperty(Guild.prototype, {
Object.defineProperty(Guild.prototype, 'commands', {
get() {
return hasManager.get(this) || hasManager.set(this, new GuildApplicationCommandManager(this));
}
});
};
};

module.exports = Object.assign(
main,
{
consts: require('./interfaces/consts')
},
{ ErrorUtil, Util },
{
ApplicationCommandManager,
ApplicationCommandPermissionsManager: require('./managers/ApplicationCommandPermissionsManager'),
GuildApplicationCommandManager
},
Util.entries2Object(
[
'ApplicationCommand',
'ApplicationCommandInteraction',
'AutocompleteInteraction',
'Base',
'BaseInteraction',
'ContextMenuInteraction',
'InteractionAuthor',
'InteractionReply'
]
.map(name => [name, require(`./structures/${name}`)])
)
);
18 changes: 5 additions & 13 deletions src/util/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
const errors = require('./errors');

exports.makeError = function(BaseError) {
class SlashError extends BaseError {
constructor(fn, ...args) {
super(errors[fn](...args));
};
};
SlashError.name = BaseError.name;
return SlashError;
};

exports.TypeError = exports.makeError(TypeError);
exports.entries2Object = function(entries) {
let result;
entries.forEach([key, value] => result[key] = value);
return result;
};
15 changes: 4 additions & 11 deletions test/i.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
class ExtendedWeakMap extends WeakMap {
set(...args) {
super.set(...args);
return args[1];
};
const obj = {
key: 'value'
};

const wm = new ExtendedWeakMap();
const { key: k } = obj;

function main() {
return wm.get(this) || wm.set(this, 'value');
};

console.log(main());
console.log(k)

0 comments on commit 06133ba

Please sign in to comment.