-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotio.js
81 lines (75 loc) · 1.85 KB
/
botio.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
//requires
var Discord = require('discord.io');
var auth = require('./auth.json');
var Cases = require('./models/botcase.js');
// Configure logger settings
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, prettyPrint } = format;
const logger = createLogger({
format: combine(
timestamp(),
prettyPrint()
),
transports: [
new transports.Console(), //show logs in console
new transports.File({ // save logs to file
json: true,
level:'debug',
maxsize: 1024 * 1024, // 1M per log
filename: 'logs/combined.log', // log file path
}),
],
});
// Initialize Discord Bot
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
// bot ready
bot.on('ready', function (evt) {
logger.info('Connected');
logger.info('Logged in as: ');
logger.info(bot.username + ' - (' + bot.id + ')');
logger.info("----------");
});
// message listener
bot.on('message', function (user, userID, channelID, message, evt) {
if (message.substring(0, 2) == 'd!') {
//logs
logger.info(user + " - " + userID + " in " + channelID);
logger.info(message);
// bot replay in different cases
const commands = splitMessage(message);
if (commands) {
Cases[commands.cmd](user, userID, channelID, commands.args, function (content) {
bot.sendMessage({
to: channelID,
embed: content
});
});
} else {
bot.sendMessage({
to: channelID,
embed: {
"color": 12345678,
"fields": [{
"name": "Unknown command!",
"value": "Try d!help for command list!"
}]
}
});
}
}
});//end of message on
function splitMessage(message) {
//split message
const msg = message.substring(2).split(' ');
const cmd = msg[0];
const args = msg.splice(1);
// check the command exists
if (typeof Cases[cmd] === "function") {
return { cmd, args };
} else {
return null;
}
}