This repository has been archived by the owner on Jan 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord-support.js
104 lines (92 loc) · 2.88 KB
/
discord-support.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
const chalk = require('chalk');
exports.discordLogin = Kirbi => {
const Discord = require('discord.js');
Kirbi.Discord = new Discord.Client();
console.log(chalk.magenta(`Discord Enabled... Starting.\nDiscord.js version: ${Discord.version}`));
if (Kirbi.Auth.discord.bot_token) {
console.log('Logging in to Discord...');
Kirbi.Discord.login(Kirbi.Auth.discord.bot_token);
require('./lib/on-event')(Kirbi);
} else {
console.log(chalk.red('ERROR: Kirbi must have a Discord bot token...'));
return;
}
Kirbi.addDiscordCommand = (commandName, commandObject) => {
try {
Kirbi.DiscordCommands[commandName] = commandObject;
} catch (err) {
console.log(err);
}
};
Kirbi.discordCommandCount = () => {
return Object.keys(Kirbi.DiscordCommands).length;
};
Kirbi.setupDiscordCommands = () => {
Kirbi.DiscordCommands = {};
// Load more complex response commands from markdown files
let markdownCommands = [];
try {
markdownCommands = Kirbi.getJsonObject('/config/markdown-commands.json');
} catch (err) {
console.log(chalk.red(err));
}
markdownCommands.forEach(markdownCommand => {
const command = markdownCommand.command;
const description = markdownCommand.description;
const deleteRequest = markdownCommand.deleteRequest;
const channelRestriction = markdownCommand.channelRestriction;
const file = markdownCommand.file;
const messagesToSend = Kirbi.getFileContents(file);
if (messagesToSend) {
Kirbi.addDiscordCommand(command, {
description,
process: (msg, suffix, isEdit, cb) => {
if (channelRestriction === undefined || (channelRestriction && msg.channel.id === channelRestriction)) {
if (deleteRequest) {
msg.delete();
}
const messages = messagesToSend.split('=====');
messages.forEach(message => {
message = message.split('-----');
cb({
embed: {
color: Kirbi.Config.discord.defaultEmbedColor,
title: message[0].trim(),
description: message[1].trim()
}
}, msg);
});
}
}
});
}
});
// Load external discord-specific modules
if (Kirbi.Config.discord.modules.length > 0 && Array.isArray(Kirbi.Config.discord.modules)) {
Kirbi.Config.discord.modules.forEach(module => {
if (Kirbi.DiscordCommands[module]) {
return;
}
try {
module = require(`kirbi-discord-${module}`)(Kirbi);
} catch (err) {
console.log(chalk.red(`Improper setup of the 'discord-${module}' command file. : ${err}`));
return;
}
if (module && module.commands) {
module.commands.forEach(command => {
if (command in module) {
try {
Kirbi.DiscordCommands[command] = module[command];
} catch (err) {
console.log(err);
}
}
});
}
});
}
};
Kirbi.setupDiscordCommands();
console.log(`Loaded ${Kirbi.discordCommandCount()} Discord commands`);
};