-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
77 lines (62 loc) · 2.31 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const Botkit = require('botkit');
const Discord = require('discord.js');
const discordEvents = require('./discord-events');
const middleware = require('./middleware');
const botDefinition = require('./bot');
const newMessageHandler = (message, controller) => {
const bot = controller.spawn({});
const source = {
raw: message
}
controller.ingest(bot, {}, source);
};
const DiscordBot = (configuration) => {
const client = new Discord.Client({});
configuration.client = client;
configuration.replyToSelf = configuration.replyToSelf || false;
const discordBotkit = Botkit.core(configuration || {});
discordBotkit.defineBot(botDefinition);
// Pass along classes
discordBotkit.RichEmbed = Discord.RichEmbed;
discordBotkit.Attachment = Discord.Attachment;
// Attach Handlers and Middlewares
discordBotkit.handleMessageReceive = newMessageHandler;
discordBotkit.middleware.ingest.use(middleware.ingest.handler);
discordBotkit.middleware.normalize.use(middleware.normalize.handler);
discordBotkit.middleware.categorize.use(middleware.categorize.handler);
discordBotkit.middleware.format.use(middleware.format.handler);
discordBotkit.middleware.receive.use(middleware.receive.handler);
client.on('ready', () => {
discordBotkit.trigger('ready', [discordBotkit, client.user])
discordBotkit.log('Logged in as %s - %s\n', client.user.username, client.user.id);
});
client.on('message', async message => {
discordBotkit.debug(`Received ${message}`);
// Bot replies to itself with the same message
// Can allow this by enabling it in the configuration
if (
message.author.id === configuration.client.user.id &&
configuration.replyToSelf === false) {
discordBotkit.debug(`Message received from bot, set configuration.replyToSelf to true to allow processing`);
return;
}
discordBotkit.handleMessageReceive(message, discordBotkit);
});
// Set up triggers for remaining events
discordEvents.map(event => {
client.on(event, (...params) => {
discordBotkit.trigger(event, [discordBotkit, params]);
});
});
if (configuration.debug) {
client.on('debug', info => {
discordBotkit.debug(info);
discordBotkit.trigger('debug', [discordBotkit, info]);
});
}
// Stay Alive Please
client.login(configuration.token);
discordBotkit.startTicking();
return discordBotkit
};
module.exports = DiscordBot;