Add balance command for users to check their balance

This commit is contained in:
Logan Cusano
2023-02-26 21:42:37 -05:00
parent 94aa9b4a32
commit 7bddccc5f4
4 changed files with 65 additions and 9 deletions

View File

@@ -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

28
commands/balance.js Normal file
View File

@@ -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`);
}
}
};

View File

@@ -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")

View File

@@ -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":