const { DebugBuilder } = require("../utilities/debugBuilder"); const log = new DebugBuilder("server", "accountController"); const { UserStorage } = require("../libStorage"); const userStorage = new UserStorage(); /** * Check to see if the discord ID has an account associated with it * @param {*} _discordAccountId The discord account to look for * @param {*} callback The callback function to be called * @callback false|* */ exports.checkForAccount = (_discordAccountId, callback) => { userStorage.getRecordBy("discord_account_id", _discordAccountId, (err, results) => { if (err) return callback(err, undefined); if (!results) return callback(undefined, false); return callback(undefined, results); }) } /** * Create an account from a discord ID * @param {*} _discordAccountId * @param {*} callback */ exports.createAccount = (_discordAccountId, callback) => { if (!_discordAccountId) return callback(new Error("No discord account specified before creation")); userStorage.saveAccount(_discordAccountId, (err, results) => { if (err) return callback(err, undefined); if (!results) return callback(new Error("No results from creating account"), undefined); return callback(undefined, results); }) } exports.withdrawBalance = async (_withdrawAmount, _discordAccountId, callback) => { userStorage.updateBalance('withdraw', _withdrawAmount, _discordAccountId, async (err, result) => { if (err) return callback(err, undefined); if(result) return callback(undefined, result); return callback(undefined, undefined); }) } exports.verifyBalance = (_tokensToBeUsed, _accountId, callback) => { userStorage.checkBalance(_tokensToBeUsed, _accountId, (err, results) => { if (err) return callback(err, undefined); if(!results) return callback(undefined, false); return callback(undefined, true); }) } /** * Check the given account for the token balance * * @param {*} _discordAccountId * @param {*} callback */ exports.checkBalance = (_discordAccountId, callback) => { if (!_discordAccountId) return callback(new Error("No discord account given to check balance of"), undefined); userStorage.getRecordBy("discord_account_id", _discordAccountId, (err, accountRecord) => { if (err) return callback(err, undefined); if (!accountRecord) return callback(new Error("No account record given"), undefined); return callback(undefined, accountRecord.balance); }) } exports.insufficientTokensResponse = (interaction) => { log.DEBUG("INSUFFICIENT TOKENS RESPONSE") return interaction.reply({ content: `Sorry ${interaction.member.user}, you don't have enough tokens for this purchase. Please contact your bot rep to increase your balance to have more fun playing around!`, ephemeral: true }); } exports.welcomeResponse = (interaction) => { log.DEBUG("WELCOME RESPONSE") return interaction.reply({ content: `Hey there ${interaction.member.user}! You haven't ran any commands that require tokens before. I've gone ahead and created an account for you. When you get a chance reach out to your nearest bot rep to fill your balance to start playing around! In the meantime however, you can run \`/pricing\` to view the current pricing.`, ephemeral: true }); }