Examples

currency-system examples

Home
Initilize the NPM Package
1. Set's MongoDB URL so It can Connect to MongoDB.
    //Require these at top of index.js
    const CurrencySystem = require("currency-system");
    const cs = new CurrencySystem;
    // Method:
    cs.setMongoURL('MongoURL Here');
2. ( optional ) Set's Default Wallet Amount when creating a new User.
    const CurrencySystem = require("currency-system");
    const cs = new CurrencySystem;
    // Method:
    cs.setDefaultWalletAmount('100') // $100 will be the default wallet money when a new User is created.
3. ( optional ) Set's Default Bank Amount when creating a new User.
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      cs.setDefaultBankAmount('100') // $100 will be the default Bank money when a new User is created.
4. ( optional ) Set's Default Wallet Amount when creating a new User Document.
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      cs.setMaxWalletAmount('10000') // $10,000 will be the Maximum amount of money a user can have in there wallet!
5. ( optional ) Set's Maximum Bank Amount limit of all users.
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      cs.setMaxBankAmount('10000') // $10,000 will be the  Maximum amount of money a user can have in there bank!
6. ( optional ) Check for new updates for the package!
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      cs.searchForNewUpdate(true) // It'll console log if there's a new update.
Economy Methods
1. Add's Money to a User.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      const money = 1000;
      const wheretoPutMoney = 'wallet'; // Or bank
      let result = await cs.addMoney({
        user: 'UserID',
        guild: 'GuildID',
        amount: money,
        wheretoPutMoney: wheretoPutMoney
      });
      if (result.error) return message.reply("You cant add negitive money");
      else message.reply(`Successfully added $${money} to ${message.author.username}, ( in ${wheretoPutMoney} )`)
2. Remove's Money from a User.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      const money = 1000;
      const wheretoPutMoney = 'wallet'; // Or bank
      let result = await cs.removeMoney({
        user: 'userID',
        guild: 'guildID',
        amount: money,
        wheretoPutMoney: wheretoPutMoney
      });
      if (result.error) return message.reply("You cant remove negitive money");
      else message.reply(`Successfully removed $${money} to ${message.author.username}, ( from ${wheretoPutMoney} )`)
3. Add's Money To All Users in a provided guild.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      const money = 1000;
      const wheretoPutMoney = 'wallet'; // Or bank
      let result = await cs.addMoneyToAllUsers({
        guild: message.guild.id,
        amount: money,
        wheretoPutMoney: wheretoPutMoney
      });
      if (result.error) {
        if (result.type === 'negative-money') return message.reply("You cant add negitive money");
        else return message.reply('No User\'s found');
    } else message.reply(`Successfully added $${money} to ${result.rawData.length} people!, ( in ${wheretoPutMoney} )`)
4. Removes's Money To All Users in a provided guild.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      const money = 1000;
      const wheretoPutMoney = 'wallet'; // Or bank
      let result = await cs.removeMoneyFromAllUsers({
        guild: message.guild.id,
        amount: money,
        wheretoPutMoney: wheretoPutMoney
      });
      if (result.error) {
        if (result.type === 'negative-money') return message.reply("You cant remove negitive money");
        else return message.reply('No User\'s found');
    } else message.reply(`Successfully removed $${money} from ${result.rawData.length} people!, ( in ${wheretoPutMoney} )`)
5. Gamble's the money for a user with some success of winning double money than you Gambled for.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let money = args[0].value
      if (isNaN(money)) return message.reply("Amount is not a number.");
      let result = await cs.gamble({
        user: 'userID',
        guild: 'guildID',
        amount: money,
        minAmount: 1,
        cooldown: 25 //25 seconds
      });
      if (result.error) {
        if (result.type == 'amount') return message.reply("Please insert an amount first.");
        if (result.type == 'nan') return message.reply("The amount was not a number.");
        if (result.type == 'low-money') return message.reply(`You don't have enough money. You need ${result.neededMoney}$ more to perform the action. `);
        if (result.type == 'gamble-limit') return message.reply(`You don't have enough money for gambling. The minimum was $${result.minAmount}.`);
        if (result.type == 'time') return message.reply(`Wooo that is too fast. You need to wait **${result.second}** second(s) before you can gamble again.`);
      } else {
        if (result.type == 'lost') return message.reply(`Ahh, no. You lose $${result.amount}. You've $${result.wallet} left. Good luck next time.`);
        if (result.type == 'won') return message.reply(`Woohoo! You won $${result.amount}! You've $${result.wallet}. Good luck, have fun!`);
      }
6. Add's money from wallet to bank.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let money = args[0].value
      if (!money) return message.reply("Enter the amount you want to deposite.");
      let result = await cs.deposite({
        user: 'userID',
        guild: 'guildID',
        amount: money
      });
      if (result.error) {
        if (result.type === 'money') return message.reply("Specify an amount to deposite");
        if (result.type === 'negative-money') return message.reply("You can't deposite negative money");
        if (result.type === 'low-money') return message.reply("You don't have that much money in wallet.");
        if (result.type === 'no-money') return message.reply("You don't have any money to deposite");
        if (result.type === 'bank-full') return message.reply("Your bank is full. It has reached it's limit.");
      } else {
        if (result.type === 'all-success') return message.reply("You have deposited all your money to your bank" + `\nNow you've $${result.rawData.wallet} In your wallet and $${result.rawData.bank} in your bank.`);
        if (result.type === 'success') return message.reply(`You have deposited $${result.amount} money to your bank.\nNow you've $${result.rawData.wallet} In your wallet and $${result.rawData.bank} in your bank.`);
      };
7. Take's money from bank to wallet.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let money = args[0].value
      if (!money) return message.reply("Enter the amount you want to withdraw.");
      let result = await cs.withdraw({
        user: 'userID',
        guild: 'guildID',
        amount: money
      });
      if (result.error) {
        if (result.type === 'money') return message.reply("Specify an amount to withdraw")
        if (result.type === 'negative-money') return message.reply("You can't withdraw negative money, please use deposit command")
        if (result.type === 'low-money') return message.reply("You don't have that much money in bank.")
        if (result.type === 'no-money') return message.reply("You don't have any money to withdraw")
      } else {
        if (result.type === 'all-success') return message.reply("You have withdraw'd all your money from your bank" + `\nNow you've $${result.rawData.wallet} In your wallet and $${result.rawData.bank} in your bank.`)
        if (result.type === 'success') return message.reply(`You have withdraw $${result.amount} money from your bank.\nNow you've $${result.rawData.wallet} In your wallet and $${result.rawData.bank} in your bank.`)
      };
8. Gets current balance of user ( Bank and wallet ).
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      const user = message.options.getUser('user') || 'userID';
      let result = await cs.balance({
        user: user,
        guild: 'guildID'.id
      });
      return message.reply(`${user.tag}, has $${(result.wallet).toLocaleString()} in there wallet and $${(result.bank).toLocaleString()} in there bank. There Max bank has been set to $${(result.rawData.bankSpace.toLocaleString())}`);
9. Moves money from one user to another.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let user = message.options.getUser('user');
      if (user.id === 'userID'.id) return message.channel.send(`You can't transfer money to yourself!`);
      let amount = message.options.getInteger('amount');
      if (!amount) return message.reply("Enter amount of money to add.");
      if (amount.includes("-")) return message.reply("You can't send negitive money.")
      let money = parseInt(amount);

       let result = await cs.transferMoney({
       user: 'userID',
        user2: user,
        guild: 'guildID',
        amount: money
       });
       if (result.error) return message.reply(`You don't have enough money in your wallet.`);
       else message.reply(`**${message.author.username}**, Successfully transfered **${result.money}** to **${result.user2.username}**`)
Leaderboards
1. Gets Guild's leaderboard.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let data = await cs.leaderboard('guildID');
      if (data.length < 1) return message.reply("Nobody's in leaderboard yet.");
      const msg = new Discord.MessageEmbed();
      let pos = 0;
      // This is to get First 10 Users :)
      data.slice(0, 10).map(e => {
        if (!client.users.cache.get(e.userID)) return;
        pos++
        msg.addField(`${pos} - **${client.users.cache.get(e.userID).username}**`, `Wallet: **${e.wallet}** - Bank: **${e.bank}**`, true);
      });
  
      message.reply({
          embeds: [msg]
      }).catch();
2. Gets Global leaderboard.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let data = await cs.globalLeaderboard();
      if (data.length < 1) return message.reply("Nobody's in Global leaderboard yet.");
      const msg = new Discord.MessageEmbed();
      let pos = 0;
      // This is to get First 10 Users )
      data.slice(0, 10).map(e => {
        if (!client.users.cache.get(e.userID)) return;
        pos++
        msg.addField(`${pos} - **${client.users.cache.get(e.userID).username}**`, `Wallet: **${e.wallet}** - Bank: **${e.bank}**`, true);
      });
  
      message.reply({
        embeds: [msg]
      }).catch(); 
Money Earning Methods
1. User can use it once per hour.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let result = await cs.hourly({
        user: 'UserID',
        guild: 'guildID',
        amount: 100,
      });
      if (result.error) return message.reply(`You have used hourly recently Try again in ${result.time}`);
      else return message.reply(`You have earned $${result.amount}. Your streak is now ${result.rawData.streak.hourly}`);
2. User can use it once every 6 hours.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let result = await cs.quaterly({
        user: 'userID',
        guild: 'guildID',
        amount: 100,
      });
      if (result.error) return message.reply(`You have used quaterly recently Try again in ${result.time}`);
      else message.reply(`You have earned $${result.amount}. Your streak is now ${result.rawData.streak.quaterly}`);
3. User can use it once every 12 hours.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let result = await cs.hafly({
        user: 'userID',
        guild: 'guildID',
        amount: 100,
      });
      if (result.error) return message.reply(`You have used hafly recently Try again in ${result.time}`);
      else return message.reply(`You have earned $${result.amount}. Your streak is now ${result.rawData.streak.hafly}`);
4. User can use it once every 24 hours.
      // Requiring the Package
        const CurrencySystem = require("currency-system");
        const cs = new CurrencySystem;
        // Method:
        let result = await cs.daily({
          user: 'userID',
          guild: 'guildID',
          amount: 100,
        });
        if (result.error) return message.reply(`You have used daily recently Try again in ${result.time}`);
        else message.reply(`You have earned $${result.amount}. Your streak is now ${result.rawData.streak.daily}`);
5. User can use it once every 7 days.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let result = await cs.weekly({
        user: 'userID',
        guild: 'guildID',
        amount: 100,
      });
      if (result.error) return message.reply(`You have used weekly recently Try again in ${result.time}`);
      else message.reply(`You have earned $${result.amount}. Your streak is now ${result.rawData.streak.weekly}`);
6. User can use it once every month.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let result = await cs.monthly({
        user: 'userID',
        guild: 'guildID',
        amount: 6000,
      });
      if (result.error) return message.reply(`You have used monthly recently Try again in ${result.time}`);
      else message.reply(`You have earned $${result.amount}. Your streak is now ${result.rawData.streak.monthly}`);
7. User can use it once every year.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let result = await cs.yearly({
        user: 'userID',
        guild: 'guildID',
        amount: 6000,
      });
      if (result.error) return message.reply(`You have used yearly recently Try again in ${result.time}`);
      else message.reply(`You have earned $${result.amount}. Your streak is now ${result.rawData.streak.yearly}`);
7. User can use it every x time. Which Bot Developor can set.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let result = await cs.work({
        user: 'userID',
        guild: 'guildID',
        maxAmount: 100,
        replies: ['Programmer', 'Builder', 'Waiter', 'Busboy', 'Chief', 'Mechanic'],
        cooldown: 25 //25 seconds
      });
      if (result.error) return message.reply(`You have already worked recently Try again in ${result.time}`);
      else message.reply(`You worked as a ${result.workType} and earned $${result.amount}.`)
8. Rob's a user and have 50-50 chance of getting his money.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let user = args[0].user
      if (user.bot || user === client.user) return message.reply("This user is a bot.");
      if (!user) return message.reply('Sorry, you forgot to mention somebody.');

      let result = await cs.rob({
        user: 'userID',
        user2: user,
        guild: 'guildID',
        minAmount: 100,
        successPercentage: 5,
        cooldown: 25, //25 seconds,
        maxRob: 1000
      });
      if (result.error) {
        if (result.type === 'time') return message.reply(`You have already robbed recently Try again in ${result.time}`);
        if (result.type === 'low-money') return message.reply(`You need atleast $${result.minAmount} to rob somebody.`);
        if (result.type === 'low-wallet') return message.reply(`${result.user2.username} have less than $${result.minAmount} to rob.`)
        if (result.type === 'caught') return message.reply(`${'userID'.username} you robbed ${result.user2.username} and got caught and you payed ${result.amount} to ${result.user2.username}!`)
      } else {
          if (result.type === 'success') return message.reply(`${'userID'.username} you robbed ${result.user2.username} and got away with ${result.amount}!`)
      }
9. User can use it every x time. Which Bot Developor can set.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let result = await cs.beg({
       user: 'userID',
       guild: 'guildID',
       minAmount: 100,
       maxAmount: 400
      });
      if (result.error) return message.reply(`You have begged recently Try again in ${result.time}`);
      else message.reply(`You have earned $${result.amount}.`)
Latest!!
Inventory System
1. Get User's Inventory.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      const user = message.options.getUser('user') || 'userID';
      let result = await cs.getUserItems({
        user: user,
        guild: 'guildID',
      });
      let inv = result.inventory.slice(0, 10)
      const embed = new Discord.MessageEmbed()
        .setDescription('Your Inventory in Empty!')
      for (key of inv) {
        embed.addField(`**${key.name}:**`, `Amount: ${key.amount}`);
        embed.setDescription('Your Inventory!')
      };
      return message.reply({
        embeds: [embed]
      })
2. Get Shop's Inventory.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let result = await cs.getShopItems({
        guild: 'guildID'
      });
      let inv = result.inventory;
      const embed = new Discord.MessageEmbed()
        .setDescription('Shop!')
      for (let key in inv) {
        embed.addField(`${parseInt(key) + 1} - Price: $${inv[key].price} - **${inv[key].name}:**`, inv[key].description)
      }
      message.reply({
        embeds: [embed]
      })
3. Add Item's to Shop!!
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      if (message.options.getInteger('price') < 1) return message.channel.send("You can't add an item for less than 1$!");
      let result = await cs.addItem({
        guild: 'guildID',
        inventory: {
          name: message.options.getString('name'),
          price: message.options.getInteger('price'),
          description: message.options.getString('description')
        }
      });
      if (result.error) {
        if (result.type == 'No-Inventory-Name') return message.channel.send('There was a error, Please enter item name to removadd.!')
        if (result.type == 'Invalid-Inventory-Price') return message.channel.send('There was a error, invalid price!')
        if (result.type == 'No-Inventory-Price') return message.channel.send('There was a error, You didnt specify price!')
        if (result.type == 'No-Inventory') return message.channel.send('There was a error, No data recieved!')
      } else return message.channel.send('Done! Successfully added `' + message.options.getString('name') + '` to the shop!')
4. Removes Item's to Shop!!
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      if (!args[0].value) return message.reply('Which item to remove?')
      let result = await cs.removeItem({
        guild: 'guildID',
        item: parseInt(args[0].value)
      });
      if (result.error) {
        if (result.type == 'Invalid-Item-Number') return message.reply('There was a error, Please enter item number to remove.!')
        if (result.type == 'Unknown-Item') return message.reply('There was a error, The Item Does not exist!')
      } else message.reply('Done! Successfully removed the `' + result.inventory.name + '` from shop!')
5. Set's Item's on Shop at once without doing it one by one.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      cs.setItems({
        guild: 'guildID', // guild, remove this line to make it global
        shop: [{
          name: 'Watch',
          price: 20
        }, {
            name: 'Rolex',
            price: 1230
          }]
      }).then(console.log)
6. addUserItem Command for User's to buy items from Shop.

   Note: This will remove money from user's bank

      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let thing = args[0].value
      if (!thing) return message.reply('Please provide item number')
      if (isNaN(thing)) return message.reply('Please provide valid item number')
      let result = await cs.addUserItem({
        user: 'userID',
        guild: 'guildID',
        item: parseInt(args[0].value)
      });
      if (result.error) {
        if (result.type === 'No-Item') return message.reply('Please provide valid item number');
        if (result.type === 'Invalid-Item') return message.reply('item does not exists');
        if (result.type === 'low-money') return message.reply(`**You don't have enough balance to buy this item!**`);
      } else return message.reply(`**Successfully bought  \`${result.inventory.name}\` for $${result.inventory.price}**`)
7. Sell Command for User's to Sell items.

   Note: This will NOT add money to user's bank

      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      if (!args[0].value) return message.reply('Which item to remove?')
       let result = await cs.removeUserItem({
        user: 'userID',
        guild: 'guildID',
        item: parseInt(args[0].value)
       });
      if (result.error) {
        if (result.type == 'Invalid-Item-Number') return message.reply('There was a error, Please enter item number to remove.!')
        if (result.type == 'Unknown-Item') return message.reply('There was a error, The Item Does not exist!')
      } else message.reply('Done! Successfully sold the `' + result.inventory.name + '` for free! You now have ' + result.inventory.amount + ' of those items left!')

  You can modify same example to make a 'use' command.

      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      // !use < item name from your inventory>
      let item = args[0].value;
      if (!item) return message.reply("What item you wana use?")
      if (parseInt(item)) return message.reply("Please use the name of the item, not the ID.")
      let haveItem = false;
      const arr = await cs.getUserItems({
        user: 'userID',
        guild: 'guildID',
      });
      let i;
      for (key in arr.inventory) {
        if (arr.inventory[key].name.toLowerCase().includes(item.toLowerCase())) haveItem = true
        i = key;
      };
      if (haveItem) {
        let money = Math.floor((Math.random() * 10) + 1) * 100 // 100 - 1000
        let result = await cs.addMoney({
          user: 'userID',
          guild: 'guildID',
          amount: money,
          wheretoPutMoney: 'wallet'
        });
        await cs.removeUserItem({
          user: 'userID',
          guild: 'guildID',
          item: i
        });
        if (result.error) {
          console.log(result)
          return message.reply("Unknown error occured see console.")
        } else return message.reply("You've used " + item + " and earned $" + money)
      } else return message.reply("Please buy the item first!")
8. This tell's the about a user on what command he used already.
      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let result = await cs.info('userID'.id, 'guildID'.id);
      const embed = new Discord.MessageEmbed()
        .setDescription('Info about ' + 'userID'.tag);
      let unUsed = '';
      let cantBeUsed = '';
      for (const [key, value] of result.info) {
        if (value.used) unUsed += `- ${key}\n`;
        else cantBeUsed += `- ${key} ( ${value.timeLeft} )\n`;
      }
      embed.addField('Commands That you can use:', unUsed || 'None');
      embed.addField('Commands That you can\'t use:', cantBeUsed || 'None');
      message.reply({
       embeds: [embed]
      });
Miscellaneous Functions
1. Get User's Raw Data.

  Note: It will auto create new user if it dosn't already exist.

      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      let result = await cs.findUser({user: 'UserID', guild: 'guildID'});
      console.log(result);
2. Save User's Raw Data.

  Note: It can take few moments to update the DB.

      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
      cs.saveUser('Raw data from findUser Function.');
3. Wait for x time.

  Note: It uses mili-seconds. You need to await it else it wont work.

      // Requiring the Package
      const CurrencySystem = require("currency-system");
      const cs = new CurrencySystem;
      // Method:
       await cs.sleep(Time_in_MS);
Custom Commands
1. Bank Note command.
2. Slot command command.
3. Set Bank Space command.
Pro Tips
If you want your bot to never crash on errors use following code:
 process.on("unhandledRejection", _ => console.error(_.stack + '\n' + '='.repeat(20)));
Make Sure to check out our Example
Bot for more guidance :)

© currency-system 2021, All Rights Reserved.