|
| 1 | +const Discord = require("discord.js"); |
| 2 | +const fs = require("fs"); |
| 3 | +const Perks = require("../utils/perks.js") |
| 4 | +const colors = require("../colors.json"); |
| 5 | +const botsettings = require("../botsettings.json"); |
| 6 | +const storeJSON = require("../store.json"); |
| 7 | +const prefix = botsettings.prefix; |
| 8 | +const bugHunterRoleID = botsettings.roleid_bughunter; |
| 9 | + |
| 10 | +const perksPath = "../utils/Perks/"; |
| 11 | + |
| 12 | +/** |
| 13 | + * |
| 14 | + * @param {Discord.Client} bot |
| 15 | + * @param {Discord.TextChannel} channel |
| 16 | + */ |
| 17 | +module.exports.run = async (bot, message, args) => { |
| 18 | + let channel = message.channel; |
| 19 | + let member = message.author; |
| 20 | + let item = args.slice(0).join(" "); |
| 21 | + let storeItems = {}; |
| 22 | + Object.entries(storeJSON).forEach(([key, val]) => storeItems[key.split("_").map(perk => perk.charAt(0).toUpperCase() + perk.slice(1)).join(" ")] = val); |
| 23 | + |
| 24 | + if(!item) return error(`ERROR: Incorrect usage. Please specify an item to buy using: \`${prefix}buy Item_Name\`. If you are unsure of this item's name, you can use \`${prefix}store\` to check it.`) |
| 25 | + item = item.split(" ").map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(" "); |
| 26 | + if(!(storeItems.hasOwnProperty(item) || item.startsWith("Color Roles"))) return error(`ERROR: Incorrect usage. Could not find an item with the name of \`${item}\`. If you are unsure of this item's name, you can use \`${prefix}store\` to check it.`) |
| 27 | + |
| 28 | + const tierColors = [colors.tier0, colors.tier1, colors.tier2, colors.tier3, colors.tier4, colors.tier5] |
| 29 | + const tierRomanNumerals = ["I", "II", "III", "IV", "V"] |
| 30 | + // Get user info |
| 31 | + fs.readFile("./bughuntertracker.json", "utf8", async (err, data) => { |
| 32 | + if(err) return console.error(err); |
| 33 | + const bugHunterArr = JSON.parse(data); |
| 34 | + const bugHunter = bugHunterArr.find(bughunter => bughunter.id === member.id); |
| 35 | + if(bugHunter) { |
| 36 | + const storeItem = storeItems[item] || storeItems["Color Roles"]; |
| 37 | + if(bugHunter.tokens < storeItem.tokens) return error(`ERROR: You do not have enough tokens to buy this item, you need \`${storeItem.tokens - bugHunter.tokens}\` more Bug Token.`); |
| 38 | + try { |
| 39 | + if(item.startsWith("Color Roles")) { |
| 40 | + const perkUnlockResponse = await buyColorRole(args[2]); |
| 41 | + if(perkUnlockResponse !== true) return error(perkUnlockResponse); |
| 42 | + } else { |
| 43 | + const perk = require(`${perksPath + item}.js`) |
| 44 | + const perkUnlockResponse = perk.unlock(channel.guild, bugHunter); |
| 45 | + if(perkUnlockResponse !== true) return error(perkUnlockResponse); |
| 46 | + } |
| 47 | + bugHunter.tokens -= storeItem.tokens; |
| 48 | + } catch (err) { |
| 49 | + return error(`ERROR: This item has not been implemented: \`(${err})\``) |
| 50 | + } |
| 51 | + } else return error("ERROR: Only Bug Hunters can use the store! If you would like to become a bug hunter, check out the <#711987145392390254> channel."); |
| 52 | + const bugHunterJSON = JSON.stringify(bugHunterArr); |
| 53 | + fs.writeFile("./bughuntertracker.json", bugHunterJSON, "utf8", err => { |
| 54 | + if(err) { |
| 55 | + console.error(err); |
| 56 | + return error(`ERROR: Write to database failed. Please send the following message to Whimpers: (${err})`); |
| 57 | + } |
| 58 | + }); |
| 59 | + const confirmationEmbed = new Discord.RichEmbed() |
| 60 | + .setAuthor("Reviewer -", bot.avatarURL) |
| 61 | + .setTitle("SUCCESS") |
| 62 | + .setDescription("Your purchase was successfull! :tada:") |
| 63 | + .setColor(colors.valid); |
| 64 | + channel.sendEmbed(confirmationEmbed).then(msg => msg.delete(5000)); |
| 65 | + }); |
| 66 | + |
| 67 | + message.delete(0); |
| 68 | + |
| 69 | + async function buyColorRole(color) { |
| 70 | + if(!color) return `ERROR: Incorrect Usage. Please specify a color. Correct usage: \`${prefix}buy Color Roles color\``; |
| 71 | + if(!(/^#[0-9A-Fa-f]{6}$/).test(color)) return "ERROR: Color must be in a hex format. Example: #f00ba7"; |
| 72 | + const bugHunterRole = message.guild.roles.find(r => r.id === bugHunterRoleID); |
| 73 | + const colorRole = await channel.guild.createRole({ |
| 74 | + name: color, |
| 75 | + color: color, |
| 76 | + position: bugHunterRole.position |
| 77 | + }) |
| 78 | + message.guild.member(member).addRole(colorRole) |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + // Error Handler |
| 83 | + |
| 84 | + function error(errorMessage) { |
| 85 | + const errorEmbed = new Discord.RichEmbed() |
| 86 | + .setAuthor("Reviewer -", bot.avatarURL) |
| 87 | + .setTitle("ERROR") |
| 88 | + .setDescription(`${errorMessage}\nPurchase process halted. Please run the command again to restart.`) |
| 89 | + .setColor(colors.error); |
| 90 | + channel.send(errorEmbed).then(msg => { |
| 91 | + message.delete(0); |
| 92 | + msg.delete(10000); |
| 93 | + }); |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | + |
| 98 | +module.exports.help = { |
| 99 | + name: "buy", |
| 100 | + description:"Purchases a perk from the store." |
| 101 | +}; |
0 commit comments