-
Notifications
You must be signed in to change notification settings - Fork 0
/
WL.js
134 lines (122 loc) · 5.26 KB
/
WL.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
124
125
126
127
128
129
130
131
132
133
134
'use strict';
let fs = require('fs');
let https = require('https');
const Autolinker = require('autolinker');
let regdateCache = {};
exports.WL = {
nameColor: function (name, bold, userGroup) {
let userGroupSymbol = Users.usergroups[toId(name)] ? '<b><font color=#948A88>' + Users.usergroups[toId(name)].substr(0, 1) + '</font></b>' : "";
return (userGroup ? userGroupSymbol : "") + (bold ? "<b>" : "") + "<font color=" + WL.hashColor(name) + ">" + (Users(name) && Users(name).connected && Users.getExact(name) ? Chat.escapeHTML(Users.getExact(name).name) : Chat.escapeHTML(name)) + "</font>" + (bold ? "</b>" : "");
},
// usage: WL.nameColor(user.name, true) for bold OR WL.nameColor(user.name, false) for non-bolded.
messageSeniorStaff: function (message, pmName, from) {
pmName = (pmName ? pmName : '~Wavelength Server');
from = (from ? ' (PM from ' + from + ')' : '');
Users.users.forEach(curUser => {
if (curUser.can('roomowner')) {
curUser.send('|pm|' + pmName + '|' + curUser.getIdentity() + '|' + message + from);
}
});
},
// format: WL.messageSeniorStaff('message', 'person')
//
// usage: WL.messageSeniorStaff('Mystifi is a confirmed user and they were banned from a public room. Assess the situation immediately.', '~Server')
//
// this makes a PM from ~Server stating the message
regdate: function (target, callback) {
target = toId(target);
if (regdateCache[target]) return callback(regdateCache[target]);
let req = https.get('https://pokemonshowdown.com/users/' + target + '.json', res => {
let data = '';
res.on('data', chunk => {
data += chunk;
}).on('end', () => {
try {
data = JSON.parse(data);
} catch (e) {
return callback(false);
}
let date = data['registertime'];
if (date !== 0 && date.toString().length < 13) {
while (date.toString().length < 13) {
date = Number(date.toString() + '0');
}
}
if (date !== 0) {
regdateCache[target] = date;
saveRegdateCache();
}
callback((date === 0 ? false : date));
});
});
req.end();
},
/* eslint-disable no-useless-escape */
parseMessage: function (message) {
if (message.substr(0, 5) === "/html") {
message = message.substr(5);
message = message.replace(/\_\_([^< ](?:[^<]*?[^< ])?)\_\_(?![^<]*?<\/a)/g, '<i>$1</i>'); // italics
message = message.replace(/\*\*([^< ](?:[^<]*?[^< ])?)\*\*/g, '<b>$1</b>'); // bold
message = message.replace(/\~\~([^< ](?:[^<]*?[^< ])?)\~\~/g, '<strike>$1</strike>'); // strikethrough
message = message.replace(/<<([a-z0-9-]+)>>/g, '«<a href="/$1" target="_blank">$1</a>»'); // <<roomid>>
message = Autolinker.link(message.replace(///g, '/'), {stripPrefix: false, phone: false, twitter: false});
return message;
}
message = Chat.escapeHTML(message).replace(///g, '/');
message = message.replace(/\_\_([^< ](?:[^<]*?[^< ])?)\_\_(?![^<]*?<\/a)/g, '<i>$1</i>'); // italics
message = message.replace(/\*\*([^< ](?:[^<]*?[^< ])?)\*\*/g, '<b>$1</b>'); // bold
message = message.replace(/\~\~([^< ](?:[^<]*?[^< ])?)\~\~/g, '<strike>$1</strike>'); // strikethrough
message = message.replace(/<<([a-z0-9-]+)>>/g, '«<a href="/$1" target="_blank">$1</a>»'); // <<roomid>>
message = Autolinker.link(message, {stripPrefix: false, phone: false, twitter: false});
return message;
},
/* eslint-enable no-useless-escape */
randomString: function (length) {
return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
},
reloadCSS: function () {
const cssPath = 'wavelength'; // This should be the server id if Config.serverid doesn't exist. Ex: 'serverid'
let req = https.get('https://play.pokemonshowdown.com/customcss.php?server=' + (Config.serverid || cssPath), () => {});
req.end();
},
//Daily Rewards System for Wavelength by Lord Haji
giveDailyReward: function (user) {
if (!user || !user.named) return false;
let reward = 0, time = Date.now();
for (let ip in user.ips) {
let cur = Db.DailyBonus.get(ip);
if (!cur) {
cur = [1, Date.now()];
Db.DailyBonus.set(ip, cur);
}
if (cur[0] < reward || !reward) reward = cur[0];
if (cur[1] < time) time = cur[1];
}
if (Date.now() - time < 86400000) return;
reward++;
if (reward > 7 || Date.now() - time > 172800000) reward = 1;
// Loop again to set the ips values
for (let ip in user.ips) {
Db.DailyBonus.set(ip, [reward, Date.now()]);
}
Economy.writeMoney(user.userid, reward);
user.send('|popup||wide||html| <center><u><b><font size="3">Wavelength Daily Bonus</font></b></u><br>You have been awarded ' + reward + ' Stardust.<br>' + showDailyRewardAni(reward) + '<br>Because you have connected to the server for the past ' + (reward === 1 ? 'Day' : reward + ' Days') + '.</center>');
},
};
// last two functions needed to make sure WL.regdate() fully works
function loadRegdateCache() {
try {
regdateCache = JSON.parse(fs.readFileSync('config/regdate.json', 'utf8'));
} catch (e) {}
}
loadRegdateCache();
function saveRegdateCache() {
fs.writeFileSync('config/regdate.json', JSON.stringify(regdateCache));
}
function showDailyRewardAni(streak) {
let output = '';
for (let i = 1; i <= streak; i++) {
output += "<img src='http://i.imgur.com/ZItWCLB.png' width='16' height='16'> ";
}
return output;
}