-
Notifications
You must be signed in to change notification settings - Fork 5
/
library.js
123 lines (100 loc) · 3.46 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(function(module) {
'use strict';
var User = require.main.require('./src/user');
var Topics = require.main.require('./src/topics');
var Categories = require.main.require('./src/categories');
var translator = require.main.require('./src/translator');
var meta = require.main.require('./src/meta');
var nconf = require.main.require('nconf');
var async = require.main.require('async');
var Discord = require('discord.js');
var hook = null;
var forumURL = nconf.get('url');
var plugin = {
config: {
webhookURL: '',
maxLength: '',
postCategories: '',
topicsOnly: '',
messageContent: ''
},
regex: /https:\/\/discord(?:app)?\.com\/api\/webhooks\/([0-9]+?)\/(.+?)$/
};
plugin.init = function(params, callback) {
function render(req, res, next) {
res.render('admin/plugins/discord-notification', {});
}
params.router.get('/admin/plugins/discord-notification', params.middleware.admin.buildHeader, render);
params.router.get('/api/admin/plugins/discord-notification', render);
meta.settings.get('discord-notification', function(err, settings) {
for (var prop in plugin.config) {
if (settings.hasOwnProperty(prop)) {
plugin.config[prop] = settings[prop];
}
}
// Parse Webhook URL (1: ID, 2: Token)
var match = plugin.config['webhookURL'].match(plugin.regex);
if (match) {
hook = new Discord.WebhookClient(match[1], match[2]);
}
});
callback();
},
plugin.postSave = function(post) {
post = post.post;
var topicsOnly = plugin.config['topicsOnly'] || 'off';
if (topicsOnly === 'off' || (topicsOnly === 'on' && post.isMain)) {
var content = post.content;
async.parallel({
user: function(callback) {
User.getUserFields(post.uid, ['username', 'picture'], callback);
},
topic: function(callback) {
Topics.getTopicFields(post.tid, ['title', 'slug'], callback);
},
category: function(callback) {
Categories.getCategoryFields(post.cid, ['name', 'bgColor'], callback);
}
}, function(err, data) {
var categories = JSON.parse(plugin.config['postCategories']);
if (!categories || categories.indexOf(String(post.cid)) >= 0) {
// Trim long posts:
var maxQuoteLength = plugin.config['maxLength'] || 1024;
if (content.length > maxQuoteLength) { content = content.substring(0, maxQuoteLength) + '...'; }
// Ensure absolute thumbnail URL if an avatar exists:
var thumbnail = null;
if (data.user.picture && data.user.picture.match(/^\//)) {
thumbnail = forumURL + data.user.picture;
} else if (data.user.picture) {
thumbnail = data.user.picture;
}
// Add custom message:
var messageContent = plugin.config['messageContent'] || '';
// Make the rich embed:
var embed = new Discord.MessageEmbed()
.setColor(data.category.bgColor)
.setURL(forumURL + '/topic/' + data.topic.slug)
.setTitle(data.category.name + ': ' + data.topic.title)
.setDescription(content)
.setFooter(data.user.username, thumbnail)
.setTimestamp();
// Send notification:
if (hook) {
hook.send(messageContent, {embeds: [embed]}).catch(console.error);
}
}
});
}
},
plugin.addAdminMenu = function(header, callback) {
translator.translate('[[discord-notification:title]]', function(title) {
header.plugins.push({
route : '/plugins/discord-notification',
icon : 'fa-bell',
name : title
});
callback(null, header);
});
};
module.exports = plugin;
}(module));