Ayoub-toxic

credits code

Dec 4th, 2019
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require("discord.js");
  2. const client = new Discord.Client();
  3. const sql = require('sqlite');
  4. const path = require('path');
  5. const ms = require('parse-ms');
  6. const prefix = "!";
  7. sql.open(path.join(__dirname, 'credits.sql')) // read sql file
  8. .then(() => { // then ?
  9.     console.log('Opened') // if the sql opened
  10.     sql.run(`CREATE TABLE IF NOT EXISTS creditSysteme (id VARCHAR(30), credits BIGINT, timeDaily BIGINT)`) // create new table if the table does'nt exosts
  11. })
  12. .catch(err => console.error(err)) // if the sql file does'nt exists
  13. client.on("message", async msg => { // event message
  14.     if(!msg.channel.guild) return; // channel guild
  15.     let men = msg.mentions.users.first() || msg.author; // the mention or the author
  16.     let prize =  msg.content.split(" ").slice(2).join(" ") // prize
  17.  
  18.     if(msg.content.startsWith(prefix+"credits")) { // if the message content credits do
  19.         if(!men || !men === undefined) return msg.channel.send("** :interrobang: | "+men.username+", I can't find "+men.username+"!**"); // undefind user
  20.         if(!prize) {
  21.         sql.get(`SELECT * FROM creditSysteme WHERE id = '${men.id}'`).then(res => { // select user from table
  22.             if(!res) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if the user does'nt exisit in table
  23.             if(res) { // if user exsist
  24.                     msg.channel.send("**"+men.username+" :credit_card: balance is ``"+res.credits+"$``.**") // reply
  25.             }
  26.         })
  27.         }else{ // else ?
  28.             if(isNaN(prize)) return msg.channel.send(" :interrobang: | "+msg.author.username+", type the credit you need to transfer!"); // is nan :)
  29.             if(parseFloat(prize) === NaN) return msg.channel.send(" :interrobang: | "+msg.author.username+", type the credit you need to transfer!"); // if nan :))
  30.             if(men === msg.author) return; // if the men = author
  31.             let authorRes = await sql.get(`SELECT * FROM creditSysteme WHERE id = '${msg.author.id}'`) // select from sql
  32.             let userRes = await sql.get(`SELECT * FROM creditSysteme WHERE id = '${men.id}'`) // select from sql
  33.             if(!authorRes) sql.run(`INSERT INTO creditSysteme VALUES ('${msg.author.id}', 0, 0)`) // if !user create new col
  34.             if(!userRes) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if !user create new col
  35.             let authorCredits = authorRes.credits; // credits before transfer
  36.             let userCredits = userRes.credits; // credits before transfer
  37.             let tprize = Math.floor(prize - (prize * 5/100))
  38.             if(prize <= 5) tprize = prize
  39.             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
  40.             sql.run(`UPDATE creditSysteme SET credits = ${authorCredits - parseInt(prize)} WHERE id = '${msg.author.id}'`); // uptade credits for the author
  41.             sql.run(`UPDATE creditSysteme SET credits = ${userCredits + parseInt(tprize)} WHERE id = '${men.id}'`); // update credits for the mentions user
  42.             msg.channel.send("**:moneybag: | "+msg.author.username+", has transferred ``$"+tprize+"`` to "+men.toString()+"**") // the message :)
  43.             men.send(`:atm:  |  Transfer Receipt\n\`\`\`You have received $ ${tprize} from user ${msg.author.username} (ID: ${msg.author.id})\`\`\``) // the dm message :)
  44.         }
  45.     } else if(msg.content.startsWith(prefix+"daily")) {  // if the message content daily do
  46.         let daily = 86400000; // 24h
  47.         let amount = Math.floor((Math.random() * 500) + 1) // Money
  48.         let res = await sql.get(`SELECT * FROM creditSysteme WHERE id = '${msg.author.id}'`) // select from sql
  49.         if(!res) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if !user create new col
  50.         let time = res.timeDaily; // select last daily
  51.         let credits = res.credits; // credits before daily
  52.         if(time != null && daily - (Date.now() - time) > 0) { // if already climed the daily in same day
  53.             let fr8 = ms(daily - (Date.now() - time)); // the remining time
  54.             msg.channel.send("**:stopwatch: | "+msg.author.username+", your daily :yen: credits refreshes in "+fr8.hours+" hours and "+fr8.seconds+" seconds. **") //reply
  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.     } else if(msg.content.startsWith(prefix+"setC")) {
  60.         var sacredits = msg.content.split(" ").slice(1).join(" ")
  61.         var sucredits = msg.content.split(" ").slice(2).join(" ")
  62.         var mentioned = msg.mentions.users.first().id
  63.         if(msg.author.bot || !msg.channel.guild || isNaN(sucredits) || parseFloat(sucredits) === NaN || isNaN(sucredits) || parseFloat(sucredits) === NaN || sucredits > 100000000 ) return msg.channel.send("**Type The Amount You Want To Set!**");
  64.         if(msg.author.id !== "You'r Id") return;
  65.         client.fetchUser(mentioned).then((user) => {
  66.             userid = user
  67.             if(mentioned) {
  68.                 sql.run(`UPDATE creditSysteme SET credits = '${sucredits}' WHERE id = '${mentioned}'`).then(msg.channel.send("**Donne...**"))
  69.             }
  70.             if(!mentioned) {
  71.                 sql.run(`UPDATE creditSysteme SET credits = '${sacredits}' WHERE id = '${msg.author.id}'`).then(msg.channel.send("**Donne...**"))
  72.             }
  73.         })
  74.     }
  75. });
Advertisement
Add Comment
Please, Sign In to add comment