-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
386 lines (319 loc) · 14.2 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
const fs = require('fs');
const path = require('path');
const Numeral = require('numeral');
const sqlite3 = require('sqlite3');
const Discord = require("discord.js");
const appRoot = require('app-root-path');
const Handlebars = require("handlebars");
const pools = require("./handlers/pools.js");
const users = require("./handlers/users.js");
const rains = require("./handlers/rains.js");
const UsersData = require("./modules/users.js");
const markets = require("./handlers/markets.js");
const marketsData = require("./modules/markets.js");
const wallets = require("./handlers/wallets.js");
const settings = require("./handlers/settings.js");
const exchanges = require("./handlers/exchanges.js");
const giveaways = require("./handlers/giveaways.js");
const blockchain = require("./handlers/blockchain.js");
const WalletsData = require("./modules/wallets.js");
const SettingsData = require("./modules/settings.js");
const GiveawaysData = require("./modules/giveaways.js");
const BlockchainData = require("./modules/blockchain.js");
const HandlebarHelpers = require('just-handlebars-helpers');
// Here we load the config.json file that contains our token and our prefix values.
const config = require("./config.json");
// open the access to the database if it fails we must stop
let db = new sqlite3.Database(path.join(appRoot.path, config.db.filename), sqlite3.OPEN_READWRITE, (err) => {
if (err) {
console.error('Could not connect to database', err);
}
});
// This is your client. Some people call it `bot`, some people call it `self`,
// some might call it `cootchie`. Either way, when you see `client.something`, or `bot.something`,
// this is what we're refering to. Your client.
const client = new Discord.Client({
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
intents: Object.keys(Discord.GatewayIntentBits).map((a)=>{
return Discord.GatewayIntentBits[a]
})
});
// initialize data models
const usersData = new UsersData(db);
const walletsData = new WalletsData(db);
const settingsData = new SettingsData(db);
const blockchainData = new BlockchainData();
const giveawaysData = new GiveawaysData(db);
if (config.debug) {
console.log("initialized data models...");
}
// register the handlebar helpers
HandlebarHelpers.registerHelpers(Handlebars);
// handle all unhandler exceptions
process.on('uncaughtException', err => {
console.error('There was an uncaught error', err)
process.exit(1);
});
client.on("ready", () => {
if (config.debug) {
console.log("on client ready called...");
}
// set the avatar for the bot user
client.user.setAvatar('./avatar.png').then(user => console.log(`Avatar is set!`)).catch(err => { console.error('Set avatar error', err) });
// This event will run if the bot starts, and logs in, successfully.
console.log(`Bot has started, with ${client.users.cache.size} users, in ${client.channels.cache.size} channels of ${client.guilds.cache.size} guilds.`);
// Example of changing the bot's playing game to something useful. `client.user` is what the
// docs refer to as the "ClientUser".
client.user.setActivity(`Serving ${client.guilds.size} servers`);
// we are ready so we can initialize the giveaways
giveawaysData.initialize(function (data) {
let channel = client.channels.cache.get(config.giveaways.channel);
if (channel) {
channel.messages.fetch(data.message_id).then(message => {
message.reactions.cache.forEach(reaction => {
if (reaction.emoji.identifier == "%F0%9F%8E%89") {
reaction.users.fetch().then(usersList => {
// exclude all bots from the list of users
let gaUsers = usersList.filter(user => !user.bot);
// call the handler for finishing the giveaway
giveaways.finishGiveaway(giveawaysData, walletsData, settingsData, client, message, gaUsers.array());
}).catch(err => {
console.error('Failed to fetch reaction users', err);
});
}
});
}).catch(err => {
console.error('Giveaway finished error', err);
});
}
});
});
client.on("guildCreate", guild => {
// This event triggers when the bot joins a guild.
console.log(`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`);
client.user.setActivity(`Serving ${client.guilds.size} servers`);
});
client.on('guildMemberAdd', member => {
marketsData.getExchanges().then(data => {
fs.readFile('./templates/welcome.msg', 'utf8', function (err, source) {
if (err) throw err;
let welcomeTpl = Handlebars.compile(source);
let welcomeEmbed = {
color: 0x0099ff,
title: "Welcome to Conceal",
url: 'https://discord.js.org',
author: {
name: 'Conceal Network',
icon_url: 'https://conceal.network/images/branding/logo.png',
url: 'https://discord.gg/YbpHVSd'
},
description: welcomeTpl({ username: member.displayName }),
timestamp: new Date(),
footer: {
text: "Conceal Network",
icon_url: 'https://conceal.network/images/branding/logo.png'
}
};
member.send({ embed: welcomeEmbed });
});
}).catch(err => {
console.error('Failed to get exchangers', err);
});
});
client.on("guildDelete", guild => {
// this event triggers when the bot is removed from a guild.
console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`);
client.user.setActivity(`Serving ${client.guilds.size} servers`);
});
client.on('messageReactionAdd', (reaction, user) => {
// nothing to do so far
});
client.on("messageCreate", async message => {
// This event will run on every single message received, from any channel or DM.
// It's good practice to ignore other bots. This also makes your bot ignore itself
// and not get into a spam loop (we call that "botception").
if (message.author.bot) return;
//if (message.channel.type == "dm") {
// return message.channel.send("Bot is not available in DM");
//}
if (message && message.author) {
// update the activity for the current user
usersData.updateUserActivity(message.author.id).catch(err => {
console.error('Error trying to log user activity', err);
});
}
// Also good practice to ignore any message that does not start with our prefix,
// which is set in the configuration file.
if (message.content.indexOf(config.prefix) !== 0) return;
// log every command
if (config.debug) {
console.log(message.content);
}
// Here we separate our "command" name, and our "arguments" for the command.
// e.g. if we have the message "+say Is this the real life?" , we'll get the following:
// command = say
// args = ["Is", "this", "the", "real", "life?"]
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === "exchanges") {
if (args.length == 0) {
return message.reply(`You need to specify a exchanges command! Type: ***${config.prefix}exchanges help*** for list of commands`);
}
// execute the exchanges commands
return exchanges.executeCommand(message, command, args);
}
if (command === "markets") {
if (args.length == 0) {
return message.reply(`You need to specify a markets command! Type: ***${config.prefix}markets help*** for list of commands`);
}
// execute the markets commands
return markets.executeCommand(message, command, args);
}
if (command === "blockchain" || command === "chain") {
if (args.length == 0) {
return message.reply(`You need to specify a blockchain command! Type: ***${config.prefix}blockchain help*** for list of commands`);
}
// execute the blockchain commands
return blockchain.executeCommand(blockchainData, message, command, args);
}
if (command === "wallet") {
if (args.length == 0) {
return message.reply(`You need to specify a wallet command! Type: ***${config.prefix}wallet help*** for list of commands`);
}
// execute the blockchain commands
return wallets.executeCommand(walletsData, message, command, args);
}
if (command === "pools") {
if (args.length == 0) {
return message.reply(`You need to specify a pool command! Type: ***${config.prefix}pools help*** for list of commands`);
}
// execute the blockchain commands
return pools.executeCommand(message, command, args);
}
if (command === "giveaway") {
if (args.length == 0) {
return message.reply(`You need to specify a giveaway command! Type: ***${config.prefix}giveaway help*** for list of commands`);
}
// execute the blockchain commands
return giveaways.executeCommand(giveawaysData, walletsData, client, message, command, args);
}
if (command === "users") {
if (args.length == 0) {
return message.reply(`You need to specify a users command! Type: ***${config.prefix}users help*** for list of commands`);
}
// execute the blockchain commands
return users.executeCommand(usersData, client, message, command, args);
}
if (command === "settings") {
if (args.length == 0) {
return message.reply(`You need to specify a settings command! Type: ***${config.prefix}settings help*** for list of commands`);
}
// execute the blockchain commands
return settings.executeCommand(settingsData, message, command, args);
}
/************************************************************
* Tip command. Take ammount specified and tip the target *
* user. User needs to have a registered wallet otherwise *
* it fails. Also checks the balance first. *
***********************************************************/
if (command === "tip") {
let payments = [];
let amount = 0;
if (args.length < 2) {
return message.reply('You need to specify an ammount and a recipient.');
}
if (!message.mentions.users.first()) {
return message.reply('You need to specify at least one recipient.');
}
if (message.author.id === message.mentions.users.first().id) {
return message.reply('You cannot tip yourself silly \:rofl:');
}
try {
amount = parseFloat(args[0]);
if (!amount || amount < config.restrictions.minTipAmount) throw `Amount cannot be less then ${config.restrictions.minTipAmount} CCX`;
} catch (err) {
return message.reply(err);
}
// calculate the pay part per every mentioned user
let payPart = ((amount - 0.001) / message.mentions.users.size);
message.mentions.users.forEach(user => {
payments.push({ userId: user.id, amount: payPart });
});
// execute the blockchain commands
return walletsData.sendPayments(message.author.id, payments).then(data => {
message.author.send(`Success! ***TX hash***: ${data.transactionHash}\n***Secret key***: ${data.transactionSecretKey}`);
message.channel.send(`\:money_with_wings: Success`);
}).catch(err => {
message.author.send(err);
message.channel.send(`\:x: Failed`);
}).finally(message.reply('The tip details were send to you in DM'));
}
/************************************************************
* Rain command. Take ammount specified and number of user *
* It then takes latest active users and proportionally *
* rains the CCX specified over all of them *
***********************************************************/
if (command === "rain") {
if (args.length == 0) {
return message.reply(`You need to specify a rain command! Type: ***${config.prefix}rain help*** for list of commands`);
}
// execute the rain commands
return rains.executeCommand(usersData, walletsData, settingsData, client, message, command, args);
}
/************************************************************
* General help command. Prints the general help out. *
***********************************************************/
if (command === "help") {
function sendNextHelpPart(partNum) {
fs.readFile(`./templates/help_general_${partNum}.msg`, 'utf8', function (err, source) {
if (err) throw err;
message.author.send(source);
});
if (partNum < 3) {
setTimeout(() => {
sendNextHelpPart(partNum + 1)
}, 200);
}
}
// warn that help is sent id DM
if (message.channel.type !== "dm") {
message.reply('The help information has been sent to you in DM. Consider using DM with the bot for help commands.');
}
// send first part and return
sendNextHelpPart(1);
return true;
}
if (command === "say") {
// makes the bot say something and delete the message. As an example, it's open to anyone to use.
// To get the "message" itself we join the `args` back into a string with spaces:
const sayMessage = args.join(" ");
// Then we delete the command message (sneaky, right?). The catch just ignores the error with a cute smiley thing.
message.delete().catch(O_o => { });
// And we get the bot to say the thing:
return message.channel.send(sayMessage);
}
if (command === "purge") {
if (message.channel.type == "dm") {
return message.channel.send("Purge command is not available in DM");
}
if (message.member && message.member.roles) {
// This command removes all messages from all users in the channel, up to 100.
if (!message.member.roles.some(r => ["admins", "mods"].includes(r.name)))
return message.reply("Sorry, you don't have permissions to use this!");
// get the delete count, as an actual number.
const deleteCount = parseInt(args[0], 10) + 1;
// Ooooh nice, combined conditions. <3
if (!deleteCount || deleteCount < 1 || deleteCount > 100)
return message.reply("Please provide a number between 1 and 100 for the number of messages to delete");
// So we get our messages, and delete them. Simple enough, right?
const fetched = await message.channel.fetchMessages({ limit: deleteCount });
return message.channel.bulkDelete(fetched).catch(error => message.reply(`Couldn't delete messages because of: ${error}`));
}
}
// if we came this far then no command was found
return message.reply('Unknown command. Please type ".help" for more info on how to use the bot');
});
if (config.debug) {
console.log("calling client.login...");
}
client.login(config.token);