-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·309 lines (296 loc) · 8.89 KB
/
server.js
File metadata and controls
executable file
·309 lines (296 loc) · 8.89 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
var Discord = require("discord.js");
var config = require('./config.js');
var edsm = require('./edsm.js');
const VERSION = "FGEBot Version 0.3.2";
var FGEBot = new Discord.Client();
var startTime = Date.now();
var enumerate = function(obj) {
var key;
for (key in obj) {
if (typeof obj[key] !== 'function') {
console.log(key + ": " + obj[key]);
}
}
}
var messagebox;
try{
messagebox = require("./messagebox.json");
} catch(e) {
//no stored messages
messagebox = {};
}
function updateMessagebox(){
require("fs").writeFile("./messagebox.json",JSON.stringify(messagebox,null,2), null);
}
var compileArgs = function(args) {
args.splice(0,1);
return args.join(" ");
}
var commands = {
"chortle": {
help: "Make 'em laugh...",
process: function(args, bot, message) { bot.sendMessage(message.channel, "*chortle*!"); }
},
"ping": {
help: "Returns pong. Useful for determining if the bot is alive.",
process: function(args, bot, message) { bot.sendMessage(message.channel, "Pong!"); }
},
"pat": {
usage: "<name>",
help: "Rough day? Comfort someone with this command.",
process: function(args, bot, message) { bot.sendMessage(message.channel, "There, there, " + compileArgs(args)); }
},
"version": {
help: "Display version information for this bot.",
process: function(args, bot, message) { bot.sendMessage(message.channel, VERSION); }
},
"servers": {
help: "lists servers bot is connected to",
process: function(args, bot, msg) { bot.sendMessage(msg.channel,bot.servers); }
},
"channels": {
help: "lists channels bot is connected to",
process: function(args, bot, msg) { bot.sendMessage(msg.channel,bot.channels); }
},
"myid": {
help: "returns the user id of the sender",
process: function(args, bot, msg) { bot.sendMessage(msg.channel,msg.author.id); }
},
"say": {
usage: "<message>",
help: "bot says message",
process: function(args, bot,msg) { bot.sendMessage(msg.channel,compileArgs(args));}
},
"announce": {
usage: "<message>",
help: "bot says message with text to speech",
process: function(args, bot,msg) { bot.sendMessage(msg.channel,compileArgs(args),{tts:true});}
},
"userid": {
usage: "<user to get id of>",
help: "Returns the unique id of a user. This is useful for permissions.",
process: function(args,bot,msg) {
var suffix = compileArgs(args);
console.log("userid [" + suffix + "]");
if(suffix){
var server = msg.channel.server;
if (server) {
var users = server.members.getAll("username",suffix);
if(users.length == 1){
bot.sendMessage(msg.channel, "The id of " + users[0] + " is " + users[0].id)
} else if(users.length > 1){
var response = "multiple users found:";
for(var i=0;i<users.length;i++){
var user = users[i];
response += "\nThe id of " + user + " is " + user.id;
}
bot.sendMessage(msg.channel,response);
} else {
bot.sendMessage(msg.channel,"No user " + suffix + " found!");
}
} else {
bot.sendMessage(msg.channel, "userid can only be run from a server channel, not a private message.");
}
} else {
bot.sendMessage(msg.channel, "The id of " + msg.author + " is " + msg.author.id);
}
}
},
"topic": {
usage: "[topic]",
help: 'Sets the topic for the channel. No topic removes the topic.',
process: function(args,bot,msg) {
bot.setChannelTopic(msg.channel,compileArgs(args), function(error) {
console.log("Channel topic result: " + error);
});
}
},
"msg": {
usage: "<user> <message to leave user>",
help: "leaves a message for a user the next time they come online",
process: function(args,bot,msg) {
var user = args.shift();
var message = args.join(' ');
if(user.startsWith('<@')){
user = user.substr(2,user.length-3);
}
var target = msg.channel.server.members.get("id",user);
if(!target){
target = msg.channel.server.members.get("username",user);
}
messagebox[target.id] = {
channel: msg.channel.id,
content: target + ", " + msg.author + " said: " + message
};
updateMessagebox();
bot.sendMessage(msg.channel,"message saved.")
}
},
"uptime": {
usage: "",
help: "returns the amount of time since the bot started",
process: function(args,bot,msg){
var now = Date.now();
var msec = now - startTime;
console.log("Uptime is " + msec + " milliseconds");
var days = Math.floor(msec / 1000 / 60 / 60 / 24);
msec -= days * 1000 * 60 * 60 * 24;
var hours = Math.floor(msec / 1000 / 60 / 60);
msec -= hours * 1000 * 60 * 60;
var mins = Math.floor(msec / 1000 / 60);
msec -= mins * 1000 * 60;
var secs = Math.floor(msec / 1000);
var timestr = "";
if(days > 0) {
timestr += days + " days ";
}
if(hours > 0) {
timestr += hours + " hours ";
}
if(mins > 0) {
timestr += mins + " minutes ";
}
if(secs > 0) {
timestr += secs + " seconds ";
}
bot.sendMessage(msg.channel,"Uptime: " + timestr);
}
},
"loc": {
usage: "<name>",
help: 'Gets the location of a commander',
process: function(args,bot,msg) {
edsm.getPosition(compileArgs(args), bot, msg);
}
},
"syscoords": {
usage: "<system>",
help: 'Gets the galactic coordinates of a system',
process: function(args,bot,msg) {
var system = compileArgs(args);
edsm.getSystemCoords(system, bot, msg);
}
},
"cmdrcoords": {
usage: "<name>",
help: "Gets the locatino of a commander, including system coordinates, if they are available",
process: function(args,bot,msg) {
edsm.getCmdrCoords(compileArgs(args), bot, msg);
}
},
"dist": {
usage: "<first> -> <second>",
help: "Gets the distance from one system or commander to another. If <second> is not given, gets the distance from first to Sol",
process: function(args,bot,msg) {
var query = compileArgs(args).split("->");
var first = query[0].trim();
var second = null;
if (query.length == 1) {
second = "Sol";
} else {
second = query[1].trim();
}
edsm.getDistance(first, second, bot, msg);
}
},
"aliases": {
help: "Returns the list of supported alias systems",
process: function(args,bot,msg) {
var key;
var output = "Supported stellar aliases:";
for (key in edsm.aliases) {
if (typeof edsm.aliases[key] != 'function') {
output += "\n *" + key + " -> " + edsm.aliases[key];
}
}
bot.sendMessage(msg.channel, output);
}
},
"help": {
help: "Display help for this bot.",
process: function(args, bot, msg) {
var output = VERSION + " commands:";
var key;
for (key in commands) {
output += "\n\t!";
output += key;
var usage = commands[key].usage;
if(usage){
output += " " + usage;
}
output += "\n\t\t\t";
output += commands[key].help;
}
// console.log(output);
bot.sendMessage(msg.channel, output);
}
},
};
FGEBot.on("message", function(message){
if (message.author !== FGEBot.user) {
console.log("[" + FGEBot.user + "] Got message from " + message.author + ": " + message);
if (message.content.startsWith("!")) {
messageContent = message.content.substr(1);
// First word is a command
var args = messageContent.split(" ");
var cmd = commands[args[0]];
if(cmd) {
try{
cmd.process(args, FGEBot, message);
} catch(e){
if(config.debug){
FGEBot.sendMessage(message.channel, "command " + message.content + " failed :(\n" + e.stack);
}
}
} else {
if(config.respondToInvalid){
FGEBot.sendMessage(message.channel, "Invalid command " + message.content);
}
}
} else if (message.author != FGEBot.user && message.isMentioned(FGEBot.user)) {
FGEBot.sendMessage(message.channel,message.author + ", you called?");
}
}
});
//Log user status changes
FGEBot.on("presence", function(user,status,gameId) {
//if(status === "online"){
//console.log("presence update");
// console.log(user+" went "+status);
//}
try{
if(status != 'offline'){
if(messagebox.hasOwnProperty(user.id)){
console.log("found message for " + user.id);
var message = messagebox[user.id];
var channel = FGEBot.channels.get("id",message.channel);
delete messagebox[user.id];
updateMessagebox();
FGEBot.sendMessage(channel,message.content);
}
}
}catch(e){}
});
FGEBot.login(config.LOGIN, config.PASSWORD, function(error, token) {
if (error) {
console.log("Error logging in: " + error);
}
if (token) {
console.log(VERSION + " logged in with token " + token);
}
});
if (config.USE_TRELLO) {
console.log("Activating trello integration");
var TrelloBot = require('./trello.js')
,bot = new TrelloBot({
pollFrequency: 1000*60*1 //every minute
,start: true
,trello: {
boards: config.TRELLO_BOARDS
,key: config.TRELLO_KEY
,token: config.TRELLO_TOKEN
,events: ['createCard','commentCard','addAttachmentToCard','updateCard','updateCheckItemStateOnCard']
}
,discord: FGEBot
});
};