Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Commit

Permalink
change modules to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sr229 committed Nov 14, 2017
1 parent 85c6669 commit 230af5e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/cmd/dev/commands/command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* @file Dynamic command loading, unloading and reloading.
* @author Ovyerus
*/

const fs = require('fs');

exports.loadAsSubcommands = true;

exports.commands = [
'enable',
'disable',
'reload'
];

exports.main = {
desc: 'Command for managing command modules.',
longDesc: 'Manages command modules for the bots. If no arguments, lists currently loaded modules, else runs the specified subcommand if possible.',
usage: '[enable|disable|reload]',
owner: true,
async main(bot, ctx) {
let unloadedMods = JSON.parse(fs.readFileSync(`${mainDir}/data/unloadedCommands.json`));
let embed = {
title: 'Currently enabled command modules',
description: `Showing **${bot.commandFolders.length}** command modules.`,
fields: [
{name: 'Loaded Modules', value: []},
{name: 'Unloaded Modules', value: []}
]
};

Object.keys(bot.commands.modules).filter(m => !m.endsWith('-fixed')).forEach(mod => {
embed.fields[0].value.push(`\`${mod}\``);
});

unloadedMods.forEach(mod => {
embed.fields[1].value.push(`\`${mod.split('/').slice(-1)[0]}\``);
});

embed.fields[0].value = embed.fields[0].value.join(', ');
embed.fields[1].value = embed.fields[1].value.join(', ') || 'None';

await ctx.createMessage({embed});
}
};

exports.enable = {
desc: 'enable a command.',
usage: '<module>',
async main(bot, ctx) {
if (!ctx.args[0]) return await ctx.createMessage('No module given to load.');
if (bot.commands.checkModule(ctx.args[0])) return await ctx.createMessage(`Module **${ctx.args[0]}** is already loaded.`);

let folders = bot.commandFolders.map(f => f.split('/').slice(-1)[0]);

if (!folders.includes(ctx.args[0])) return await ctx.channel.createMessage(`Module **${ctx.args[0]}** does not exist.`);

let pkg = JSON.parse(fs.readFileSync(bot.commandFolders[folders.indexOf(ctx.args[0])] + '/package.json'));
let mod = `${bot.commandFolders[folders.indexOf(ctx.args[0])]}/${pkg.main}`;

bot.commands.loadModule(mod);

let unloadedMods = JSON.parse(fs.readFileSync(`${mainDir}/data/unloadedCommands.json`));
let sliced = unloadedMods.map(f => f.split('/').slice(-1)[0]);

if (!sliced.includes(ctx.args[0])) {
unloadedMods.splice(sliced.indexOf(ctx.args[0]), 1);
fs.writeFileSync(`${mainDir}/data/unloadedCommands.json`, JSON.stringify(unloadedMods));
}

await ctx.createMessage(`Loaded module **${ctx.args[0]}**`);
}
};

exports.disable = {
desc: 'Disable a command.',
usage: '<module>',
async main(bot, ctx) {
if (!ctx.args[0]) return await ctx.createMessage('No module given to unload.');
if (!bot.commands.checkModule(ctx.args[0])) return await ctx.createMessage(`Module **${ctx.args[0]}** is not loaded or does not exist.`);

let folders = bot.commandFolders.map(f => f.split('/').slice(-1)[0]);
let pkg = JSON.parse(fs.readFileSync(bot.commandFolders[folders.indexOf(ctx.args[0])] + '/package.json'));
let mod = `${bot.commandFolders[folders.indexOf(ctx.args[0])]}/${pkg.main}`;

bot.commands.unloadModule(mod);
delete require.cache[require.resolve(mod)];

let unloadedMods = JSON.parse(fs.readFileSync(`${mainDir}/data/unloadedCommands.json`));

unloadedMods.push(mod.split('/').slice(0, -1).join('/'));
fs.writeFileSync(`${mainDir}/data/unloadedCommands.json`, JSON.stringify(unloadedMods));

await ctx.channel.createMessage(`Unloaded module **${ctx.args[0]}**`);
}
};

exports.reload = {
desc: 'Reload a command.',
usage: '<module>',
async main(bot, ctx) {
if (!ctx.args[0]) return await ctx.createMessage('No module given to reload.');
if (!bot.commands.checkModule(ctx.args[0])) return await exports.load.main(bot, ctx);

let folders = bot.commandFolders.map(f => f.split('/').slice(-1)[0]);
let pkg = JSON.parse(fs.readFileSync(bot.commandFolders[folders.indexOf(ctx.args[0])] + '/package.json'));
let mod = `${bot.commandFolders[folders.indexOf(ctx.args[0])]}/${pkg.main}`;

bot.commands.reloadModule(mod);

await ctx.createMessage(`Reloaded module **${ctx.args[0]}**`);
}
};
5 changes: 5 additions & 0 deletions src/cmd/dev/commands/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"main": "command.js",
"dependencies": {
}
}

0 comments on commit 230af5e

Please sign in to comment.