· 6 years ago · Sep 22, 2019, 01:30 AM
1const sql = require('sqlite');
2const path = require('path');
3sql.open(path.join(__dirname, 'credits.sql')) // read sql file
4.then(() => { // tstev
5 console.log('Opened') // stev
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
10client.on("message", async msg => { // stev
11 if(!msg.channel.guild) return; // stev
12 let men = msg.mentions.users.first() || msg.author; // the mention or the author
13 let prize = msg.content.split(" ").slice(2).join(" ") // prize
14
15 if(msg.content.startsWith("$رصيدي")) { // if the message content credits do
16 if(!men || !men === undefined) return msg.channel.send("** :interrobang: | "+men.username+", I can't find "+men.username+"!**"); // undefind user
17 if(!prize) {
18 sql.get(`SELECT * FROM creditSysteme WHERE id = '${men.id}'`).then(res => { // select user from table
19 if(!res) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if the user does'nt exisit in table
20 if(res) { // if user exsist
21 msg.channel.send("**"+men.username+" :credit_card: balance is ``"+res.credits+"$``.**") // reply
22 }
23 })
24 }else{ // else ?
25 if(isNaN(prize)) return msg.channel.send(" :interrobang: | "+msg.author.username+", type the credit you need to transfer!"); // is nan :)
26 if(isNaN(args[2]) || args[2] < 0) return message.channel.send(' هذه الخانة يجب ان تتكون من ارقام صحيحة وليس احرف.');
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("$راتب")) { // 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{ // stev
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})