· 6 years ago · Oct 21, 2019, 04:46 PM
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
10let ammount = (300, 500, 100, 200, 120, 150, 350, 320, 220, 250);
11
12client.on("message", async msg => { // event message
13 if(!msg.channel.guild) return; // channel guild
14 let men = msg.mentions.users.first() || msg.author; // the mention or the author
15 let prize = msg.content.split(" ").slice(2).join(" ") // prize
16
17 if(msg.content.startsWith(prefix+"credits")) { // if the message content credits do
18 if(!men || !men === undefined) return msg.channel.send("** :interrobang: | "+men.username+", I can't find "+men.username+"!**"); // undefind user
19 if(!prize) {
20 sql.get(`SELECT * FROM creditSysteme WHERE id = '${men.id}'`).then(res => { // select user from table
21 if(!res) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if the user does'nt exisit in table
22 if(res) { // if user exsist
23 msg.channel.send("**"+men.username+" :credit_card: balance is ``"+res.credits+"$``.**") // reply
24 }
25 })
26 }else{ // else ?
27 if(isNaN(prize)) return msg.channel.send(" :interrobang: | "+msg.author.username+", type the credit you need to transfer!"); // is nan :)
28 if(parseFloat(prize) === NaN) return msg.channel.send(" :interrobang: | "+msg.author.username+", type the credit you need to transfer!"); // if nan :))
29 if(men === msg.author) return; // if the men = author
30 let authorRes = await sql.get(`SELECT * FROM creditSysteme WHERE id = '${msg.author.id}'`) // select from sql
31 let userRes = await sql.get(`SELECT * FROM creditSysteme WHERE id = '${men.id}'`) // select from sql
32 if(!authorRes) sql.run(`INSERT INTO creditSysteme VALUES ('${msg.author.id}', 0, 0)`) // if !user create new col
33 if(!userRes) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if !user create new col
34 let authorCredits = authorRes.credits; // credits before transfer
35 let userCredits = userRes.credits; // credits before transfer
36 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
37 sql.run(`UPDATE creditSysteme SET credits = ${authorCredits - parseInt(prize)} WHERE id = '${msg.author.id}'`); // uptade credits for the author
38 sql.run(`UPDATE creditSysteme SET credits = ${userCredits + parseInt(prize)} WHERE id = '${men.id}'`); // update credits for the mentions user
39 msg.channel.send("**:moneybag: | "+msg.author.username+", has transferred ``$"+prize+"`` to "+men.toString()+"**") // the message :)
40 }
41 } else if(msg.content.startsWith(prefix+"daily")) { // if the message content daily do
42 let daily = 86400000; // 24h
43 let amount = Math.floor((Math.random() * 1000) + 1) // Money
44 let res = await sql.get(`SELECT * FROM creditSysteme WHERE id = '${msg.author.id}'`) // select from sql
45 if(!res) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if !user create new col
46 let time = res.timeDaily; // select last daily
47 let credits = res.credits; // credits before daily
48 if(time != null && daily - (Date.now() - time) > 0) { // if already climed the daily in same day
49fs.writeFile("./time.json", JSON.stringify(time), function(e) {
50if(e)throw e;
51})
52 let fr8 = ms(daily - (Date.now() - time)); // the remining time
53 msg.channel.send("**:stopwatch: | "+msg.author.username+", your daily :yen: credits refreshes in "+fr8.hours+" hours and "+fr8.seconds+" seconds. **") //reply
54
55 }else{ // if does'nt clim her daily in 24h
56 msg.channel.send("**:atm: | "+msg.author.username+", you received your :yen: "+amount+" daily credits!**"); // reply
57 sql.run(`UPDATE creditSysteme SET credits = ${credits + amount}, timeDaily = ${Date.now()} WHERE id = '${msg.author.id}'`); // add amount to the credits before daily
58 }
59 }
60})