mirror of
https://github.com/ION606/selmerBot.git
synced 2026-05-14 21:26:54 +00:00
Added the framework for the 'game battle --> item' command, fixed the 'buy' command, changed the currency symbol, added the 'game equip' command, added extra fields to the 'items --> wepaons category
This commit is contained in:
+63
-16
@@ -3,6 +3,7 @@ const { MessageActionRow, MessageButton, MessageSelectMenu } = require('discord.
|
||||
const { STATE } = require('./econ');
|
||||
const { winGame, getCustomEmoji } = require('./external_game_functions.js');
|
||||
const { changeTurn } = require('../turnManager.js');
|
||||
const { default: mongoose } = require('mongoose');
|
||||
|
||||
|
||||
function postActionBar(thread, user_dbo) {
|
||||
@@ -39,7 +40,7 @@ function attack_special() {
|
||||
|
||||
|
||||
//Bow special phrase: Σ>―(´・ω・`)→
|
||||
function attack(client, user_dbo, other_dbo, bot, thread, command, mongouri, items, xp_collection, interaction) {
|
||||
function attack(client, user_dbo, other_dbo, bot, thread, xp_collection, interaction) {
|
||||
//Get the weapon
|
||||
user_dbo.find({'equipped': {$exists: true}}).toArray(function(err, docs) {
|
||||
const doc = docs[0];
|
||||
@@ -53,7 +54,7 @@ function attack(client, user_dbo, other_dbo, bot, thread, command, mongouri, ite
|
||||
dmg = doc.rank;
|
||||
} else {
|
||||
dmg = (doc.rank - 1) + Math.round(weapon.cost/5);
|
||||
}
|
||||
}
|
||||
|
||||
other_dbo.find({'equipped': {$exists: true}}).toArray(function (err, docs) {
|
||||
const odoc = docs[0];
|
||||
@@ -71,7 +72,7 @@ function attack(client, user_dbo, other_dbo, bot, thread, command, mongouri, ite
|
||||
if (new_hp <= 0) {
|
||||
winGame(client, bot, client.db(user_dbo.s.namespace.db), user_dbo, xp_collection, interaction.message);
|
||||
} else {
|
||||
other_dbo.updateOne({'equipped': {$exists: true}}, { $set: { 'hpmp.hp' :new_hp }});
|
||||
other_dbo.updateOne({'equipped': {$exists: true}}, { $set: { 'hpmp.hp' :new_hp, state: STATE.FIGHTING }});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -85,13 +86,13 @@ function attack(client, user_dbo, other_dbo, bot, thread, command, mongouri, ite
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called by "item"
|
||||
*/
|
||||
|
||||
async function heal(interaction, client, user_dbo, bot, thread, command, mongouri, items) {
|
||||
if (interaction.message.content.toLowerCase().indexOf('Which item would you like to use?') != -1) {
|
||||
// The person picked out an item
|
||||
//I think this is unecessary
|
||||
}
|
||||
|
||||
//Get the 'healing' items (stored in "{item}: num" format)
|
||||
user_dbo.find({'equipped': {$exists: true}}).toArray(async function(err, docs) {
|
||||
const doc = docs[0];
|
||||
@@ -100,8 +101,8 @@ async function heal(interaction, client, user_dbo, bot, thread, command, mongour
|
||||
|
||||
|
||||
if (JSON.stringify(items) == '[]') {
|
||||
postActionBar(thread, user_dbo);
|
||||
return interaction.editReply("You don't have any items!");
|
||||
interaction.editReply("You don't have any items!");
|
||||
return postActionBar(thread, user_dbo);
|
||||
} else { console.log(JSON.stringify(items))}
|
||||
|
||||
var itemlist = [];
|
||||
@@ -142,22 +143,66 @@ async function heal(interaction, client, user_dbo, bot, thread, command, mongour
|
||||
|
||||
|
||||
//Gets items by section/name, reacts with them to the message, when pressed, trigger a response
|
||||
function item() {
|
||||
throw 'THE "ITEM" COMMAND HAS NOT BEEN SET UP YET!';
|
||||
function presentItems(interaction, client, user_dbo, bot, thread) {
|
||||
// throw 'THE "ITEM" COMMAND HAS NOT BEEN SET UP YET!';
|
||||
user_dbo.find({'equipped': {$exists: true}}).toArray(async function(err, docs) {
|
||||
const doc = docs[0];
|
||||
const items = doc.equipped.items;
|
||||
// const items = rawitems.filter(function(f) { return (f.sect.toLowerCase() == 'hp') });
|
||||
|
||||
|
||||
if (JSON.stringify(items) == '[]' || JSON.stringify(items) == '{}') {
|
||||
interaction.editReply("You don't have any items!");
|
||||
return postActionBar(thread, user_dbo);
|
||||
} else { console.log(JSON.stringify(items))}
|
||||
|
||||
var itemlist = [];
|
||||
|
||||
items.forEach(function(item) {
|
||||
let n = item.name;
|
||||
|
||||
|
||||
|
||||
itemlist.push({label: n, description: `${item.num} equipped!`, value: `${n}`});
|
||||
});
|
||||
|
||||
|
||||
//Find something to heal with
|
||||
const row = new MessageActionRow()
|
||||
.addComponents(
|
||||
new MessageSelectMenu()
|
||||
.setCustomId(`${interaction.user.id}|heal`)
|
||||
.setPlaceholder('Nothing selected')
|
||||
.addOptions(itemlist)
|
||||
);
|
||||
|
||||
await interaction.editReply({ content: 'Please choose a health potion!', components: [row] });
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function defend(user_dbo, bot, thread, command, mongouri, items) {
|
||||
function defend(client, interaction, user_dbo, bot, thread) {
|
||||
user_dbo.find({'equipped': {$exists: true}}).toArray(function(err, docs) {
|
||||
const doc = docs[0];
|
||||
const all_weapons = doc.get('weapons');
|
||||
const shield = all_weapons.get('secondary');
|
||||
|
||||
//They don't have a shield
|
||||
if (shield == undefined) {
|
||||
thread.send("You don't have a shield equipped!");
|
||||
return postActionBar(thread, user_dbo);
|
||||
}
|
||||
|
||||
//Change state
|
||||
user_dbo.updateOne({state: {$exists: true}}, {$set: {state: STATE.DEFENDING}});
|
||||
})
|
||||
|
||||
changeTurn(client, bot, interaction);
|
||||
postActionBar(thread, user_dbo);
|
||||
}
|
||||
|
||||
|
||||
function usePotion(interaction, client, user_dbo, bot, thread, command, mongouri) {
|
||||
function usePotion(interaction, client, user_dbo, bot, thread) {
|
||||
const name = interaction.values[0];
|
||||
const cursor = user_dbo.find({'equipped.items': {$exists: true}});
|
||||
|
||||
@@ -176,7 +221,7 @@ function usePotion(interaction, client, user_dbo, bot, thread, command, mongouri
|
||||
//If there's more than 1, subtract 1
|
||||
if (items.num > 1) { items.num -= 1; allitems[ind] = items; }
|
||||
else { allitems.splice(ind, 1) }
|
||||
|
||||
|
||||
user_dbo.updateOne({'equipped.items': {$exists: true}}, {$set: {'equipped.items': allitems}});
|
||||
})
|
||||
|
||||
@@ -196,14 +241,16 @@ async function handle(client, user_dbo, other_dbo, bot, thread, command, mongour
|
||||
if (command == 'initalize') {
|
||||
return postActionBar(thread, user_dbo);
|
||||
} else if (command == 'attack') {
|
||||
attack(client, user_dbo, other_dbo, bot, thread, command, mongouri, items, xp_collection, interaction);
|
||||
attack(client, user_dbo, other_dbo, bot, thread, xp_collection, interaction);
|
||||
postActionBar(thread, other_dbo);
|
||||
} else if (command == 'items') {
|
||||
item();
|
||||
presentItems(interaction, client, user_dbo, bot, thread);
|
||||
} else if (command == 'heal') {
|
||||
heal(interaction, client, user_dbo, bot, thread, command, mongouri, items); //.then(() => {postActionBar(thread, other_dbo)});
|
||||
} else if (command == 'usepotion') {
|
||||
usePotion(interaction, client, user_dbo, bot, thread, command, mongouri);
|
||||
usePotion(interaction, client, user_dbo, bot, thread);
|
||||
} else if (command == 'defend') {
|
||||
defend(client, interaction, user_dbo, bot, thread);
|
||||
}
|
||||
|
||||
// initiate(user_dbo, other_dbo, command, message);
|
||||
|
||||
+50
-37
@@ -2,6 +2,9 @@ const { MongoClient, ServerApiVersion } = require('mongodb');
|
||||
// const { update } = require('apt');
|
||||
const { Collection, Client, Formatters, Intents } = require('discord.js');
|
||||
const { CLIENT_ODBC } = require('mysql/lib/protocol/constants/client');
|
||||
const { time } = require('@discordjs/builders');
|
||||
|
||||
let currencySymbol = '$';
|
||||
|
||||
//Declair an "enum" to help with BASE calculations
|
||||
const BASE = {
|
||||
@@ -91,7 +94,11 @@ function addxp(message, dbo, amt, xp_list) {
|
||||
|
||||
function getBalance(dbo, message) {
|
||||
dbo.find({"balance": {$exists: true}}).toArray(function(err, doc) {
|
||||
return message.reply('Your current balance is $' + String(doc[0].balance));
|
||||
let bal = 0;
|
||||
if (doc[0] && doc[0].balance) {
|
||||
bal = doc[0].balance;
|
||||
}
|
||||
return message.reply(`Your current balance is ${currencySymbol}${bal}`);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -114,26 +121,25 @@ function convertCurrency(id, amt, dbo) {
|
||||
}
|
||||
|
||||
function checkAndUpdateBal(dbo, item, message, args) {
|
||||
let b = false;
|
||||
dbo.find({"balance": {$exists: true}}).toArray(b = function(err, doc) {
|
||||
if (!String(doc)) {
|
||||
message.reply("Your account doesn't exist, please contact the mods for support");
|
||||
return false;
|
||||
}
|
||||
return new Promise(function(resolve, reject) {
|
||||
dbo.find({"balance": {$exists: true}}).toArray(b = function(err, doc) {
|
||||
if (!String(doc)) {
|
||||
message.reply("Your account doesn't exist, please contact the mods for support");
|
||||
return false;
|
||||
}
|
||||
|
||||
const icost = args[0] * item.cost;
|
||||
if (doc[0].balance < icost) {
|
||||
message.reply("Insufficient funds!");
|
||||
return false;
|
||||
} else {
|
||||
let temp = doc[0];
|
||||
dbo.updateOne({balance: temp.balance, rank: temp.rank, lastdayworked: temp.lastdayworked}, { $set: { balance: doc[0].balance -= icost }});
|
||||
message.reply("You have bought " + item.name + " for $" + icost + "!");
|
||||
return true;
|
||||
}
|
||||
const icost = args[0] * item.cost;
|
||||
if (doc[0].balance < icost) {
|
||||
message.reply("Insufficient funds!");
|
||||
resolve(false);
|
||||
} else {
|
||||
let temp = doc[0];
|
||||
dbo.updateOne({balance: temp.balance, rank: temp.rank, lastdayworked: temp.lastdayworked}, { $set: { balance: doc[0].balance -= icost }});
|
||||
message.reply(`You have bought ${item.name} for ${currencySymbol}${icost}!`);
|
||||
resolve(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
@@ -142,25 +148,29 @@ function buy(id, message, args, dbo, shop, xp_list) {
|
||||
if (!isNum(args[0])) { return message.reply("Please enter a number for query 2"); }
|
||||
|
||||
let query = args[1];
|
||||
let item = shop.filter(function (item) { return item.name.toLowerCase() == query.toLowerCase(); });
|
||||
let item = shop.filter(function (item) { return item.name.toLowerCase() == query.toLowerCase(); })[0];
|
||||
|
||||
if (!String(item)) { return message.reply("This item does not exist!"); }
|
||||
|
||||
let success = Boolean(checkAndUpdateBal(dbo, item[0], message, args));
|
||||
if (!success) { return; }
|
||||
// let success = Boolean(checkAndUpdateBal(dbo, item, message, args));
|
||||
checkAndUpdateBal(dbo, item, message, args).then((success) => {
|
||||
if (!success) { return } //The message is handled in the CheckAndUpdateBal() function
|
||||
|
||||
var newObj = { name: item[0].name, cost: item[0].cost, icon: item[0].icon, sect: item[0].sect};
|
||||
var newObj = { name: item.name, cost: item.cost, icon: item.icon, sect: item.sect};
|
||||
|
||||
addxp(message, dbo, Math.ceil(item[0].cost * 1.2), xp_list);
|
||||
|
||||
dbo.find(newObj, {$exists: true}).toArray(function(err, doc) {
|
||||
if(String(doc)) {
|
||||
let newnum = doc[0].num + Number(args[0]);
|
||||
dbo.updateOne({ name: item[0].name }, {$set: {num: newnum}});
|
||||
} else {
|
||||
dbo.insertOne({ name: item[0].name, cost: item[0].cost, icon: item[0].icon, sect: item[0].sect, num: Number(args[0])});
|
||||
}
|
||||
});
|
||||
addxp(message, dbo, Math.ceil(item.cost * 1.2), xp_list);
|
||||
|
||||
dbo.find(newObj, {$exists: true}).toArray(function(err, doc) {
|
||||
if(String(doc)) {
|
||||
let newnum = doc[0].num + Number(args[0]);
|
||||
dbo.updateOne({ name: item.name }, {$set: {num: newnum}});
|
||||
} else {
|
||||
// dbo.insertOne({ name: item.name, cost: item.cost, icon: item.icon, sect: item.sect, num: Number(args[0])}); //Causes "cyclic dependancy"
|
||||
dbo.insertOne(item);
|
||||
dbo.updateOne(item, { $set: {num: Number(args[0]) }});
|
||||
}
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
|
||||
@@ -201,7 +211,7 @@ function sell(id, message, args, dbo, shop, xp_list) {
|
||||
|
||||
addxp(message, dbo, Math.ceil(functional_item.cost * 1.2), xp_list);
|
||||
|
||||
message.reply(`You've sold ${num} ${String(functional_item.name)} for $${amountSoldFor}`);
|
||||
message.reply(`You've sold ${num} ${String(functional_item.name)} for ${currencySymbol}${amountSoldFor}`);
|
||||
} else {
|
||||
message.reply("You don't own this item!");
|
||||
}
|
||||
@@ -225,7 +235,7 @@ function work(dbo, message, xp_list) {
|
||||
//Update the amount to the new TOTAL balance
|
||||
dbo.updateOne({"balance": {$exists: true}}, { $set: { balance: doc[0].balance + amt, lastdayworked: date }});
|
||||
addxp(message, dbo, xp_earned, xp_list);
|
||||
message.channel.send('<@' + message.author.id + '> worked and earned $' + amt +' and ' + String(xp_earned) + ' xp!');
|
||||
message.channel.send(`<@${message.author.id}> worked and earned ${currencySymbol}${amt} and ${xp_earned} xp!`);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -235,10 +245,11 @@ function printInventory(dbo, message) {
|
||||
let tempstring = "";
|
||||
dbo.find().toArray(function(err, docs){
|
||||
docs.forEach(val => {
|
||||
if (!val.balance) {
|
||||
if (!val.balance && val.name != undefined) {
|
||||
tempstring += String(val.num) + " " + val.name + " (" + val.icon + ")\n";
|
||||
}
|
||||
});
|
||||
|
||||
if (tempstring == "") { tempstring += "You have nothing in your inventory!"; }
|
||||
message.reply(tempstring);
|
||||
});
|
||||
@@ -266,7 +277,7 @@ function getShop(message, args, items, bot) {
|
||||
}
|
||||
|
||||
const items2 = items.filter(function(f) { return (f.sect.toLowerCase() == args[0].toLowerCase()) }).slice((ind - 1)*10, (ind - 1)*10+10);
|
||||
newText = Formatters.codeBlock(items2.map(i => `${i.icon} (${i.name}): \$${i.cost}`).join('\n'));
|
||||
newText = Formatters.codeBlock(items2.map(i => `${i.icon} (${i.name}): $${i.cost}`).join('\n')); //${currencySymbol} doesn't owrk for some reason
|
||||
|
||||
if (noinp) {
|
||||
newText += `(Use ${bot.prefix}shop [type] [page number] to access other pages)`;
|
||||
@@ -305,6 +316,8 @@ module.exports = {
|
||||
const dbo = db.collection(id);
|
||||
if (err) { return console.log(err); }
|
||||
|
||||
currencySymbol = bot.currencysymbolmmain;
|
||||
|
||||
//test area
|
||||
if (command == 'xp' || command == 'adbal') {
|
||||
//Selmer Dev only command
|
||||
|
||||
@@ -11,10 +11,11 @@ function loseGame(user_dbo, xp_collection, message, bot = null) {
|
||||
if (doc == undefined) { return message.reply("Oops! There's been an error! Please contact support if this problem persists!"); }
|
||||
if (doc.game == null) { return message.reply("You're not even in a game and you're trying to quit! Sad..."); }
|
||||
|
||||
var addbal;
|
||||
//If this function was called from "winGame", return
|
||||
if (doc.opponent) {
|
||||
//If remove some money (looting) [maybe implement a "friendly" game setting later with no looting]
|
||||
var addbal = doc.rank * 2;
|
||||
addbal = doc.rank * 2;
|
||||
if (doc.balance - addbal < 5) { addbal = addbal - doc.balance; }
|
||||
if (doc.balance > 5) {
|
||||
user_dbo.updateOne(doc, { $set: { balance: doc.balance - addbal}});
|
||||
@@ -60,12 +61,17 @@ function winGame(client, bot, db, user_dbo, xp_collection, message) {
|
||||
|
||||
|
||||
function equipItem(client, bot, db, dbo, message) {
|
||||
|
||||
if (!bot.inDebugMode) { return; }
|
||||
let items = [
|
||||
{ name: 'HP Potion', cost: 20, icon: 'CUSTOM|healing_potion', sect: 'HP', num: 2 },
|
||||
{ name: 'Super HP Potion', cost: 50, icon: 'CUSTOM|super_healing_potion', sect: 'HP', num: 2 },
|
||||
{ name: 'MP Potion', cost: 15, icon: 'CUSTOM|mana_potion', sect: 'MP', num: 2 }
|
||||
]
|
||||
for (let i = 1; i <= 10; i ++) {
|
||||
|
||||
items.push({ name: `${String.fromCharCode(i + 64)}`, cost: i * 10, icon: 'N/A', sect: 'N/A', num: i })
|
||||
}
|
||||
|
||||
dbo.updateMany({}, {$set: {'equipped.items': items}});
|
||||
}
|
||||
|
||||
+47
-6
@@ -1,4 +1,4 @@
|
||||
// @ts-check
|
||||
// // @ts-check //Disabled
|
||||
|
||||
const { MongoClient, ServerApiVersion } = require('mongodb');
|
||||
let ecoimport = require("./econ.js");
|
||||
@@ -130,15 +130,52 @@ function hpmp(message, command, dbo) {
|
||||
}
|
||||
|
||||
|
||||
function equip(client, message, command, dbo, bot) {
|
||||
function equip(message, args, command, dbo, bot, shop) {
|
||||
const inp = args[1];
|
||||
|
||||
//Check if the user is already in a game
|
||||
|
||||
dbo.find({'game': {$exists: true}}).toArray(function(err, docs) {
|
||||
const doc = docs[0];
|
||||
|
||||
if (doc.game != null) {
|
||||
ret = true;
|
||||
console.log(doc.game);
|
||||
return message.reply('You can\'t equip while in a game!');
|
||||
}
|
||||
|
||||
//If the thing is a shield, add it to secondary
|
||||
if (inp.toLowerCase().indexOf('shield') != -1) {
|
||||
dbo.find({def: true}).toArray(function(err, docs) {
|
||||
if (docs[0] != undefined) {
|
||||
dbo.updateOne({}, {$set: {'equipped.weapons.secondary': docs[0]}});
|
||||
} else {
|
||||
message.reply("You don't own a shield!");
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
//Else, equip the weapon(s)
|
||||
|
||||
dbo.find({name: inp, sect: 'Weapons'}).toArray(function(err, docs) {
|
||||
if (docs[0] != undefined) {
|
||||
//Equip the weapon
|
||||
dbo.updateOne({}, {$set: {'equipped.weapons.main': docs[0]}});
|
||||
} else {
|
||||
message.reply(`You don't own any ${inp}s!`);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
|
||||
//#region GAME SPECIFIC
|
||||
|
||||
|
||||
//#region Game Handlers
|
||||
|
||||
function in_game_redirector(bot, interaction, threadname, doc, client, mongouri, items, xp_collection) {
|
||||
|
||||
//Maybe fix this later......
|
||||
@@ -276,7 +313,8 @@ module.exports ={
|
||||
} else if (command == 'hp' || command == 'mp') {
|
||||
hpmp(message, command, dbo);
|
||||
} else if (command == 'equip') {
|
||||
equipItem(client, bot, db, dbo, message);
|
||||
// equipItem(client, bot, db, dbo, message);
|
||||
equip(message, args, command, dbo, bot, items);
|
||||
}
|
||||
//#endregion
|
||||
|
||||
@@ -305,4 +343,7 @@ module.exports ={
|
||||
|
||||
client.close();
|
||||
}, allGames, in_game_redirector
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//#endregion
|
||||
Reference in New Issue
Block a user