Skip to content

Commit

Permalink
Add plugin manager
Browse files Browse the repository at this point in the history
  • Loading branch information
brandoningli committed May 16, 2022
1 parent 3df5fa0 commit 135ebb9
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,7 @@ dist
.svelte-kit

# End of https://www.toptal.com/developers/gitignore/api/node

# Plugins (These are to be handled in separate repos)
plugins/*
!plugins/README.md
130 changes: 130 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"discord-api-types": "^0.25.2",
"discord.js": "^13.3.1",
"dotenv": "^10.0.0",
"glob": "^8.0.3",
"piston-client": "^1.0.2",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.5"
Expand Down
23 changes: 23 additions & 0 deletions plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# MoDiBo Plugins

## File Structure

Plugins should be placed here, each in their own directory. In that directory should be a `main.js` file with the required blueprint components.

```
plugins/
- testPlugin/
-- main.js
-- supportingFile.js
-- data.json
- testPlugin2/
-- main.js
```

## To Enable a Plugin Installed Here...

`//TODO`

## Other `//TODO`

- Finalize the wording of this document. Limited information about developing plugins should be placed here; the bulk of that should be in the actual documentation somewhere, either in the wiki or website.
4 changes: 4 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const winston = require("winston");
const winstonDiscord = require("./CustomDiscordWebhookTransport.js");
const winstonRotateFile = require("winston-daily-rotate-file");
const utils = require("./utils.js");
const pluginManager = require("./plugin-manager.js");

const PREFIX = "$";

Expand Down Expand Up @@ -115,6 +116,9 @@ const bot = new Client({
bot.on("ready", async () => {
// when loaded (ready event)
bot.user.setActivity(`${PREFIX}help | ${PREFIX}info`, { type: "PLAYING" });

pluginManager.load();

utils.logger.log("debug", `${bot.user.username} is ready...`);
});

Expand Down
67 changes: 67 additions & 0 deletions src/plugin-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const glob = require("glob");
const utils = require("./utils.js");
const PLUGIN_FILES = "../plugins/*/main.js";

/**
* Load plugin modules
* @param {Discord.Client} bot The instantiated Discord Bot object, for use in calling module onLoad
*/
function load(bot) {
//TODO Integrate with Config to only load enabled plugins

utils.logger.log("debug", "Loading Plugins...");

glob.sync(PLUGIN_FILES).forEach((file) => {
let dash = file.split("/");
if (dash.length !== 4) {
return;
}

let dot = dash[3].split(".");
if (dot.length !== 2) {
return;
}

let module = require(file);
let key = module.SLUG;
let loaded = false;

if (module.processCommand) {
utils.plugins.command[key] = module;
loaded = true;
utils.logger.log("debug", `Loaded "${module.NAME}" as a Command Plugin`);
utils.plugins.command[key].processCommand();
}

if (module.processMessage) {
utils.plugins.message[key] = module;
loaded = true;
utils.logger.log("debug", `Loaded "${module.NAME}" as a Message Plugin`);
utils.plugins.message[key].processMessage();
}

if (module.startCron) {
utils.plugins.cron[key] = module;
loaded = true;
utils.logger.log("debug", `Loaded "${module.NAME}" as a Cron Plugin`);
}

if (loaded && module.onLoad) {
utils.logger.log("debug", `Running "${module.NAME}" onLoad function...`);
module.onLoad(bot);
}
});

utils.logger.log("debug", "All Plugins Loaded");
for (const [pluginType, list] of Object.entries(utils.plugins)) {
const pluginNames = Object.values(list).map((p) => `"${p.NAME}"`);
utils.logger.log(
"debug",
`${pluginType} plugins: ${pluginNames.join(", ")}`
);
}
}

module.exports = {
load,
};
8 changes: 8 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ const SECOND = 1000;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;

// Loaded Plugin storage
let plugins = {
command: {},
message: {},
cron: {},
};

/**
* Send a message to a channel of your choice.
* @param {Discord.MessageEmbed|String} content The content to include in the message.
Expand Down Expand Up @@ -141,4 +148,5 @@ module.exports = {
reply,
createEmbed,
logger,
plugins,
};

0 comments on commit 135ebb9

Please sign in to comment.