· 6 years ago · Nov 27, 2019, 04:28 AM
1const sql = require('sqlite');
2const path = require('path');
3sql.open(path.join(__dirname, 'credits.sql')) // read sql file
4.then(() => { // then ?
5 console.log('Opened') // if the sql opened
6 sql.run(`CREATE TABLE IF NOT EXISTS creditSysteme (id VARCHAR(30), credits BIGINT, timeDaily BIGINT)`) // create new table if the table does'nt exosts
7})
8.catch(err => console.error(err)) // if the sql file does'nt exists
9
10const ms = require('parse-ms'); // package time ?
11client.on("message", async msg => { // event message
12 if(!msg.channel.guild) return; // channel guild
13 let men = msg.mentions.users.first() || msg.author; // the mention or the author
14 let prize = msg.content.split(" ").slice(2).join(" ") // prize
15
16 if(msg.content.startsWith(prefix+"credits")) { // if the message content credits do
17 if(!men || !men === undefined) return msg.channel.send("** :interrobang: | "+men.username+", I can't find "+men.username+"!**"); // undefind user
18 if(!prize) {
19 sql.get(`SELECT * FROM creditSysteme WHERE id = '${men.id}'`).then(res => { // select user from table
20 if(!res) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if the user does'nt exisit in table
21 if(res) { // if user exsist
22 msg.channel.send("**"+men.username+" :credit_card: balance is ``"+res.credits+"$``.**") // reply
23 }
24 })
25 }else{ // else ?
26 if(isNaN(prize)) return msg.channel.send(" :interrobang: | "+msg.author.username+", type the credit you need to transfer!"); // is nan :)
27 if(parseFloat(prize) === NaN) return msg.channel.send(" :interrobang: | "+msg.author.username+", type the credit you need to transfer!"); // if nan :))
28 if(men === msg.author) return; // if the men = author
29 let authorRes = await sql.get(`SELECT * FROM creditSysteme WHERE id = '${msg.author.id}'`) // select from sql
30 let userRes = await sql.get(`SELECT * FROM creditSysteme WHERE id = '${men.id}'`) // select from sql
31 if(!authorRes) sql.run(`INSERT INTO creditSysteme VALUES ('${msg.author.id}', 0, 0)`) // if !user create new col
32 if(!userRes) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if !user create new col
33 let authorCredits = authorRes.credits; // credits before transfer
34 let userCredits = userRes.credits; // credits before transfer
35 if(parseFloat(prize) > authorCredits) return msg.channel.send("** :thinking: | "+msg.author.username+", Your balance is not enough for that!**"); // if the balance hight then prize
36 sql.run(`UPDATE creditSysteme SET credits = ${authorCredits - parseInt(prize)} WHERE id = '${msg.author.id}'`); // uptade credits for the author
37 sql.run(`UPDATE creditSysteme SET credits = ${userCredits + parseInt(prize)} WHERE id = '${men.id}'`); // update credits for the mentions user
38 msg.channel.send("**:moneybag: | "+msg.author.username+", has transferred ``$"+prize+"`` to "+men.toString()+"**") // the message :)
39 }
40 } else if(msg.content.startsWith(prefix+"daily")) { // if the message content daily do
41 let daily = 86400000; // 24h
42 let amount = Math.floor((Math.random() * 500) + 1) // Money
43 let res = await sql.get(`SELECT * FROM creditSysteme WHERE id = '${msg.author.id}'`) // select from sql
44 if(!res) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if !user create new col
45 let time = res.timeDaily; // select last daily
46 let credits = res.credits; // credits before daily
47 if(time != null && daily - (Date.now() - time) > 0) { // if already climed the daily in same day
48
49 let fr8 = ms(daily - (Date.now() - time)); // the remining time
50 msg.channel.send("**:stopwatch: | "+msg.author.username+", your daily :yen: credits refreshes in "+fr8.hours+" hours and "+fr8.seconds+" seconds. **") //reply
51
52 }else{ // if does'nt clim her daily in 24h
53 msg.channel.send("**:atm: | "+msg.author.username+", you received your :yen: "+amount+" daily credits!**"); // reply
54 sql.run(`UPDATE creditSysteme SET credits = ${credits + amount}, timeDaily = ${Date.now()} WHERE id = '${msg.author.id}'`); // add amount to the credits before daily
55 }
56 }
57})
58//58 line :) -