mirror of
https://github.com/ION606/selmerBot.git
synced 2026-06-06 07:12:58 +00:00
Added the 'Econ/sell' command
This commit is contained in:
+46
-17
@@ -1,6 +1,7 @@
|
|||||||
const { MongoClient, ServerApiVersion } = require('mongodb');
|
const { MongoClient, ServerApiVersion } = require('mongodb');
|
||||||
// const { update } = require('apt');
|
// const { update } = require('apt');
|
||||||
const { Collection, Client, Formatters, Intents } = require('discord.js');
|
const { Collection, Client, Formatters, Intents } = require('discord.js');
|
||||||
|
const { CLIENT_ODBC } = require('mysql/lib/protocol/constants/client');
|
||||||
const BASE_PAY = 5;
|
const BASE_PAY = 5;
|
||||||
const BASE_LVL_XP = 35;
|
const BASE_LVL_XP = 35;
|
||||||
//Note that leveling up to the next level takes 10% more xp than the previous one
|
//Note that leveling up to the next level takes 10% more xp than the previous one
|
||||||
@@ -42,6 +43,13 @@ 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));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function rank(dbo, message, xp_list) {
|
function rank(dbo, message, xp_list) {
|
||||||
dbo.find({"balance": {$exists: true}}).toArray(function(err, doc) {
|
dbo.find({"balance": {$exists: true}}).toArray(function(err, doc) {
|
||||||
if (!String(doc)) { return console.log("ERROR!\nThis account does not exist!"); }
|
if (!String(doc)) { return console.log("ERROR!\nThis account does not exist!"); }
|
||||||
@@ -95,10 +103,10 @@ function buy(id, message, args, dbo, shop, xp_list) {
|
|||||||
var newObj = { name: item[0].name, cost: item[0].cost, icon: item[0].icon, sect: item[0].sect};
|
var newObj = { name: item[0].name, cost: item[0].cost, icon: item[0].icon, sect: item[0].sect};
|
||||||
|
|
||||||
addxp(message, dbo, Math.ceil(item[0].cost * 1.2), xp_list);
|
addxp(message, dbo, Math.ceil(item[0].cost * 1.2), xp_list);
|
||||||
|
|
||||||
dbo.find(newObj, {$exists: true}).toArray(function(err, doc) {
|
dbo.find(newObj, {$exists: true}).toArray(function(err, doc) {
|
||||||
if(String(doc)) {
|
if(String(doc)) {
|
||||||
let newnum = doc[0].num + Number(args[0]);
|
let newnum = doc[0].num + Number(args[0]);
|
||||||
console.log(newnum);
|
|
||||||
dbo.updateOne({ name: item[0].name }, {$set: {num: newnum}});
|
dbo.updateOne({ name: item[0].name }, {$set: {num: newnum}});
|
||||||
} else {
|
} else {
|
||||||
dbo.insertOne({ name: item[0].name, cost: item[0].cost, icon: item[0].icon, sect: item[0].sect, num: Number(args[0])});
|
dbo.insertOne({ name: item[0].name, cost: item[0].cost, icon: item[0].icon, sect: item[0].sect, num: Number(args[0])});
|
||||||
@@ -107,20 +115,44 @@ function buy(id, message, args, dbo, shop, xp_list) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function sell(id, message, args, dbo, shop) {
|
function sell(id, message, args, dbo, shop, xp_list) {
|
||||||
if (args.length < 2) { return; }
|
if (args.length < 2) { return; }
|
||||||
if (!isNum(args[0])) { return message.reply("Please enter a number for query 2"); }
|
if (!isNum(args[0])) { return message.reply("Please enter a number for query 1"); }
|
||||||
var newObj = { name: args[1] };
|
|
||||||
|
|
||||||
let query = args[1];
|
let query = args[1];
|
||||||
let item = shop.filter(function (item) { return item.name.toLowerCase() == query.toLowerCase(); });
|
var newObj = { name: query };
|
||||||
if (!String(item)) { return message.reply("This item does not exist!"); }
|
|
||||||
if (!success) { return; }
|
|
||||||
|
|
||||||
dbo.find(newObj, {$exists: true}).toArray(function(err, doc) {
|
let item = shop.filter(function (titem) { return titem.name.toLowerCase() == query.toLowerCase(); });
|
||||||
return console.log(String(doc));
|
if (!String(item)) { return message.reply("This item does not exist!"); }
|
||||||
|
|
||||||
|
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) {
|
||||||
if(String(doc)) {
|
if(String(doc)) {
|
||||||
dbo.updateOne({ name: args[1] }, {$set: {num: doc[0].num + Number(args[0])}});
|
|
||||||
|
//Make sure you don't sell more than you have
|
||||||
|
let num = Number(args[0]);
|
||||||
|
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 }});
|
||||||
|
});
|
||||||
|
|
||||||
|
addxp(message, dbo, Math.ceil(functional_item.cost * 1.2), xp_list);
|
||||||
|
|
||||||
|
message.reply(`You've sold ${num} ${String(functional_item.name)} for $${amountSoldFor}`);
|
||||||
} else {
|
} else {
|
||||||
message.reply("You don't own this item!");
|
message.reply("You don't own this item!");
|
||||||
}
|
}
|
||||||
@@ -155,18 +187,12 @@ function printInventory(dbo, message) {
|
|||||||
tempstring += String(val.num) + " " + val.name + " (" + val.icon + ")\n";
|
tempstring += String(val.num) + " " + val.name + " (" + val.icon + ")\n";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if (tempstring == "") { tempstring += "You have nothing in your inventory!"; }
|
||||||
message.reply(tempstring);
|
message.reply(tempstring);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function getBalance(dbo, message) {
|
|
||||||
dbo.find({"balance": {$exists: true}}).toArray(function(err, doc) {
|
|
||||||
message.reply('Your current balance is $' + String(doc[0].balance));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function getShop(message, args, items) {
|
function getShop(message, args, items) {
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
let temp = Formatters.codeBlock(items.map(i => `${i.sect}`).join(' '));
|
let temp = Formatters.codeBlock(items.map(i => `${i.sect}`).join(' '));
|
||||||
@@ -210,6 +236,7 @@ module.exports = {
|
|||||||
const server = message.guild.id;
|
const server = message.guild.id;
|
||||||
|
|
||||||
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
|
||||||
|
if (client.writeConcern || client.writeConcern) { return client.close(); }
|
||||||
client.connect(err => {
|
client.connect(err => {
|
||||||
const db = client.db(String(server) + "[ECON]");
|
const db = client.db(String(server) + "[ECON]");
|
||||||
const dbo = db.collection(id);
|
const dbo = db.collection(id);
|
||||||
@@ -253,6 +280,8 @@ module.exports = {
|
|||||||
printInventory(dbo, message);
|
printInventory(dbo, message);
|
||||||
} else if (command == 'balance') {
|
} else if (command == 'balance') {
|
||||||
getBalance(dbo, message);
|
getBalance(dbo, message);
|
||||||
|
} else if (command == 'sell') {
|
||||||
|
sell(id, message, args, dbo, items, xp_list);
|
||||||
} else {
|
} else {
|
||||||
message.channel.send("'" + message.content + "' is not a command!");
|
message.channel.send("'" + message.content + "' is not a command!");
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+4
-2
@@ -1,5 +1,5 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'r',
|
name: 'react',
|
||||||
description: "Reacts with a phrase or single emoji",
|
description: "Reacts with a phrase or single emoji",
|
||||||
async execute(message, args, Discord, Client, bot) {
|
async execute(message, args, Discord, Client, bot) {
|
||||||
|
|
||||||
@@ -8,9 +8,11 @@ module.exports = {
|
|||||||
if (message.reference) {
|
if (message.reference) {
|
||||||
msg = await message.channel.messages.fetch(message.reference.messageId);
|
msg = await message.channel.messages.fetch(message.reference.messageId);
|
||||||
} else { msg = message; }
|
} else { msg = message; }
|
||||||
|
//Get rid of any custom emojis
|
||||||
|
console.log("IMPLEMENT THIS");
|
||||||
|
|
||||||
let emoji = [...new Set(args[0])];
|
let emoji = [...new Set(args[0])];
|
||||||
if (emoji.length > 15) { return message.reply("Please enter less than 15 emojis"); }
|
if (emoji.length > 15 || message.IndexOf(":") != -1) { return message.reply("Please enter less than 15 emojis"); }
|
||||||
let notused = new Array(15);
|
let notused = new Array(15);
|
||||||
let counter = 0;
|
let counter = 0;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user