-
Notifications
You must be signed in to change notification settings - Fork 1
/
library.js
70 lines (58 loc) · 1.81 KB
/
library.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';
const topics = require.main.require('./src/topics');
const socketHelpers = require.main.require('./src/socket.io/helpers');
const meta = require.main.require('./src/meta');
const plg = require('./plugin.json');
const { DiceRoll } = require('rpg-dice-roller');
const plugin = module.exports;
plugin.Name = plg.name;
plugin.name = plg.name.toLowerCase();
plugin.settings = {
botUsername: '',
botUid: 1
};
plugin.init = async function (data) {
let settings = await meta.settings.get(plugin.name);
plugin.settings = {
botUsername: settings.botUsername || '',
botUid: settings.botUid || 1
};
data.router.get(`/admin/plugins/${plugin.name}`, data.middleware.admin.buildHeader, plugin.renderAdminPage);
data.router.get(`/api/admin/plugins/${plugin.name}`, plugin.renderAdminPage);
};
plugin.addAdminNavigation = async function (header) {
header.plugins.push({
route: `/plugins/${plugin.name}`,
icon: 'fa-edit',
name: plugin.Name
});
return header;
};
plugin.rollRE = /^\/roll/i;
plugin.replyWithDiceRoll = async function (data) {
let { uid, tid, content } = data.post;
let rolls = (content || '').replace(/(\r\n|\n|\r)/gm, '\n').split('\n')
.filter(line => {
return line && plugin.rollRE.test(line.trim())
})
.map(line => {
let input = line.trim().replace(plugin.rollRE, '').trim();
return (new DiceRoll(input)).toString();
});
if (!rolls.length) {
return;
}
const postData = await topics.reply({
tid: tid,
uid: plugin.settings.botUid,
content: `${rolls.join('\n')}`
});
socketHelpers.notifyNew(postData.uid, 'newPost', {
posts: [ postData ],
'reputation:disabled': meta.config['reputation:disabled'] === 1,
'downvote:disabled': meta.config['downvote:disabled'] === 1
});
};
plugin.renderAdminPage = function (req, res) {
res.render(`admin/plugins/${plugin.name}`, {});
};