Skip to content

Commit

Permalink
feat: Rename SlashCommand into ApplicationCommand and support other t…
Browse files Browse the repository at this point in the history
…ypes of ApplicationCommand

Also add log for command and applicationCommand ran
  • Loading branch information
YoannMa committed Nov 12, 2021
1 parent 17f79e9 commit ee4f74f
Show file tree
Hide file tree
Showing 41 changed files with 310 additions and 201 deletions.
4 changes: 2 additions & 2 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const store = new Confidence.Store({
guilds : { $env : 'EBOT_CACHE_WARMUP_GUILDS', $coerce : 'array', $default : [] },
users : { $env : 'EBOT_CACHE_WARMUP_USERS', $coerce : 'array', $default : [] }
},
slashCommands : {
register : { $env : 'EBOT_SLASH_COMMAND_REGISTER', $coerce : 'boolean', $default : true }
applicationCommands : {
register : { $env : 'EBOT_APPLICATION_COMMAND_REGISTER', $coerce : 'boolean', $default : true }
}
},
discord : {
Expand Down
48 changes: 24 additions & 24 deletions src/core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const { AkairoClient, InhibitorHandler } = require('discord-akairo');

const { CoreEvents } = require('./constants');

const CommandHandler = require('./struct/command/commandHandler');
const SlashCommandHandler = require('./struct/slashCommand/slashCommandHandler');
const ListenerHandler = require('./struct/listener/listenerHandler');
const Module = require('./struct/module');
const CommandHandler = require('./struct/command/commandHandler');
const ApplicationCommandHandler = require('./struct/applicationCommand/applicationCommandHandler');
const ListenerHandler = require('./struct/listener/listenerHandler');
const Module = require('./struct/module');

const ClientUtil = require('./clientUtil');

Expand All @@ -35,7 +35,7 @@ module.exports = class EbotClient extends AkairoClient {
#coreListenerHandlers = new Map();

#commandHandler;
#slashCommandHandler;
#applicationCommandHandler;
#inhibitorHandler;
#listenerHandler;

Expand Down Expand Up @@ -86,11 +86,6 @@ module.exports = class EbotClient extends AkairoClient {
}

this.#coreListenerHandlers.get('process').setEmitters({ process });

for (const module of this.#coreListenerHandlers.values()) {

module.loadAll();
}
}

registerCommandHandler(settings) {
Expand All @@ -116,14 +111,18 @@ module.exports = class EbotClient extends AkairoClient {

}, settings));

this.#coreListenerHandlers.get('command').setEmitters({ handler : this.#commandHandler });

this.logger.trace({ event : CoreEvents.COMMAND_HANDLER_REGISTERED, emitter : 'core' });
}

registerSlashCommandHandler(settings) {
registerApplicationCommandHandler(settings) {

this.#applicationCommandHandler = new ApplicationCommandHandler(this, Hoek.merge({}, settings));

this.#slashCommandHandler = new SlashCommandHandler(this, Hoek.merge({}, settings));
this.#coreListenerHandlers.get('applicationCommand').setEmitters({ handler : this.#applicationCommandHandler });

this.logger.trace({ event : CoreEvents.SLASH_COMMAND_HANDLER_REGISTERED, emitter : 'core' });
this.logger.trace({ event : CoreEvents.APPLICATION_COMMAND_HANDLER_REGISTERED, emitter : 'core' });
}

registerListenerHandler(settings) {
Expand Down Expand Up @@ -165,7 +164,7 @@ module.exports = class EbotClient extends AkairoClient {
this.logger.debug({
event : CoreEvents.MODULE_LOADED,
emitter : 'core',
msg : `Module ${ name } loaded from ${ path }`
msg : `Module ${ name } loaded from ${ path }`
});
}
}
Expand All @@ -175,10 +174,15 @@ module.exports = class EbotClient extends AkairoClient {
await this.#setupCoreListenerHandlers();

this.registerCommandHandler();
this.registerSlashCommandHandler();
this.registerApplicationCommandHandler();
this.registerListenerHandler();
this.registerInhibitorHandler();

for (const module of this.#coreListenerHandlers.values()) {

module.loadAll();
}

this.#initialized = true;

this.logger.debug({ event : 'initialized', emitter : 'core' });
Expand Down Expand Up @@ -226,23 +230,19 @@ module.exports = class EbotClient extends AkairoClient {
this.#commandHandler.useListenerHandler(this.#listenerHandler);
}

this.#coreListenerHandlers.set('command', new ListenerHandler(this, { directory : Path.join(__dirname, './listeners/command/') }));

this.#coreListenerHandlers.get('command').setEmitters({ commandHandler : this.#commandHandler });

this.logger.trace({ event : CoreEvents.LISTENER_HANDLER_LOADED, emitter : 'core' });
}

if (this.#slashCommandHandler) {
if (this.#applicationCommandHandler) {

this.logger.trace({ event : CoreEvents.SLASH_COMMAND_HANDLER_LOADED, emitter : 'core' });
this.logger.trace({ event : CoreEvents.APPLICATION_COMMAND_HANDLER_LOADED, emitter : 'core' });
}

await this.login(this.#settings.discord.token);

await this.warmupCache();

await this.#slashCommandHandler.registerCommands();
await this.#applicationCommandHandler.registerCommands();

this.#started = true;

Expand Down Expand Up @@ -296,9 +296,9 @@ module.exports = class EbotClient extends AkairoClient {
return this.#commandHandler;
}

get slashCommandHandler() {
get applicationCommandHandler() {

return this.#slashCommandHandler;
return this.#applicationCommandHandler;
}

get listenerHandler() {
Expand Down
24 changes: 12 additions & 12 deletions src/core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ exports.CoreEvents = {
SERVICE_REGISTERED : 'serviceRegistered',
SERVICE_INITIALIZED : 'serviceInitialized',

COMMAND_HANDLER_REGISTERED : 'commandHandlerRegistered',
SLASH_COMMAND_HANDLER_REGISTERED : 'slashCommandHandlerRegistered',
INHIBITOR_HANDLER_REGISTERED : 'inhibitorHandlerRegistered',
LISTENER_HANDLER_REGISTERED : 'listenerHandlerRegistered',
COMMAND_HANDLER_REGISTERED : 'commandHandlerRegistered',
APPLICATION_COMMAND_HANDLER_REGISTERED : 'applicationCommandHandlerRegistered',
INHIBITOR_HANDLER_REGISTERED : 'inhibitorHandlerRegistered',
LISTENER_HANDLER_REGISTERED : 'listenerHandlerRegistered',

COMMAND_HANDLER_LOADED : 'commandHandlerLoaded',
SLASH_COMMAND_HANDLER_LOADED : 'slashCommandHandlerLoaded',
INHIBITOR_HANDLER_LOADED : 'inhibitorHandlerLoaded',
LISTENER_HANDLER_LOADED : 'listenerHandlerLoaded',
COMMAND_HANDLER_LOADED : 'commandHandlerLoaded',
APPLICATION_COMMAND_HANDLER_LOADED : 'applicationCommandHandlerLoaded',
INHIBITOR_HANDLER_LOADED : 'inhibitorHandlerLoaded',
LISTENER_HANDLER_LOADED : 'listenerHandlerLoaded',

MODULE_ERROR : 'moduleError',
MODULE_LOADED : 'moduleLoaded',

CACHE_WARMUP_STARTED : 'cacheWarmupStarted',
CACHE_WARMUP_FINISHED : 'cacheWarmupFinished',

GUILD_SLASH_COMMAND_BUILT : 'guildSlashCommandBuilt',
GUILD_SLASH_COMMANDS_REGISTERED : 'guildSlashCommandRegistered',
GUILD_APPLICATION_COMMAND_BUILT : 'guildApplicationCommandBuilt',
GUILD_APPLICATION_COMMANDS_REGISTERED : 'guildApplicationCommandRegistered',

GLOBAL_SLASH_COMMAND_BUILT : 'globalSlashCommandBuilt',
GLOBAL_SLASH_COMMANDS_REGISTERED : 'globalSlashCommandRegistered'
GLOBAL_APPLICATION_COMMAND_BUILT : 'globalApplicationCommandBuilt',
GLOBAL_APPLICATION_COMMANDS_REGISTERED : 'globalApplicationCommandRegistered'
};
14 changes: 7 additions & 7 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ module.exports.Client = require('./client');
module.exports.Module = require('./struct/module');
module.exports.Service = require('./struct/service');

module.exports.CommandHandler = require('./struct/command/commandHandler');
module.exports.ListenerHandler = require('./struct/listener/listenerHandler');
module.exports.SlashCommandHandler = require('./struct/slashCommand/slashCommandHandler');
module.exports.CommandHandler = require('./struct/command/commandHandler');
module.exports.ListenerHandler = require('./struct/listener/listenerHandler');
module.exports.ApplicationCommandHandler = require('./struct/applicationCommand/applicationCommandHandler');

module.exports.Command = require('./struct/command/command');
module.exports.Listener = require('./struct/listener/listener');
module.exports.SlashCommand = require('./struct/slashCommand/slashCommand');
module.exports.Command = require('./struct/command/command');
module.exports.Listener = require('./struct/listener/listener');
module.exports.ApplicationCommand = require('./struct/applicationCommand/applicationCommand');

module.exports.Util = require('./util');

module.exports.Constants = require('./constants');

module.exports.KeyvProvider = require('./struct/providers/keyv');
module.exports.KeyvProvider = require('./struct/providers/keyv');
module.exports.ObjectionProvider = require('./struct/providers/objection');
34 changes: 34 additions & 0 deletions src/core/listeners/applicationCommand/commandFinished.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const { Listener, ApplicationCommandHandler } = require('../../');

module.exports = class CommandFinishedListener extends Listener {

constructor() {

super(ApplicationCommandHandler.Events.COMMAND_FINISHED, {
category : 'core',
emitter : 'handler',
event : ApplicationCommandHandler.Events.COMMAND_FINISHED
});
}

exec(interaction, command) {

this.client.logger.info({
event : this.event,
emitter : this.emitter,
command : {
id : command.id,
category : command.categoryID
},
interaction : {
id : interaction.id,
channelName : interaction?.channel?.name,
authorName : interaction?.user?.username,
guildName : interaction?.member?.guild?.name,
content : interaction.toString()
}
});
}
};
2 changes: 1 addition & 1 deletion src/core/listeners/client/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class clientDebugListener extends Listener {
module.exports = class ClientDebugListener extends Listener {

constructor() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/listeners/client/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class clientErrorListener extends Listener {
module.exports = class ClientErrorListener extends Listener {

constructor() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/listeners/client/rateLimit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class clientRateLimitListener extends Listener {
module.exports = class ClientRateLimitListener extends Listener {

constructor() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/listeners/client/ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class clientReadyListener extends Listener {
module.exports = class ClientReadyListener extends Listener {

constructor() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/listeners/client/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class clientWarningListener extends Listener {
module.exports = class ClientWarningListener extends Listener {

constructor() {

Expand Down
35 changes: 35 additions & 0 deletions src/core/listeners/command/commandFinished.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const { Listener, CommandHandler } = require('../../');

module.exports = class CommandFinishedListener extends Listener {

constructor() {

super(CommandHandler.Events.COMMAND_FINISHED, {
category : 'core',
emitter : 'handler',
event : CommandHandler.Events.COMMAND_FINISHED
});
}

exec(message, command, params, reply) {

this.client.logger.info({
event : this.event,
emitter : this.emitter,
params,
command : {
id : command.id,
category : command.categoryID
},
message : {
id : message.id,
channelName : message?.channel?.name,
authorName : message?.author?.username,
guildName : message?.guild?.name,
content : message?.content
}
});
}
};
2 changes: 1 addition & 1 deletion src/core/listeners/guild/guildUnvailable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class guildUnavailableListener extends Listener {
module.exports = class GuildUnavailableListener extends Listener {

constructor() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/listeners/guild/memberAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class memberAddListener extends Listener {
module.exports = class MemberAddListener extends Listener {

constructor() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/listeners/message/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class messageDeleteListener extends Listener {
module.exports = class MessageDeleteListener extends Listener {

constructor() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/listeners/message/deleteBulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class messageDeleteBulkListener extends Listener {
module.exports = class MessageDeleteBulkListener extends Listener {

constructor() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/listeners/message/reactionAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class messageReactionAddListener extends Listener {
module.exports = class MessageReactionAddListener extends Listener {

constructor() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/listeners/message/reactionRemove.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class messageReactionRemoveListener extends Listener {
module.exports = class MessageReactionRemoveListener extends Listener {

constructor() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/listeners/message/sent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class messageSentListener extends Listener {
module.exports = class SessageSentListener extends Listener {

constructor() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/listeners/message/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Constants } = require('discord.js');
const { Listener } = require('../../');

module.exports = class messageUpdateListener extends Listener {
module.exports = class MessageUpdateListener extends Listener {

constructor() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/listeners/process/uncaughtException.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { Listener } = require('../../');

module.exports = class processUncaughtExceptionListener extends Listener {
module.exports = class ProcessUncaughtExceptionListener extends Listener {

constructor() {

Expand Down
Loading

0 comments on commit ee4f74f

Please sign in to comment.