-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (47 loc) · 1.45 KB
/
index.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
const SlackBot = require('slackbots');
const config = require('./config.json');
const listJS = require('./listJS');
const listPHP = require('./listPHP');
const bot = new SlackBot({
token: config.token,
name: 'Who\'s Deleguate'
});
//If error on start
bot.on('error', (err) => console.log(err));
//Message
bot.on('message', (data) => {
if(data.type !== 'message') {
return;
}
handleMessage(data.text);
});
//Respons to Data
function handleMessage(message) {
if (message.includes('!deleguate')) {
displayDeleguates(Number(new Date().getWeekNumber()));
}
}
//Get the current week
Date.prototype.getWeekNumber = function(){
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
};
//Get index of actual week
function displayDeleguates(week) {
let getIndex = () => {
for(let i = 0; i < listJS.list.length; i++) {
if (listJS.list[i].week === week) {
return i;
}
}
}
//Display list of Deleguates
let index = getIndex();
bot.postMessageToChannel(
'général', //Change if you need to post in an other channel
`Semaine ${week} : Les délégués de cette semaine sont ${listJS.list[index].deleguates} pour les JavaScript ainsi que ${listPHP.list[index].deleguates} pour les PHP !`
)
}