forked from roj42/nnnslackbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
134 lines (108 loc) · 3.44 KB
/
main.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
// Author: Netherfore
// Discord
const Discord = require("discord.io");
const logger = require("winston");
const auth = require("./auth.json");
const help = require("./help");
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console(), {
colorize: true,
});
logger.level = "debug";
// 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 + ")");
});
/**
* main should remain fairly logic-sparse as far as different behaviors.
* the interface exposed by each module should be the following:
*
* key - primary command for calling the module
* aliases - alternative keys that also call the module (optional)
* help - function for handling help messages about the module
* directMessages - function for calling the module when a message calling it comes in.
* ambientMessages - function for handling messages not directed at the module.
*
* TODO:
* - message type configuration? Not sure this is necessary to be honest, but including as it was prior functionality.
*/
const modules = ["ping", "help", "sass", "catFacts"].map((folder) => {
logger.info(`Loading ${folder} module`);
try {
return require(`./${folder}`);
} catch (e) {
logger.error(`Failure loading ${folder} module. ${e}`);
}
});
logger.info("Setting commands");
const commandMappings = modules.reduce(
(mapping, { key, aliases, directMessages }) => {
// skip over modules that do not have a message handler
if (!directMessages) return mapping;
if (key) {
logger.info(`Adding ${key}`);
mapping[key] = directMessages;
}
if (aliases) {
aliases.forEach((alias) => {
logger.info(`Adding ${alias}`);
mapping[alias] = directMessages;
});
}
return mapping;
},
{}
);
logger.info("Completed commands");
logger.info("Adding help");
const helpMappings = modules.reduce((mapping, { key, aliases, help }) => {
// skip over modules that do not have a message handler
if (!help) return mapping;
if (key) {
logger.info(`Adding help for ${key}`);
mapping[key] = help;
}
if (aliases) {
aliases.forEach((alias) => {
logger.info(`Adding help for ${alias}`);
mapping[alias] = help;
});
}
return mapping;
}, {});
help.setMappings(helpMappings);
logger.info("Adding ambient behaviors");
const ambient = modules.reduce((list, { key, ambientMessages }) => {
if (!ambientMessages) return list;
logger.info(`Adding ambient behavior for ${key}`);
list.push(ambientMessages);
return list;
}, []);
logger.info("Module loading complete");
/** Main message handling function.
* Takes incoming messages and routes them to the corresponding modules/ambient handlers
*/
bot.on("message", function (user, userID, channelID, message, evt) {
try {
if (message.substring(0, 1) === "$") {
const [cmd, ...args] = message.substring(1).split(" ");
if (commandMappings[cmd]) {
commandMappings[cmd](bot, { user, userID, channelID, message, evt });
}
} else {
// might need to restructure down the line so if there's ambient behavior, he's throttled to not do a bunch of other ones.
ambient.forEach((module) =>
module(bot, { user, userID, channelID, message, evt })
);
}
} catch (e) {
logger.error(`${e.stack}`);
}
});