-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (176 loc) · 7.43 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const Discord = require('discord.js');
const fs = require('fs');
const env = require('dotenv').config();
const path = require('path');
const dbFileName = process.env.TD_DB ? process.env.TD_DB : path.join(__dirname, 'db', 'todos.json');
const prefix = process.env.TD_PREFIX ? process.env.TD_PREFIX : 'todos';
const color = process.env.TD_COLOR ? process.env.TD_COLOR : '#0099ff';
const token = process.env.TD_AUTH_TOKEN;
const username = process.env.TD_USERNAME ? process.env.TD_USERNAME : 'Todo Bot';
const messageFormatFileName = path.join(__dirname, 'messages.json');
let messageFormats;
let client;
function loadMessageFormats() {
return new Promise(resolve => {
fs.readFile(messageFormatFileName, 'utf-8', (err, data) => {
if (!err && data) {
messageFormats = JSON.parse(data);
return resolve();
}
return reject("Message formats file is missing!");
});
});
}
loadMessageFormats()
.then(() => {
if (!token) {
throw new Error(messageFormats.errors.botAuthTokenRequired);
}
const check = require("check-node-version");
check({ node: "<12", }, (error, result) => {
if (error) {
console.error(error);
return;
}
if (result.isSatisfied) {
require('array-flat-polyfill');
}
return readFile().then(start);
});
})
.catch(e => {
console.error(e);
});
function readFile() {
return new Promise(resolve => {
fs.readFile(dbFileName, 'utf-8', (err, data) => {
if (!err && data) {
return resolve(JSON.parse(data));
}
return resolve({});
});
});
}
function writeFile(todos) {
return new Promise(resolve => {
fs.writeFile(dbFileName, JSON.stringify(todos), (err) => {
if (err) {
console.error(err);
return resolve(false);
}
return resolve(true);
});
});
}
function start(todos) {
try {
const helpEmbed = new Discord.MessageEmbed()
.setColor(color)
.setTitle(messageFormats.help.title)
.addField(prefix + ' [a]dd <' + messageFormats.help.description +'>', messageFormats.help.add, false)
.addField(prefix + ' [r]emove <' + messageFormats.help.number + '>', messageFormats.help.remove, false)
.addField(prefix + ' [h]elp', messageFormats.help.help, false)
.addField(prefix, messageFormats.help.current, false);
client = new Discord.Client();
client.once('ready', () => {
console.log(messageFormats.console.ready);
});
client.on('guildCreate', guild => {
guild.systemChannel.send(helpEmbed);
});
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length + 1).split(' ');
const command = args.shift().toLowerCase();
if (command) {
let help = false;
if (command === 'add' || command === 'a') {
if (!todos[message.guild.id]) {
todos[message.guild.id] = {};
todos[message.guild.id][message.channel.id] = [];
} else if (!todos[message.guild.id][message.channel.id]) {
todos[message.guild.id][message.channel.id] = [];
}
const newTodo = args.join(' ');
if (newTodo) {
todos[message.guild.id][message.channel.id].push(newTodo);
writeFile(todos);
let respondText = messageFormats.responds.addSuccess;
respondText = respondText.replace('{0}', newTodo);
message.channel.send(respondText);
} else {
help = true;
}
} else if (command === 'remove' || command === 'r') {
if (todos[message.guild.id] && todos[message.guild.id][message.channel.id] && todos[message.guild.id][message.channel.id].length > 0) {
let idx = args.shift();
if (idx) {
idx = Number(idx);
idx--;
if (idx >= 0 && idx<todos[message.guild.id][message.channel.id].length) {
const deleted = todos[message.guild.id][message.channel.id].splice(idx, 1);
writeFile(todos);
let respondText = messageFormats.responds.removeSuccess;
respondText = respondText.replace('{0}', deleted);
message.channel.send(respondText);
} else {
message.channel.send(messageFormats.responds.removeNotFound);
}
} else {
help = true;
}
} else {
message.channel.send(messageFormats.responds.removeEmptyList);
}
} else {
help = true;
}
if (help) {
message.channel.send({ embed: helpEmbed });
}
} else {
if (todos[message.guild.id] && todos[message.guild.id][message.channel.id] && todos[message.guild.id][message.channel.id].length > 0) {
let descriptions = [];
let description = '';
for (let i=0; i<todos[message.guild.id][message.channel.id].length; i++) {
let newLine = messageFormats.responds.descriptionLine + '\n';
newLine = newLine.replace('{0}', (i+1)).replace('{1}', todos[message.guild.id][message.channel.id][i]);
if (description.length + newLine.length > 2048) {
descriptions.push(description);
description = newLine;
} else {
description += newLine;
}
}
descriptions.push(description);
for (let i=0; i<descriptions.length; i++) {
let title = message.channel.name;
title = descriptions.length > 1 ? title + '(' + (i+1) + '/' + descriptions.length + ')' : title;
const exampleEmbed = new Discord.MessageEmbed()
.setColor(color)
.setTitle(title)
.setDescription(descriptions[i]);
message.channel.send({ embed: exampleEmbed });
}
} else {
message.channel.send(messageFormats.responds.showEmptyList);
}
}
});
client.login(token);
} catch (e) {
console.error(e);
}
}
function end() {
if (client) {
client.destroy();
client = null;
console.log(messageFormats.console.bye);
}
}
process.on('exit', end);
process.on('SIGINT', end);
process.on('SIGUSR1', end);
process.on('SIGUSR2', end);
process.on('uncaughtException', end);