Skip to content

Create a new plugins

lequanglam edited this page Jun 20, 2019 · 9 revisions

A plugin contains a plugin.json file, a javascript executable file, and other files that plugin uses.

plugin.json

plugin.json is a file that contain information about the plugin, command list, authors, etc...

Example:

{
	"plugin_name": "Ping",
	"plugin_scope": "ping",
	"plugin_exec": "ping.js",
	"command_map": {
		"ping": {
			"hargs": "",
			"hdesc": "Ping",
			"fscope": "fping",
			"compatibly": 0
		}
	},
	"author": "",
	"version": "1",
	"node_depends": {
		"http": "*"
	}
}

That plugin.json content above will define this plugin is named "Ping", version 1 and requires a node modules named http, and contains a command /ping with compatibly 0 (more on this later).

plugin_name, plugin_scope and plugin_exec is required, but author and version is not. Also command_map and node_depends is required but content inside isn't.

Also, there's some properties that is not required but some plugin might contains it like:

  • file_map: To map file inside plugin file for the code to use.
  • dependents: An array. If your plugin need to access other plugins, bot will read this property and check if plugin defined (by plugin names) in here is installed. If not, your plugin will not load until that plugin is installed. file_map is an object which structures is name (directory with file names): value (custom names). After mapped, files can be accessed via global.fileMap[(custom names)] and stored as fs.ReadableStream

Per command, there is:

  • hargs: Some help about arguments required on the command
  • hdesc: Some information about what the command does
  • fscope: Function name in plugin_scope that contains code capable of processing input data
  • compatibly: Because this bot can be cross-platform (like Facebook, Discord, ...), this value defines the command can run on what. This value is treated as binary. Every bits on compatibly is assigned to a platform. For example: 00000010 convert to decimal is 2, we enter 2 into compatibly. Now if we run the bot, then only Discord can execute that command, but not everything else. If we enter 1 (00000001) into compatibly, then only Facebook can execute that command. Currently the structure of compatibly is 000000DF where 0 is Not assigned, D is Discord, F is Facebook. If the value is 0, it'll indicate that this command can run on every platform this bot supported.

Javascript executable file

This file contains javascript code required to run commands. It need to be named after the name written in config.json (plugin_exec).

Example of content (plugin Ping):

var pingfunc = function (type, data) {
	return {
		handler: "core",
		data: "Pong!"
	}
}

module.exports = {
	fping: pingfunc
}

Ok, so if the function is a function that will be called by bots, then it will be called with 2 arguments type and data.

type is a string that let the function knows that an user from "type" called a command that linked to the function. It can be "Facebook", or "Discord".

data is an object contains pretty much any data from users. It has a structure like this:

{
	args: ["Some", "arguments", "that", "users", "typed", "alongside", "commands"],
	time: 123456789, //Unix time inform received time
	msgdata: {}, //An object returned by facebook-chat-api or discord.js
	api: {}, //Facebook only. This is an object facebook-chat-api return when log in.
	prefix: "[Bot]", //Used only if response is handled by plugin itself.
	admin: false, //Indicate if users requested the command is admin configured in config.
	mentions: {
		"FB-0": "@Someone", //Mentions from facebook-chat-api
		"DC-0": {} //An MessageMention returned from discord.js
	},
	client: {} //Discord only. A Discord.Client object returned by discord.js
}
Clone this wiki locally