-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
190 lines (178 loc) · 6.43 KB
/
bot.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
var Discord = require('discord.io');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var auth = require('./auth.json');
var winston = require('winston');
var PAUL = '492601218313748480';
var BOSS = '496641958970785792';
var BOBS = ['248152313410093057', '157606631293714432', '492601758003232788'];
var ONZINCHAT = '492600858081624064';
var TIME = {};
var logger = winston.createLogger({
level: 'debug',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(info => {
return `${info.timestamp} ${info.level}: ${info.message}`;
})
),
transports: [new winston.transports.Console()]
});
// Initialize Discord Bot
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
bot.on('ready', function (evt) {
logger.info('Connected');
logger.info('Logged in as: ');
logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
logger.info(user + " " + userID + " " + channelID);
// Our bot needs to know if it will execute a command
// It will listen for messages that will start with `!`
if (message.substring(0, 1) == '!') {
var args = message.substring(1).split(' ');
var cmd = args[0];
//args = args.splice(1);
switch(cmd) {
case 'beer':
var today = new Date();
if (args[1] == "ineedit") {
send_message_to_discord('If it\'s for medical purposes, you can drink as much as you need!', channelID);
} else if (BOBS.includes(userID)) {
send_message_to_discord('You don\'t even drink, who are you kidding?', channelID);
} else if (userID == BOSS) {
send_message_to_discord('You\'re the boss, you can drink whenever you want!', channelID);
} else if ((today.getDay() == 5) && (today.getHours() >= 14)) {
send_message_to_discord('It\'s friday and it\'s after 16h, so the chances are that you could drink a beer and get away with it!', channelID);
} else {
send_message_to_discord('It\'s not yet friday 16h! Drink water or something!', channelID);
}
break;
case 'slap':
send_raw_message_to_discord('_Slaps <@157606631293714432>_', channelID);
break;
case 'friet':
send_message_to_discord('again?', channelID);
break;
case 'ping':
send_message_to_discord('pong', channelID);
break;
case 'fortune':
var Http = new XMLHttpRequest();
const url = 'http://yerkee.com/api/fortune';
Http.open("GET", url);
Http.send();
Http.onreadystatechange=(e)=> {
var text = Http == null? "" : Http.responseText;
text = text.substring(11, text.length - 1);
// Fuck this shit
text = text.replace("\\n\\t\\t", " ");
text = text.replace("\\t", " ");
text = text.replace("\\t", " ");
text = text.replace("\\t", " ");
text = text.replace("\\t", " ");
text = text.replace("\\n", " ");
text = text.replace("\\n", " ");
text = text.replace("\\n", " ");
text = text.replace("\\n", " ");
text = text.replace("\\\"", " ");
if (text.length > 0) {
send_message_to_discord(text, channelID);
Http = null;
}
}
break;
case 'spam':
var tot = 1000;
if (args[1] != undefined && args[1] != null) {
tot = parseInt(args[1]);
}
for (var i = 0; i < tot; i++) {
send_raw_message_to_discord('Sorry <@' + userID + '> made me do it :\'(', channelID);
}
break;
case 'onzin':
send_raw_message_to_discord('***Not the <#' + ONZINCHAT + '> chat***', channelID);
break;
case 'timer':
if (TIME.user == undefined || TIME.user == null) {
TIME.user = new Date().getTime();
send_message_to_discord('Starting timer for ' + user, channelID);
} else {
var DELTA = new Date().getTime() - TIME.user;
TIME.user = null;
send_message_to_discord(DELTA + ' millies have passed for user ' + user, channelID);
}
break;
case 'jira':
var action = args[0];
var ticket = args[1];
switch(action) {
case 'to_do':
case 'td':
if (args.length == 2) {
var base_url = 'http://jira.adhese.org/rest/api/latest/issue/AD-' + ticket;
logger.info('base_url is ' + base_url);
send_message_to_discord('Moving ticket #' + ticket + ' to to_do status!', channelID);
}
break;
case 'in_progress':
case 'ip':
send_message_to_discord('Moving ticket #' + ticket + ' to in_progress status!', channelID);
break;
case 'code_review':
case 'cr':
send_message_to_discord('Moving ticket #' + ticket + ' to code_review status!', channelID);
break;
case 'functional_review':
case 'fr':
send_message_to_discord('Moving ticket #' + ticket + ' to functional_review status!', channelID);
break;
case 'assign':
var assignee = args[2];
var json = '{"fields": {"assignee":{"name":"' + assignee + '"}}}';
send_message_to_discord('Assigning ticket #' + ticket + ' to ' + assignee + '!', channelID);
break;
case 'done':
send_message_to_discord('Moving ticket #' + ticket + ' to done status!', channelID);
break;
case 'help':
send_message_to_discord('Possible commands:\n!jira to_do <ticket_number>: moves ticket to to_do status\n'
+ '!jira in_progress <ticket_number>: moves ticket to in_progress status\n'
+ '!jira code_review <ticket_number>: moves ticket to code_review status\n'
+ '!jira functional_review <ticket_number>: moves ticket to functional_review status\n'
+ '!jira assign <ticket_number> <assignee>: assigns ticket to assignee\n'
+ '!jira done <ticket_number>: moves ticket to done status', channelID);
break;
default:
send_message_to_discord('I do not recognize your command: ' + action + ' ; Type !jira help to see a list of possible commands', channelID);
break;
}
break;
}
} else if (message.toLowerCase().includes("permission") && channelID != ONZINCHAT) {
tag(PAUL, channelID);
} else if (message == '#suckstobeyou') {
send_message_to_discord('Haha', channelID);
}
});
function send_message_to_discord(message_to_display, channelID) {
bot.sendMessage({
to:channelID,
message: '```' + message_to_display + '```'
});
}
function send_raw_message_to_discord(message_to_display, channelID) {
bot.sendMessage({
to:channelID,
message: message_to_display
});
}
function tag(userID, channelID) {
bot.sendMessage({
to:channelID,
message: '<@'+userID+'>'
});
}