2022-05-12 17:38:08 +03:00
const { MongoClient , ServerApiVersion } = require ( 'mongodb' );
// const { update } = require('apt');
2022-09-27 16:45:50 -04:00
const { Collection , Client , Formatters , Intents , Interaction } = require ( 'discord.js' );
2022-05-19 21:31:40 +03:00
const { CLIENT_ODBC } = require ( 'mysql/lib/protocol/constants/client' );
2022-06-17 16:41:02 +03:00
const { time } = require ( '@discordjs/builders' );
let currencySymbol = '$' ;
2022-05-24 08:55:57 +03:00
//Declair an "enum" to help with BASE calculations
const BASE = {
PAY : 5 ,
HP : 5 ,
2022-05-25 22:33:42 +03:00
MP : 10 ,
XP : 5
2022-05-24 08:55:57 +03:00
}
const STATE = {
IDLE : 0 ,
FIGHTING : 1 ,
2022-05-29 20:59:39 +03:00
DEFENDING : 2 ,
PRONE : 3 ,
WAITING : 4 //For items ONLY
2022-05-24 08:55:57 +03:00
}
2022-05-12 17:38:08 +03:00
//Note that leveling up to the next level takes 10% more xp than the previous one
//#region functions
function isNum ( arg ) {
return ( ! isNaN ( arg ) && Number . isSafeInteger ( Number ( arg )));
};
2022-09-27 16:45:50 -04:00
function CreateNewCollection ( interaction , client , server , id , opponent = null , game = null ) {
2022-07-31 10:11:55 +03:00
const db = client . db ( String ( server ));
const dbo = db . collection ( id );
db . listCollections ({ name : id })
. next ( function ( err , collinfo ) {
if ( ! collinfo ) {
2022-09-27 16:45:50 -04:00
interaction . reply ( "You didn't have a place in my databases, so I created one for you!\nPlease try your command again!" )
2022-07-31 10:11:55 +03:00
let hp_mp = { maxhp : BASE . HP , hp : BASE . HP , maxmp : BASE . MP , mp : BASE . MP }
dbo . insertOne ({ balance : 10 , rank : 1 , lastdayworked : 0 , xp : 0 , hpmp : hp_mp , game : game , gamesettings : { battle : { class : 'none' , ultimate : true }}, opponent : opponent , state : STATE . IDLE , equipped : { weapons : { main : null , secondary : null }, items : {}}});
}
2022-05-24 08:55:57 +03:00
});
}
2022-11-18 11:01:15 -05:00
function addxp ( interaction , dbo , amt , xp_list , noPing = false ) {
2022-05-12 17:38:08 +03:00
if ( ! isNum ( amt )) { return console . log ( "This isn't a number...." ); }
dbo . find ({ "balance" : { $exists : true }}). toArray ( function ( err , doc ) {
if ( ! String ( doc )) { return console . log ( "ERROR!\nThis account does not exist!" ); }
temp = doc [ 0 ];
2022-05-24 08:55:57 +03:00
let rank = temp . rank + 1 ; //The table starts at rank 0, the user starts at rank 1
const txp = amt ; /*temp.xp + amt; // This part was used before the xp check was made in the 'work' function */
2022-05-12 17:38:08 +03:00
//If the rank is less than 100, you can still advance
if ( rank < 101 ) {
let needed = xp_list . get ( rank );
if ( txp >= needed ) {
//Get to the max level possible with the current xp (may skip)
while ( txp >= needed ) {
rank ++ ;
needed = xp_list . get ( rank );
}
rank -- ; //Maybe?
2022-05-29 20:59:39 +03:00
let newhp ;
if ( newhp < 200 ) {
newhp = BASE . HP * rank ;
} else {
newhp = temp . hpmp . hp + 50 ;
}
let newmp = temp . mp + 5 ;
2022-11-18 11:01:15 -05:00
dbo . updateOne ({ balance : temp . balance , rank : temp . rank , lastdayworked : temp . lastdayworked }, { $set : { rank : rank , hpmp : { maxhp : newhp , maxmp : newmp }, xp : txp }});
var user ;
if ( interaction . user ) {
user = interaction . user ;
} else {
// This is a message
user = interaction . author ;
}
interaction . channel . send ( 'Congradulations <@' + user . id + '> for reaching rank ' + String ( rank ) + '!' );
2022-05-12 17:38:08 +03:00
}
2022-05-29 20:59:39 +03:00
} else {
2022-11-18 11:01:15 -05:00
if ( ! noPing ) {
interaction . reply ( "You've already reached max level!" ). catch (( err ) => {
interaction . channel . send ( "You've already reached max level!" );
});
}
2022-05-12 17:38:08 +03:00
}
dbo . updateOne ({ balance : temp . balance }, { $set : { xp : txp }});
});
}
2022-09-27 16:45:50 -04:00
function getBalance ( dbo , interaction ) {
2022-05-19 21:31:40 +03:00
dbo . find ({ "balance" : { $exists : true }}). toArray ( function ( err , doc ) {
2022-06-17 16:41:02 +03:00
let bal = 0 ;
if ( doc [ 0 ] && doc [ 0 ]. balance ) {
bal = doc [ 0 ]. balance ;
}
2022-09-27 16:45:50 -04:00
return interaction . reply ( `<@ ${ interaction . user . id } >, your current balance is ${ currencySymbol }${ bal } ` )
. catch (( err ) => {
interaction . channel . send ( `<@ ${ interaction . user . id } >, your current balance is ${ currencySymbol }${ bal } ` );
});
2022-05-19 21:31:40 +03:00
});
}
2022-09-27 16:45:50 -04:00
function rank ( dbo , interaction , xp_list ) {
2022-05-12 17:38:08 +03:00
dbo . find ({ "balance" : { $exists : true }}). toArray ( function ( err , doc ) {
if ( ! String ( doc )) { return console . log ( "ERROR!\nThis account does not exist!" ); }
let next = doc [ 0 ]. rank + 1 ;
let needed = xp_list . get ( next );
2022-09-27 16:45:50 -04:00
interaction . channel . send ( `<@ ${ interaction . user . id } '> you are currently at rank ${ next - 1 } and have ${ doc [ 0 ]. xp } xp. You need ${ needed - doc [ 0 ]. xp } more xp to get to rank ${ next } ` );
2022-05-12 17:38:08 +03:00
});
}
//Changes one type of currency for another
function convertCurrency ( id , amt , dbo ) {
}
2022-09-27 16:45:50 -04:00
function checkAndUpdateBal ( dbo , item , interaction , amt ) {
2022-06-17 16:41:02 +03:00
return new Promise ( function ( resolve , reject ) {
dbo . find ({ "balance" : { $exists : true }}). toArray ( b = function ( err , doc ) {
if ( ! String ( doc )) {
2022-09-27 16:45:50 -04:00
interaction . reply ( "Your account doesn't exist, please contact the mods for support" ). catch (() => {
interaction . channel . send ( "Your account doesn't exist, please contact the mods for support" );
});
2022-06-17 16:41:02 +03:00
return false ;
}
2022-05-12 17:38:08 +03:00
2022-09-27 16:45:50 -04:00
const icost = amt * item . cost ;
2022-06-17 16:41:02 +03:00
if ( doc [ 0 ]. balance < icost ) {
2022-09-27 16:45:50 -04:00
interaction . reply ( "Insufficient funds!" ). catch (() => { interaction . channel . send ( "Insufficient funds!" ); });
2022-06-17 16:41:02 +03:00
resolve ( false );
} else {
let temp = doc [ 0 ];
dbo . updateOne ({ balance : temp . balance , rank : temp . rank , lastdayworked : temp . lastdayworked }, { $set : { balance : doc [ 0 ]. balance -= icost }});
2022-09-27 16:45:50 -04:00
interaction . reply ( `You have bought ${ item . name } for ${ currencySymbol }${ icost } !` ). catch (() => {
interaction . channel . send ( `You have bought ${ item . name } for ${ currencySymbol }${ icost } !` );
});
2022-06-17 16:41:02 +03:00
resolve ( true );
}
});
2022-05-12 17:38:08 +03:00
});
}
2022-09-27 16:45:50 -04:00
function buy ( id , interaction , dbo , shop , xp_list ) {
const args = interaction . options . data ;
2022-05-12 17:38:08 +03:00
2022-09-27 16:45:50 -04:00
//REAPPLY THIS TO OTHER FUNCTIONS
let query = args . filter (( arg ) => { return ( arg . name == 'item' ); })[ 0 ]. value ;
let amt = args . filter (( arg ) => { return ( arg . name == 'amount' ); })[ 0 ]. value ;
2022-06-17 16:41:02 +03:00
let item = shop . filter ( function ( item ) { return item . name . toLowerCase () == query . toLowerCase (); })[ 0 ];
2022-05-12 17:38:08 +03:00
2022-09-27 16:45:50 -04:00
if ( ! String ( item )) { return interaction . reply ( "This item does not exist!" ). catch (() => { interaction . channel . send ( "This item does not exist!" ); }); }
2022-05-12 17:38:08 +03:00
2022-09-27 16:45:50 -04:00
checkAndUpdateBal ( dbo , item , interaction , amt ). then (( success ) => {
//The message is handled in the CheckAndUpdateBal() function
if ( ! success ) { return }
2022-05-12 17:38:08 +03:00
2022-06-17 16:41:02 +03:00
var newObj = { name : item . name , cost : item . cost , icon : item . icon , sect : item . sect };
2022-05-12 17:38:08 +03:00
2022-09-27 16:45:50 -04:00
addxp ( interaction , dbo , Math . ceil ( item . cost * 1.2 ), xp_list );
2022-06-17 16:41:02 +03:00
dbo . find ( newObj , { $exists : true }). toArray ( function ( err , doc ) {
if ( String ( doc )) {
2022-09-27 16:45:50 -04:00
let newnum = doc [ 0 ]. num + amt ;
2022-06-17 16:41:02 +03:00
dbo . updateOne ({ name : item . name }, { $set : { num : newnum }});
} else {
2022-09-27 16:45:50 -04:00
item . num = amt ;
2022-06-17 16:41:02 +03:00
// dbo.insertOne({ name: item.name, cost: item.cost, icon: item.icon, sect: item.sect, num: Number(args[0])}); //Causes "cyclic dependancy"
dbo . insertOne ( item );
2022-09-27 16:45:50 -04:00
dbo . updateOne ( item , { $set : { num : amt }});
2022-06-17 16:41:02 +03:00
}
});
})
2022-05-12 17:38:08 +03:00
};
2022-09-27 16:45:50 -04:00
//FIXME
function sell ( id , interaction , dbo , shop , xp_list ) {
const args = interaction . options . data ;
const query = args . filter (( arg ) => { return ( arg . name == 'item' ); })[ 0 ]. value ;
var num = args . filter (( arg ) => { return ( arg . name == 'amount' ); })[ 0 ]. value ;
2022-05-19 21:31:40 +03:00
let item = shop . filter ( function ( titem ) { return titem . name . toLowerCase () == query . toLowerCase (); });
2022-09-27 16:45:50 -04:00
if ( ! String ( item )) {
return interaction . reply ( "This item does not exist!" ). catch (( err ) => {
interaction . channel . send ( "This item does not exist!" );
});
}
2022-05-12 17:38:08 +03:00
2022-05-19 21:31:40 +03:00
item [ 0 ] = { name : item [ 0 ]. name , cost : item [ 0 ]. cost , icon : item [ 0 ]. icon , sect : item [ 0 ]. sect };
let functional_item = item [ 0 ];
dbo . find ( functional_item , { $exists : true }). toArray ( function ( err , doc ) {
2022-05-12 17:38:08 +03:00
if ( String ( doc )) {
2022-05-19 21:31:40 +03:00
//Make sure you don't sell more than you have
if ( num < doc [ 0 ]. num ) {
let newNum = doc [ 0 ]. num - num ;
dbo . updateOne ({ name : item [ 0 ]. name }, { $set : { num : newNum }});
} else {
num = doc [ 0 ]. num ;
dbo . deleteOne ({ name : item [ 0 ]. name });
}
//Update the balance
let amountSoldFor = functional_item . cost * num ;
dbo . find ({ "balance" : { $exists : true }}). toArray ( function ( err , doc ) {
let currentBal = doc [ 0 ]. balance ;
dbo . updateOne ({ "balance" : { $exists : true }}, { $set : { balance : currentBal + amountSoldFor }});
});
2022-09-27 16:45:50 -04:00
addxp ( interaction , dbo , Math . ceil ( functional_item . cost * 1.2 ), xp_list );
2022-05-19 21:31:40 +03:00
2022-09-27 16:45:50 -04:00
interaction . reply ( `You've sold ${ num } ${ String ( functional_item . name ) } for ${ currencySymbol }${ amountSoldFor } ` )
. catch (( err ) => {
interaction . channel . send ( `You've sold ${ num } ${ String ( functional_item . name ) } for ${ currencySymbol }${ amountSoldFor } ` );
});
2022-05-12 17:38:08 +03:00
} else {
2022-09-27 16:45:50 -04:00
interaction . reply ( "You don't own this item!" ). catch (( err ) => {
interaction . channel . send ( `You've sold ${ num } ${ String ( functional_item . name ) } for ${ currencySymbol }${ amountSoldFor } ` );
});
2022-05-12 17:38:08 +03:00
}
});
}
2022-09-27 16:45:50 -04:00
function work ( dbo , interaction , xp_list ) {
2022-05-12 17:38:08 +03:00
let fulldate = new Date ();
let date = fulldate . getDate ();
dbo . find ({ "lastdayworked" : { $exists : true }}). toArray ( function ( err , doc ) {
2022-09-27 16:45:50 -04:00
if ( ! String ( doc )) {
return interaction . reply ( "Your account doesn't exist, please contact the mods for support" ). catch (( err ) => {
interaction . channel . send ( "Your account doesn't exist, please contact the mods for support" );
});
}
2022-06-26 08:26:01 +03:00
if ( doc [ 0 ]. lastdayworked == date ) { //date
2022-09-27 16:45:50 -04:00
interaction . reply ( "You've already worked today, try again tomorrow!" ). catch (( err ) => {
interaction . channel . send ( "You've already worked today, try again tomorrow!" );
});
2022-05-12 17:38:08 +03:00
} else {
//Amount to be paid
let amt = 0 ;
2022-05-24 08:55:57 +03:00
amt = ( BASE . PAY * doc [ 0 ]. rank );
let xp_earned = doc [ 0 ]. xp + Math . ceil ( amt * 1.5 );
//Update the amount to the new TOTAL balance
dbo . updateOne ({ "balance" : { $exists : true }}, { $set : { balance : doc [ 0 ]. balance + amt , lastdayworked : date }});
2022-09-27 16:45:50 -04:00
addxp ( interaction , dbo , xp_earned , xp_list );
interaction . reply ( `<@ ${ interaction . user . id } > worked and earned ${ currencySymbol }${ amt } and ${ xp_earned } xp!` ). catch (( err ) => {
interaction . channel . send ( `<@ ${ interaction . user . id } > worked and earned ${ currencySymbol }${ amt } and ${ xp_earned } xp!` )
});
2022-05-12 17:38:08 +03:00
}
});
}
2022-09-27 16:45:50 -04:00
function printInventory ( dbo , interaction ) {
2022-05-12 17:38:08 +03:00
let tempstring = "" ;
dbo . find (). toArray ( function ( err , docs ){
docs . forEach ( val => {
2022-06-17 16:41:02 +03:00
if ( ! val . balance && val . name != undefined ) {
2022-05-12 17:38:08 +03:00
tempstring += String ( val . num ) + " " + val . name + " (" + val . icon + ")\n" ;
}
});
2022-06-17 16:41:02 +03:00
2022-05-19 21:31:40 +03:00
if ( tempstring == "" ) { tempstring += "You have nothing in your inventory!" ; }
2022-09-27 16:45:50 -04:00
interaction . reply ( tempstring ). catch (( err ) => {
interaction . channel . send ( tempstring );
});
2022-05-12 17:38:08 +03:00
});
}
2022-09-27 16:45:50 -04:00
function getShop ( interaction , items , bot ) {
const args = interaction . options . data ;
const type = args . filter (( arg ) => { return ( arg . name == 'type' ); })[ 0 ]. value . toLowerCase ();
2022-05-12 17:38:08 +03:00
2022-09-27 16:45:50 -04:00
// if (args.length == 0) {
// let temp = Formatters.codeBlock(items.map(i => `${i.sect}`).join(' '));
// temp = [...new Set(temp.split(' '))];
// return message.reply(`Please use the format ${bot.prefix}shop [type] [page number]\nTypes are: ${temp}`);
// }
2022-05-12 17:38:08 +03:00
let ind = 1 ;
let noinp = false ;
if ( args . length > 1 ) {
2022-09-27 16:45:50 -04:00
const amt = args . filter (( arg ) => { return ( arg . name == 'page' ); })[ 0 ]. value ;
if ( amt . value < ( items . length / 9 )) {
ind = Number ( amt );
2022-05-12 17:38:08 +03:00
} else {
2022-09-27 16:45:50 -04:00
return interaction . reply ( "That number is too large" ). catch (() => { interaction . channel . send ( "That number is too large" ); });
2022-05-12 17:38:08 +03:00
}
} else {
noinp = true ;
}
2022-09-27 16:45:50 -04:00
const items2 = items . filter ( function ( f ) { return ( f . sect . toLowerCase () == type ) }). slice (( ind - 1 ) * 10 , ( ind - 1 ) * 10 + 10 );
2022-06-17 16:41:02 +03:00
newText = Formatters . codeBlock ( items2 . map ( i => ` ${ i . icon } ( ${ i . name } ): $ ${ i . cost } ` ). join ( '\n' )); //${currencySymbol} doesn't owrk for some reason
2022-05-12 17:38:08 +03:00
if ( noinp ) {
2022-05-24 08:55:57 +03:00
newText += `(Use ${ bot . prefix } shop [type] [page number] to access other pages)` ;
2022-05-12 17:38:08 +03:00
}
2022-09-27 16:45:50 -04:00
return interaction . reply ( newText ). catch (() => { interaction . channel . send ( newtext ); });
2022-05-12 17:38:08 +03:00
}
2022-05-24 08:55:57 +03:00
function econHelp () {
2022-09-27 16:45:50 -04:00
let l = [ "buy" , 'shop' , 'work' , 'rank' , 'inventory' , 'balance' , 'sell' ];
2022-05-24 08:55:57 +03:00
return l . join ( ", " );
}
2022-05-12 17:38:08 +03:00
//#endregion
//Main Code
module . exports = {
2022-05-24 08:55:57 +03:00
name : 'econ' ,
2022-05-12 17:38:08 +03:00
description : 'ECON' ,
2022-09-27 16:45:50 -04:00
async execute ( bot , interaction , Discord , mongouri , items , xp_list ) {
2022-05-12 17:38:08 +03:00
//Set Discord vars
2022-09-27 16:45:50 -04:00
const id = interaction . user . id ;
const server = interaction . guildId ;
const command = interaction . commandName ;
2022-05-12 17:38:08 +03:00
2022-07-31 10:11:55 +03:00
// const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
// if (client.writeConcern || client.writeConcern) {
// client.close();
// return message.reply("Something went wrong with the database, please try again later and contact support if this problem persists!");
// }
2022-05-24 08:55:57 +03:00
2022-07-31 10:11:55 +03:00
bot . mongoconnection . then ( async ( client ) => {
//Initialize if necessary
2022-09-27 16:45:50 -04:00
CreateNewCollection ( interaction , client , server , id );
2022-07-31 10:11:55 +03:00
2022-07-09 20:30:31 +03:00
const db = client . db ( String ( server ));
2022-05-12 17:38:08 +03:00
const dbo = db . collection ( id );
2022-06-17 16:41:02 +03:00
currencySymbol = bot . currencysymbolmmain ;
2022-09-27 16:45:50 -04:00
/*/test area
2022-05-24 08:55:57 +03:00
if (command == 'xp' || command == 'adbal') {
//Selmer Dev only command
if (message.member.roles.cache.has('944048889038774302')) {
if (command == 'xp') {
return addxp(message, dbo, Number(args[0]), xp_list);
2022-05-12 17:38:08 +03:00
}
}
2022-09-27 16:45:50 -04:00
}*/
2022-05-12 17:38:08 +03:00
2022-05-24 08:55:57 +03:00
//Command Area
if ( command == 'init' ) {
//Add security check here
// init.execute(bot, message, args, command, dbo, Discord, connect);
return ;
} else if ( command == 'buy' ) {
2022-09-27 16:45:50 -04:00
buy ( id , interaction , dbo , items , xp_list );
2022-05-24 08:55:57 +03:00
} else if ( command == 'shop' ) {
2022-09-27 16:45:50 -04:00
getShop ( interaction , items , bot );
2022-05-24 08:55:57 +03:00
} else if ( command == 'work' ) {
2022-09-27 16:45:50 -04:00
work ( dbo , interaction , xp_list );
2022-05-24 08:55:57 +03:00
} else if ( command == 'rank' ) {
2022-09-27 16:45:50 -04:00
rank ( dbo , interaction , xp_list );
2022-05-24 08:55:57 +03:00
} else if ( command == 'inventory' ) {
2022-09-27 16:45:50 -04:00
printInventory ( dbo , interaction );
2022-05-24 08:55:57 +03:00
} else if ( command == 'balance' ) {
2022-09-27 16:45:50 -04:00
getBalance ( dbo , interaction );
2022-05-24 08:55:57 +03:00
} else if ( command == 'sell' ) {
2022-09-27 16:45:50 -04:00
sell ( id , interaction , dbo , items , xp_list );
2022-05-24 08:55:57 +03:00
} else {
2022-09-27 16:45:50 -04:00
interaction . reply ( ` ${ command } is not a command` ). catch (( err ) => {
interaction . channel . send ( ` ${ command } is not a command` );
});
2022-05-24 08:55:57 +03:00
}
2022-05-12 17:38:08 +03:00
});
2022-05-24 08:55:57 +03:00
},
//Battle Updating stuff
2022-11-18 11:01:15 -05:00
addxp , checkAndUpdateBal , CreateNewCollection , econHelp , BASE , STATE ,
2022-09-27 16:45:50 -04:00
options : []
}