diff --git a/TODO.md b/TODO.md index 13c7624..7fcecaf 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,5 @@ ## TODOs -- Create balance command for user to view their balance +- ~~Create balance command for user to view their balance~~ - Create a command that explains the pricing - Update welcome and insufficient replies - add a section for the help menu to show items that need tokens diff --git a/commands/balance.js b/commands/balance.js new file mode 100644 index 0000000..96ca913 --- /dev/null +++ b/commands/balance.js @@ -0,0 +1,28 @@ +const { SlashCommandBuilder } = require('discord.js'); +const { DebugBuilder } = require("../utilities/debugBuilder"); +const log = new DebugBuilder("server", "balance"); + +const { checkBalance } = require("../controllers/accountController"); + +module.exports = { + data: new SlashCommandBuilder() + .setName('balance') + .setDescription('Check your balance of AI tokens'), + example: "balance", + isPrivileged: false, + requiresTokens: false, + defaultTokenUsage: 0, + deferInitialReply: false, + async execute(interaction) { + try{ + checkBalance(interaction.member.id, async (err, balance) => { + if (err) throw err; + + await interaction.reply({ content: `${interaction.member.user}, you have ${balance} tokens remaining`, ephemeral: true }) + }) + }catch(err){ + log.ERROR(err) + await interaction.reply(`Sorry ${interaction.member.user}, something went wrong`); + } + } +}; \ No newline at end of file diff --git a/controllers/accountController.js b/controllers/accountController.js index deb841a..a6287d0 100644 --- a/controllers/accountController.js +++ b/controllers/accountController.js @@ -36,8 +36,8 @@ exports.createAccount = (_discordAccountId, callback) => { }) } -exports.withdrawBalance = async (_withdrawAmount, _accountId, callback) => { - userStorage.updateBalance('withdraw', _withdrawAmount, _accountId, async (err, result) => { +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); @@ -55,6 +55,22 @@ exports.verifyBalance = (_tokensToBeUsed, _accountId, callback) => { 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") diff --git a/libStorage.js b/libStorage.js index 2b969f8..8974c00 100644 --- a/libStorage.js +++ b/libStorage.js @@ -177,14 +177,26 @@ exports.UserStorage = class UserStorage extends Storage { }) } + /** + * Check or return the balance of a given account ID + * + * @param {*} _tokensToBeUsed The amount of tokens to be used, set to 0 to return the balance + * @param {*} _account_id The account ID to check or return the balance of + * @param {*} callback + */ checkBalance(_tokensToBeUsed, _account_id, callback) { + if (!_account_id) return callback(new Error("Account not specified when checking account balance"), undefined); + if (!_tokensToBeUsed && !_tokensToBeUsed >= 0) return callback(new Error("Specified tokens are invalid when checking account balance"), undefined); this.getRecordBy('account_id', _account_id, (err, record) => { if (err) return callback(err, undefined); - - if (record?.balance && record.balance > _tokensToBeUsed) return callback(undefined, true); - else{ - return callback(undefined, false); + + // Check to see if the account has a balance greater than what was given + if(_tokensToBeUsed > 0){ + if (record?.balance && record.balance > _tokensToBeUsed) return callback(undefined, true); + else return callback(undefined, false); } + + return callback(undefined, record.balance) }) } @@ -193,11 +205,11 @@ exports.UserStorage = class UserStorage extends Storage { * * @param {string} _updateType The type of update to make to the account [ withdraw | deposit ] * @param {number} _updateAmount The amount to update the account - * @param {number} _account_id The ID of the account to update + * @param {number} _account_id The ID of the discord account to update * @param {function} callback The callback function to call with the results * @returns Result from the SQL query or false */ - updateBalance(_updateType, _updateAmount, _account_id, callback){ + updateBalance(_updateType, _updateAmount, _discord_account_id, callback){ var sqlQuery = ""; switch(_updateType){ case "withdraw":