gaber-elsayed

Credits 2 , Database (JSON)

Nov 26th, 2019
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. const pretty = require('pretty-ms'); // npm i pretty-ms
  2. const credits = require('./Credits.json');
  3. const creditsPath = './Credits.json';
  4. client.on('message',async message => {
  5. if(message.author.bot || message.channel.type === 'dm') return;
  6. let args = message.content.split(' ');
  7. let author = message.author.id;
  8. if(!credits[author]) credits[author] = { messages: 0, credits: 0, xp: 0, daily: 86400000 };
  9. credits[author].messages += 1;
  10. credits[author].xp += 1;
  11. if(credits[author].xp === 5) {
  12. credits[author].xp = 0;
  13. credits[author].credits += 1;
  14. fs.writeFileSync(creditsPath, JSON.stringify(credits, null, 4));
  15. }
  16. fs.writeFileSync(creditsPath, JSON.stringify(credits, null, 4));
  17.  
  18.  
  19. if(args[0].toLowerCase() == `${prefix}credit` || args[0].toLowerCase() === `${prefix}credits`) {
  20. let mention = message.mentions.users.first() || message.author;
  21. let mentionn = message.mentions.users.first();
  22. if(!credits[mention.id]) return message.channel.send(`**? |** Failed To Find the **Needed Data**.`);
  23. if(!args[2]) {
  24. let creditsEmbed = new Discord.RichEmbed()
  25. .setColor("#36393e")
  26. .setAuthor(mention.username, mention.avatarURL)
  27. .setThumbnail(mention.avatarURL)
  28. .addField(`? الكردت`, `» \`${credits[mention.id].credits} $\`\n`, true)
  29. .addField(`? الرسائل`, `» \`${credits[mention.id].messages} ??\``, true);
  30. message.channel.send(creditsEmbed);
  31.  
  32. } else if(mentionn && args[2]) {
  33. if(isNaN(args[2])) return message.channel.send(`**? |** The **"Number"** You Entered **Isn't Correct**.`);
  34. if(mentionn.id === message.author.id) return message.channel.send(`**? |** You Can't Give **Credits** To **Yourself**.`);
  35. if(args[2] > credits[author].credits) return message.channel.send(`**? |** You don't have **Enough** credits to give to ${mentionn}`);
  36. let first = Math.floor(Math.random() * 9);
  37. let second = Math.floor(Math.random() * 9);
  38. let third = Math.floor(Math.random() * 9);
  39. let fourth = Math.floor(Math.random() * 9);
  40. let num = `${first}${second}${third}${fourth}`;
  41.  
  42. message.channel.send(`**?? |** **Type** \`${num}\` To **Complete** the transfer!`).then(m => {
  43. message.channel.awaitMessages(r => r.author.id === message.author.id, { max: 1, time: 20000, errors:['time'] }).then(collected => {
  44. let c = collected.first();
  45. if(c.content === num) {
  46. message.channel.send(`**? |** Successfully **Transfered** \`$${args[2]}\` !`);
  47. m.delete();
  48. c.delete();
  49. credits[author].credits += (-args[2]);
  50. credits[mentionn.id].credits += (+args[2]);
  51. fs.writeFileSync(creditsPath, JSON.stringify(credits, null, 4));
  52. } else {
  53. m.delete();
  54. }
  55. });
  56. });
  57.  
  58. } else {
  59. message.channel.send(`**? |** The **Syntax** should be like **\`${prefix}credits <Mention> [Ammount]\`**`);
  60. }
  61. } else if(args[0].toLowerCase() === `${prefix}daily`) {
  62. if(credits[author].daily !== 86400000 && Date.now() - credits[author].daily !== 86400000) {
  63. message.channel.send(`**? |** You already **Claimed** the daily ammount of credits since \`${pretty(Date.now() - credits[author].daily)}\`.`);
  64. } else {
  65. let ammount = getRandom(300, 500);
  66. credits[author].daily = Date.now();
  67. credits[author].credits += ammount;
  68. fs.writeFileSync(creditsPath, JSON.stringify(credits, null, 4));
  69. message.channel.send(`**? |** \`${ammount}\`, Successfully **Claimed** Your daily ammount of credits!`);
  70. }
  71. }
  72. });
Add Comment
Please, Sign In to add comment