-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
165 lines (153 loc) · 6 KB
/
bot.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
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
var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
//const Promise = require('bluebird');
const AppDAO = require('./dao');
const CommandRepository = require('./command_repository');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
colorize: true
});
logger.level = 'debug';
const dao = new AppDAO('./database.sqlite3');
const nightBotData = { name: 'Test', value: 'test passed!' };
const commandRepo = new CommandRepository(dao);
commandRepo.createTable()
.then(() => commandRepo.create(nightBotData.name, nightBotData.value))
.then((data) => {
console.log("created command with id: " + data.id);
})
.then(() => commandRepo.getByName(nightBotData.name))
.then((command) => {
console.log(`\nRetrieved command from database`);
console.log(`command name = ${command.name}`);
console.log(`command value = ${command.value}`);
})
.catch((err) => {
console.log('Error: ');
console.log(JSON.stringify(err));
});
let intervals = [];
function setPeriodicCommand(command) {
return setInterval(function() {
bot.sendMessage({
to: '650397815427563580',
message: command.value
});
}, 3600000);
}
function initPeriodic() {
commandRepo.getAll().then(function (commands) {
console.log("all commands", commands);
let periodicCommands = commands.filter(command => command.periodic === 1);
console.log("periodicCommands", periodicCommands);
for (let i = 0; i < periodicCommands.length; i++) {
intervals.push(setPeriodicCommand(periodicCommands[i]));
}
}).catch(function (err) {
console.log('Error retrieving all commands!: ');
console.log(JSON.stringify(err));
});
}
// 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 + ')');
initPeriodic();
});
bot.on('message', function (user, userID, channelID, message, evt) {
// Our bot needs to know if it will execute a command
// It will listen for messages that will start with `!`
if (message.substring(0, 1) == '!') {
var args = message.substring(1).split('`').splice(1);
var cmd = message.substring(1).split(' ')[0];
var commandName = args[0];
var commandValue = args[2];
commandRepo.getByName(cmd).then(function (command) {
bot.sendMessage({
to: channelID,
message: command.value
});
}).catch(function (err) {
console.log('Command not found!: ');
console.log(JSON.stringify(err));
});
switch(cmd) {
// !ping
case 'ping':
bot.sendMessage({
to: channelID,
message: 'Pong!'
});
break;
case 'newcommand':
commandRepo.create(commandName, commandValue).then((data) => {
console.log("Created command with id: " + data.id);
bot.sendMessage({
to: channelID,
message: 'Command ' + commandName + ' created.'
});
}).catch(function (err) {
console.log("Error when creating the command, does it already exist?");
bot.sendMessage({
to: channelID,
message: 'Command ' + commandName + ' could not be created. Does it already exist?'
});
});
break;
case 'newcommandperiodic':
commandRepo.createPeriodic(commandName, commandValue).then((data) => {
console.log("Created periodic command with id: " + data.id);
bot.sendMessage({
to: channelID,
message: 'Periodic command ' + commandName + ' created.'
});
}).catch(function (err) {
console.log("Error when creating the periodic command, does it already exist?");
bot.sendMessage({
to: channelID,
message: 'Periodic command ' + commandName + ' could not be created. Does it already exist?'
});
});
break;
case 'deletecommand':
commandRepo.delete(commandName).then((data) => {
console.log("Deleted command with id: " + data.id);
bot.sendMessage({
to: channelID,
message: 'Command ' + commandName + ' deleted.'
});
}).catch(function (err) {
console.log("Error when deleting the command, does it exist?");
bot.sendMessage({
to: channelID,
message: 'Command ' + commandName + ' could not be deleted. Does it exist?'
});
});
break;
case 'updatecommand':
let command = {commandName, commandValue};
commandRepo.update(command).then((data) => {
console.log("Updated command with id: " + data.id);
bot.sendMessage({
to: channelID,
message: 'Command ' + commandName + ' updated.'
});
}).catch(function (err) {
console.log("Error when updating the command, does it exist?");
bot.sendMessage({
to: channelID,
message: 'Command ' + commandName + ' could not be updated. Does it exist?'
});
});
break;
// Just add any case commands if you want to..
}
}
});