-
Notifications
You must be signed in to change notification settings - Fork 0
/
birthday.js
220 lines (200 loc) · 6.05 KB
/
birthday.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
registerPlugin({
name: 'Birthday Script',
version: '1.1',
description: 'create birthday notifications',
author: 'mcj201',
vars: [
{
name: 'message',
title: 'The message that should be displayed. (%n = nickname, %b = list of birthdays)',
type: 'multiline'
},
{
name: 'type',
title: 'Message-Type',
type: 'select',
options: [
'Private chat',
'Poke'
]
},
{
name: 'nDays',
title: 'send the notification upto this amount of days after birthday',
type: 'number'
},
{
name: 'serverGroup',
title: 'Server group name/id for birthdays',
type: 'string'
}
],
autorun: false,
requiredModules: [
'net'
]
}, function(sinusbot, config, meta) {
const event = require('event')
const engine = require('engine')
const backend = require('backend')
const format = require('format')
const store = require('store');
const net = require('net');
engine.log(`Loaded ${meta.name} v${meta.version} by ${meta.author}.`)
event.on('load', () => {
const command = require('command');
if (!command) {
engine.log('command.js library not found! Please download command.js and enable it to be able use this script!');
return;
}
let bDays = store.get('birthdays') || {};
let notifs = store.get('birthday_notifications') || {};
if (!net) {
engine.log('net library not found! You will not be able to use webDAV sync!');
} else {
syncDavAddressBook();
setInterval(syncDavAddressBook, 1000 * 60);
}
setInterval(updateServerGroups, 1000 * 60);
event.on('clientMove', ({ client, fromChannel }) => {
const avail = getNotifications(client, 30);
if (avail.length < 1)
return;
let msgs = []
for(const uid of avail) {
msgs.push(`${getName(uid)}: ${formatDate(getBday(uid))}`);
}
const msg = config.message.replace('%n', client.name()).replace('%b', msgs.join('\r\n'))
if (!fromChannel) {
if (config.type == '0') {
client.chat(msg)
} else {
client.poke(msg)
}
updateServerGroups();
}
})
command.createCommand('birthdays')
.help('Show user birthdays')
.manual('Show user birthdays from DB.')
.exec((client, args, reply, ev) => {
let msgs = ["List of saved birthdays:"];
for(const uid in bDays) {
msgs.push(`${getName(uid)}: ${formatDate(getBday(uid))}`);
}
reply(msgs.join('\r\n'));
});
command.createCommand('birthday')
.addArgument(command.createArgument('string').setName('date'))
.help('Set user birthdays')
.manual('Save user birthdays to DB.')
.exec((client, args, reply, ev) => {
var date = args.date.split('.');
if(date.length >= 2) {
let m = date[0];
date[0] = date[1];
date[1] = m;
}
date = new Date(date);
if(args.date === "") {
let date = getBday(ev.client.uid());
if(date)
reply(`Your birthday is ${formatDate(date)}.`);
else
reply(`Set your birthday first! e.g. !birthday 24.12.`);
} else if(!isNaN(date)) {
setBday(ev.client, date);
reply(`Your birthday was set to ${formatDate(date)}.`);
} else {
setBday(ev.client, date);
reply(`Your birthday has been cleared.`);
}
});
function setBday(client, date) {
if(isNaN(date) && bDays[client.uid()]) {
delete bDays[client.uid()];
} else if(!isNaN(date)) {
bDays[client.uid()] = [client.name(), date, new Date()];
}
store.set('birthdays', bDays);
}
function getBday(uid) {
if(!bDays[uid])
return undefined;
if(bDays[uid][1])
return new Date(bDays[uid][1]);
else
return undefined;
}
function getName(uid) {
return bDays[uid][0];
}
function getNotifications(client, nDays = 30) {
const start = new Date();
start.setDate(start.getDate()-nDays);
const now = new Date();
let sentNotifs = notifs[client.uid()] || {};
let avail = [];
for(const uid in bDays) {
let bDay = new Date(bDays[uid][1]);
bDay.setFullYear((new Date()).getFullYear());
let lastNotif = new Date(sentNotifs[uid]);
if(bDay >= start && bDay <= now && (isNaN(lastNotif) || lastNotif < start)) {
avail.push(uid);
sentNotifs[uid] = now;
}
}
notifs[client.uid()] = sentNotifs;
store.set('birthday_notifications', notifs);
return avail;
}
function formatDate(dt) {
if(dt)
return `${dt.getDate()}.${dt.getMonth()+1}.`;
else
return 'invalid date';
}
function updateServerGroups() {
if(config.serverGroup === "")
return;
const now = new Date();
for(const client of backend.getClients()) {
if(bDays[client.uid()]) {
const bDay = new Date(bDays[client.uid()][1]);
let hasGroup = false;
for(const group of client.getServerGroups()) {
hasGroup |= group.name() === config.serverGroup || group.id() == config.serverGroup;
}
if(bDay.getDate() === now.getDate() && bDay.getMonth() === now.getMonth()) {
if(!hasGroup) client.addToServerGroup(config.serverGroup);
} else {
if(hasGroup) client.removeFromServerGroup(config.serverGroup);
}
}
}
}
function syncDavAddressBook() {
const conn = net.connect({
url: 'ws://127.0.0.1:23845',
port: 23845,
protocol: 'ws'
}, err => {
// log connection errors if any
if (err) {
engine.log(err);
}
});
if (conn) {
conn.on('data', data => {
engine.log('received data');
engine.log(data.toString());
bDays = JSON.parse(data);
store.set('birthdays', bDays);
})
conn.write(JSON.stringify(bDays));
} else {
engine.log('ws connection unavailable');
}
}
});
});