-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (74 loc) · 2.3 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
/*
created by MasterPtato
https://github.com/masterptato
have fun with it
*/
let Discord = require('discord.js');
let sqlite = require('sqlite');
let util = require('util');
let fs = require('fs');
let moment = require('moment');
let glob = require('./globalVars.js');
let utilities = require('./utilities.js');
// enable console logging
let logging = false;
let client = new Discord.Client();
glob.client = client;
let botToken = "UR TOKEN HERE";
client.login(botToken);
//access the database
glob.database = {};
let database = sqlite.open('./database.sqlite').then(db => {
database = db.driver;
glob.database = database;
});
let commands = require('./commands/commands.js');
client.on('ready', () => {
if(logging) console.log('Bot is ready');
client.user.setActivity('Do raf help for commands!');
});
client.on('message', message => {
if(message.author.bot) return;
if(!message.content.startsWith('raf ')) return;
let args = message.content.replace(/raf +/i, '').split(' ');
let commandName = args[0];
if(commands[commandName]) commands[commandName](message, args.slice(1));
});
// test for completions
setInterval(() => {
utilities.dbasyncall(`select * from raffles`).then(raffles => {
for(let raffle of raffles) {
utilities.endRaffle(raffle);
}
});
}, 3500);
// update "ends in" message
setInterval(() => {
utilities.dbasyncall(`select * from raffles`).then(raffles => {
for(let raffle of raffles) {
if(Date.now() > raffle.endstamp) return;
let guildID = raffle.id.match(/^\d+:/)[0].replace(/:/g, '');
let guild = client.guilds.get( guildID );
if(guild) {
let channelID = raffle.id.match(/:\d+:/)[0].replace(/:/g, '');
let channel = guild.channels.get( channelID );
if(channel) {
let msgID = raffle.id.match(/:\d+$/)[0].replace(/:/g, '');
channel.fetchMessage( msgID ).then(msg => {
if(msg) {
let embed = new Discord.RichEmbed({
title: raffle.name,
description: `React to enter!${raffle.winners != 1 ? `\n*${raffle.winners} winners*`:''}\nEnds in: **${utilities.getTime(raffle.endstamp - Date.now(), true)}**`,
color: 0x3386BF,
footer: {
text: msg.embeds[0].footer.text
}
});
msg.edit(':anger: **NEW RAFFLE** :anger:', {embed: embed});
}
});
}
}
}
});
}, 1000 * 60 * 16.231);