-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.plugin.js
More file actions
48 lines (41 loc) · 2.17 KB
/
Copy pathexample.plugin.js
File metadata and controls
48 lines (41 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const { createSidebarApi } = require("rprox"); // handed out by the proxy
// this import is not needed, but allows you to use the types in your plugin code, along with the various api interfaces available in the proxy (ie. Session, Chat, and in this case, the SidebarApi)
module.exports = {
name: "example",
version: "1.0.0",
description: "An example plugin for the rProx proxy.",
defaultConfig: {
enabled: false,
reply: "pong",
},
setup(api) {
const config = api.pluginConfig ?? this.defaultConfig; // Use a reference to the plugin here
// this lets you have a reference of the plugins config object, instead of a copy of the current value you'd have by doing
// const reply = api.pluginConfig.reply; // this would be a copy of the value, and if the config is changed, it would not be updated until the process restarted
const bars = new Map(); // sidebars
const sidebarFor = (session) => { // get or create a sidebar for this session
let sidebar = bars.get(session.id);
if (!sidebar) {
sidebar = createSidebarApi(
{ sendPacket: (name, data) => session.sendPacket(name, data) },
{ onError: (error) => api.log.error(`sidebar: ${error}`) },
);
bars.set(session.id, sidebar);
}
return sidebar;
};
api.registerCommand("ping", async (args, session) => { session.chat.text(config.reply) },
"Responds with 'pong' to test the plugin.");
api.registerCommand("togglesidebar", async (args, session) => {
const sidebar = sidebarFor(session);
sidebar.setVisible(!sidebar.enabled ? true : false);
sidebar.setEnabled(!sidebar.enabled);
session.chat.text(`Sidebar is now ${sidebar.enabled ? "enabled" : "disabled"}.`);
}, "Toggles the sidebar on or off.");
api.on("serverPacket", (name, data, session) => sidebarFor(session).handlePacket(name, data));
api.on("sessionEnd", (session) => { // clean up
bars.get(session.id)?.dispose();
bars.delete(session.id);
});
},
};