Advertisement
Guest User

Untitled

a guest
Aug 6th, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Users, CurrencyShop } = require('./dbObjects');
  2. const Sequelize = require('sequelize');
  3. const currency = new Discord.Collection();
  4.  
  5. const sequelize = new Sequelize('database', 'user', 'password', {
  6.     host: 'localhost',
  7.     dialect: 'sqlite',
  8.     logging: false,
  9.     operatorsAliases: false,
  10.     // SQLite only
  11.     storage: 'database.sqlite',
  12. });
  13.  
  14. sequelize
  15.   .authenticate()
  16.   .then(() => {
  17.     console.log('Connection has been established successfully.');
  18.   })
  19.   .catch(err => {
  20.     console.error('Unable to connect to the database:', err);
  21.   });
  22.  
  23. Reflect.defineProperty(currency, 'add', {
  24.     value: async function add(id, amount) {
  25.         const user = currency.get(id);
  26.         if (user) {
  27.             user.balance += Number(amount);
  28.             return user.save();
  29.         }
  30.         const newUser = await Users.create({ user_id: id, balance: amount });
  31.         currency.set(id, newUser);
  32.         return newUser;
  33.     },
  34. });
  35.  
  36. Reflect.defineProperty(currency, 'getBalance', {
  37.     value: function getBalance(id) {
  38.         const user = currency.get(id);
  39.         return user ? user.balance : 0;
  40.     },
  41. });
  42.  
  43. client.once('ready', async () => {
  44.     const storedBalances = await Users.findAll();
  45.     storedBalances.forEach(b => currency.set(b.user_id, b));
  46.     console.log(`Logged in as ${client.user.tag}!`);
  47. });
  48.  
  49. client.on('message', async message => {
  50.     if (message.author.bot) return;
  51.     currency.add(message.author.id, 1);
  52.  
  53.     if (!message.content.startsWith(prefix)) return;
  54.     const input = message.content.slice(prefix.length).trim();
  55.     if (!input.length) return;
  56.     const [, command, commandArgs] = input.match(/(\w+)\s*([\s\S]*)/);
  57.  
  58.     if (command === 'balance') {
  59.  
  60.         const target = message.mentions.users.first() || message.author;
  61.         return message.channel.send(`${target.tag} has ${currency.getBalance(target.id)}💰`);
  62.  
  63.     }
  64.     else if (command === 'inventory') {
  65.  
  66.         const target = message.mentions.users.first() || message.author;
  67.         const user = await Users.findOne({ where: { user_id: target.id } });
  68.         const items = await user.getItems();
  69.  
  70.         if (!items.length) return message.channel.send(`${target.tag} has nothing!`);
  71.         return message.channel.send(`${target.tag} currently has ${items.map(t => `${t.amount} ${t.item.name}`).join(', ')}`);
  72.  
  73.     }
  74.     else if (command === 'transfer') {
  75.  
  76.         const currentAmount = currency.getBalance(message.author.id);
  77.         const transferAmount = commandArgs.split(/ +/).find(arg => !/<@!?\d+>/.test(arg));
  78.         const transferTarget = message.mentions.users.first();
  79.  
  80.         if (!transferAmount || isNaN(transferAmount)) return message.channel.send(`Sorry ${message.author}, that's an invalid amount`);
  81.         if (transferAmount > currentAmount) return message.channel.send(`Sorry ${message.author} you don't have that much.`);
  82.         if (transferAmount <= 0) return message.channel.send(`Please enter an amount greater than zero, ${message.author}`);
  83.  
  84.         currency.add(message.author.id, -transferAmount);
  85.         currency.add(transferTarget.id, transferAmount);
  86.  
  87.         return message.channel.send(`Successfully transferred ${transferAmount}💰 to ${transferTarget.tag}. Your current balance is ${currency.getBalance(message.author.id)}💰`);
  88.  
  89.     }
  90.     else if (command === 'buy') {
  91.  
  92.         const item = await CurrencyShop.findOne({ where: { name: { [Op.like]: commandArgs } } });
  93.         if (!item) return message.channel.send('That item doesn\'t exist.');
  94.         if (item.cost > currency.getBalance(message.author.id)) {
  95.             return message.channel.send(`You don't have enough currency, ${message.author}`);
  96.         }
  97.  
  98.         const user = await Users.findOne({ where: { user_id: message.author.id } });
  99.         currency.add(message.author.id, -item.cost);
  100.         await user.addItem(item);
  101.  
  102.         message.channel.send(`You've bought a ${item.name}`);
  103.  
  104.     }
  105.     else if (command === 'shop') {
  106.  
  107.         const items = await CurrencyShop.findAll();
  108.         return message.channel.send(items.map(i => `${i.name}: ${i.cost}💰`).join('\n'), { code: true });
  109.  
  110.     }
  111.     else if (command === 'leaderboard') {
  112.  
  113.         return message.channel.send(
  114.             currency.sort((a, b) => b.balance - a.balance)
  115.                 .filter(user => client.users.has(user.user_id))
  116.                 .first(10)
  117.                 .map((user, position) => `(${position + 1}) ${(client.users.get(user.user_id).tag)}: ${user.balance}💰`)
  118.                 .join('\n'),
  119.             { code: true }
  120.         );
  121.     }
  122.  
  123. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement