mirror of
https://github.com/Hessenuk/DiscordTickets.git
synced 2025-09-03 00:41:27 +03:00
Improve plugins, make settings server separate
This commit is contained in:
@@ -5,7 +5,6 @@ const fs = require('fs');
|
||||
const { join } = require('path');
|
||||
const { path } = require('../../utils/fs');
|
||||
const { createMessage, flags } = require('../../utils/discord');
|
||||
const Plugin = require('../plugins/plugin');
|
||||
|
||||
/**
|
||||
* A command
|
||||
|
@@ -1,6 +1,4 @@
|
||||
module.exports = {
|
||||
CommandManager: require('./manager'),
|
||||
Command: require('./command'),
|
||||
OptionTypes: {
|
||||
SUB_COMMAND: 1,
|
||||
SUB_COMMAND_GROUP: 2,
|
||||
@@ -15,5 +13,5 @@ module.exports = {
|
||||
Pong: 1,
|
||||
ChannelMessageWithSource: 4,
|
||||
DeferredChannelMessageWithSource: 5,
|
||||
},
|
||||
}
|
||||
};
|
@@ -182,7 +182,7 @@ module.exports = class CommandManager {
|
||||
// if (typeof res === 'object' || typeof res === 'string')
|
||||
// cmd.sendResponse(interaction, res, res.secret);
|
||||
} catch (e) {
|
||||
this.client.log.warn(`[COMMANDS] An error occurred whilst executed the ${cmd} command`);
|
||||
this.client.log.warn(`(COMMANDS) An error occurred whilst executed the ${cmd_name} command`);
|
||||
this.client.log.error(e);
|
||||
}
|
||||
|
||||
|
@@ -1,4 +0,0 @@
|
||||
module.exports = {
|
||||
PluginManager: require('./manager'),
|
||||
Plugin: require('./plugin')
|
||||
};
|
@@ -3,10 +3,6 @@ const { Collection, Client } = require('discord.js');
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const Plugin = require('./plugin');
|
||||
|
||||
const fs = require('fs');
|
||||
const { join } = require('path');
|
||||
const { path } = require('../../utils/fs');
|
||||
|
||||
/**
|
||||
* Manages the loading of plugins
|
||||
*/
|
||||
@@ -24,17 +20,22 @@ module.exports = class PluginManager {
|
||||
|
||||
/** Array of official plugins to be used to check if a plugin is official */
|
||||
this.official = [
|
||||
'dsctickets.settings-server',
|
||||
'dsctickets.portal'
|
||||
];
|
||||
}
|
||||
|
||||
handleError(id) {
|
||||
if (!this.official.includes(id))
|
||||
this.client.log.notice(`"${id}" is NOT an official plugin, please do not ask for help with it in the Discord Tickets support server, seek help from the plugin author instead.`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register and load a plugin
|
||||
* @param {Boolean} npm Installed by NPM?
|
||||
* @param {Plugin} Main The Plugin class
|
||||
* @param {Object} pkg Contents of package.json
|
||||
* @param {Plugin} plugin - the Plugin class
|
||||
* @param {Object} pkg - contents of package.json
|
||||
*/
|
||||
register(npm, Main, pkg) {
|
||||
register(plugin, pkg) {
|
||||
let {
|
||||
name: id,
|
||||
version,
|
||||
@@ -43,7 +44,7 @@ module.exports = class PluginManager {
|
||||
} = pkg;
|
||||
|
||||
if (this.plugins.has(id)) {
|
||||
this.client.log.warn(`[PLUGINS] A plugin with the ID "${id}" is already loaded, skipping`);
|
||||
this.client.log.warn(`(PLUGINS) A plugin with the ID "${id}" is already loaded, skipping`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -51,10 +52,6 @@ module.exports = class PluginManager {
|
||||
author = author.name || 'unknown';
|
||||
}
|
||||
|
||||
let loading = npm ? 'Loading' : 'Sideloading';
|
||||
|
||||
this.client.log.plugins(`${loading} "${id}" v${version} by ${author}`);
|
||||
|
||||
let about = {
|
||||
id,
|
||||
version,
|
||||
@@ -63,16 +60,13 @@ module.exports = class PluginManager {
|
||||
};
|
||||
|
||||
try {
|
||||
let plugin = new Main(this.client, about);
|
||||
plugin = new (plugin(Plugin))(this.client, about);
|
||||
this.plugins.set(id, plugin);
|
||||
plugin.preload();
|
||||
|
||||
this.client.log.plugins(`Loading "${plugin.name}" v${version} by ${author}`);
|
||||
plugin.preload();
|
||||
} catch (e) {
|
||||
if (npm) {
|
||||
this.client.log.warn(`An error occurred whilst loading "${id}"`);
|
||||
} else {
|
||||
this.client.log.warn(`An error occurred whilst sideloading "${id}"; have you manually installed its dependencies?`);
|
||||
}
|
||||
this.handleError(id);
|
||||
this.client.log.warn(`An error occurred whilst loading the "${id}" plugin`);
|
||||
this.client.log.error(e);
|
||||
process.exit();
|
||||
}
|
||||
@@ -80,27 +74,18 @@ module.exports = class PluginManager {
|
||||
|
||||
/** Automatically register and load plugins */
|
||||
load() {
|
||||
// normal plugins (NPM)
|
||||
this.client.config.plugins.forEach(plugin => {
|
||||
try {
|
||||
let main = require(plugin);
|
||||
let pkg = require(`${plugin}/package.json`);
|
||||
this.register(true, main, pkg);
|
||||
this.register(main, pkg);
|
||||
} catch (e) {
|
||||
this.handleError(plugin);
|
||||
this.client.log.warn(`An error occurred whilst loading ${plugin}; have you installed it?`);
|
||||
this.client.log.error(e);
|
||||
process.exit();
|
||||
}
|
||||
});
|
||||
|
||||
// sideload plugins for development
|
||||
const dirs = fs.readdirSync(path('./user/plugins'));
|
||||
dirs.forEach(dir => {
|
||||
if (!fs.existsSync(path(`./user/plugins/${dir}/package.json`))) return;
|
||||
let pkg = require(`../../../user/plugins/${dir}/package.json`);
|
||||
let main = require(join(`../../../user/plugins/${dir}/`, pkg.main));
|
||||
this.register(false, main, pkg);
|
||||
});
|
||||
}
|
||||
|
||||
};
|
@@ -1,6 +1,10 @@
|
||||
/* eslint-disable no-unused-vars */
|
||||
const { Client } = require('discord.js');
|
||||
|
||||
const Command = require('../commands/command');
|
||||
const {
|
||||
OptionTypes,
|
||||
ResponseTypes
|
||||
} = require('../commands/helpers');
|
||||
const fs = require('fs');
|
||||
const { join } = require('path');
|
||||
const { path } = require('../../utils/fs');
|
||||
@@ -24,8 +28,8 @@ module.exports = class Plugin {
|
||||
/** The PluginManager */
|
||||
this.manager = this.client.plugins;
|
||||
|
||||
// Object.assign(this, this.manager.plugins.get(id));
|
||||
// make JSDoc happy
|
||||
/** An official plugin? */
|
||||
this.official = this.manager.official.includes(this.id);
|
||||
|
||||
let {
|
||||
id,
|
||||
@@ -81,6 +85,12 @@ module.exports = class Plugin {
|
||||
name: clean,
|
||||
path: path(`./user/plugins/${clean}`)
|
||||
};
|
||||
|
||||
this.helpers = {
|
||||
Command,
|
||||
OptionTypes,
|
||||
ResponseTypes
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user