Added the 'heal' command and potion functionionality. It doesn't work for the opponent for some reason

This commit is contained in:
ION606
2022-06-11 17:53:44 +03:00
parent 5bdb93be68
commit b432d7a4ed
6 changed files with 209 additions and 54 deletions
+8 -1
View File
@@ -134,7 +134,14 @@ client.connect(err => {
{ name: 'Shield', cost: 100, icon: '🛡', sect: 'Weapons' }, { name: 'Shield', cost: 100, icon: '🛡', sect: 'Weapons' },
{ name: 'Axe', cost: 40, icon: '🪓', sect: 'Weapons' }, { name: 'Axe', cost: 40, icon: '🪓', sect: 'Weapons' },
{ name: 'Trident', cost: 140, icon: '🔱', sect: 'Weapons' }, { name: 'Trident', cost: 140, icon: '🔱', sect: 'Weapons' },
{ name: 'Scissors', cost: 10, icon: '✂️', sect: 'Weapons' } { name: 'Scissors', cost: 10, icon: '✂️', sect: 'Weapons' },
//Potions (of varying sections)
{ name: 'HP Potion', cost: 20, icon: 'CUSTOM|healing_potion', sect: 'HP' },
{ name: 'MP Potion', cost: 15, icon: 'CUSTOM|mana_potion', sect: 'MP' },
{ name: 'Super HP Potion', cost: 50, icon: 'CUSTOM|superior_healing_potion', sect: 'HP' },
{ name: 'Super MP Potion', cost: 40, icon: 'CUSTOM|superior_mana_potion', sect: 'MP' }
]); ]);
}); });
+5 -2
View File
@@ -41,11 +41,14 @@ async function welcome(member, welcomechannel, welcomemessage = null, welcomeban
// Declare a base size of the font // Declare a base size of the font
let fontSize = 70; let fontSize = 70;
let i = 0;
do { do {
// Assign the font to the context and decrement it so it can be measured again // Assign the font to the context and decrement it so it can be measured again
context.font = `italic ${fontSize -= 10}px sans-serif`; context.font = `italic ${fontSize -= 10}px sans-serif`;
// Compare pixel width of the text to the canvas minus the approximate avatar size // Compare pixel width of the text to the canvas minus the approximate avatar size
i ++;
} while (context.measureText(text).width > canvas.width - 100); } while (context.measureText(text).width > canvas.width - 100);
// Return the result to use in the actual canvas // Return the result to use in the actual canvas
@@ -62,9 +65,9 @@ async function welcome(member, welcomechannel, welcomemessage = null, welcomeban
text = text.replace('{ut}', member.user.discriminator); text = text.replace('{ut}', member.user.discriminator);
} }
context.font = applyText(canvas, text); context.font= applyText(canvas, text);
context.fillStyle = '#ffffff'; context.fillStyle = '#ffffff';
context.fillText(text, (canvas.width/2) - (text.length * 7.5), canvas.height - 15); context.fillText(text, (canvas.width/2) - (context.measureText(text).width)/2, canvas.height - 15);
//Draw a white circle //Draw a white circle
+95 -45
View File
@@ -1,10 +1,35 @@
//@ts-check //@ts-check
const { MessageActionRow, MessageButton, MessageSelectMenu } = require('discord.js'); const { MessageActionRow, MessageButton, MessageSelectMenu } = require('discord.js');
const { STATE } = require('./econ'); const { STATE } = require('./econ');
const { winGame } = require('./external_game_functions.js'); const { winGame, getCustomEmoji } = require('./external_game_functions.js');
const { changeTurn } = require('../turnManager.js'); const { changeTurn } = require('../turnManager.js');
function postActionBar(thread, user_dbo) {
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('ATTACK')
.setLabel('ATTACK')
.setStyle('DANGER'),
new MessageButton()
.setCustomId('HEAL')
.setLabel('HEAL')
.setStyle('SUCCESS'),
new MessageButton()
.setCustomId('DEFEND')
.setLabel('DEFEND')
.setStyle('PRIMARY'),
new MessageButton()
.setCustomId('ITEMS')
.setLabel('ITEMS')
.setStyle('SECONDARY')
);
thread.send({ content: `Your turn <@${user_dbo.s.namespace.collection}>!`, components: [row] });
}
/** /**
* Called by "attack" * Called by "attack"
*/ */
@@ -63,7 +88,7 @@ function attack(client, user_dbo, other_dbo, bot, thread, command, mongouri, ite
/** /**
* Called by "item" * Called by "item"
*/ */
async function heal(interaction, user_dbo, bot, thread, command, mongouri, items) { 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) { if (interaction.message.content.toLowerCase().indexOf('Which item would you like to use?') != -1) {
// The person picked out an item // The person picked out an item
} }
@@ -71,29 +96,47 @@ async function heal(interaction, user_dbo, bot, thread, command, mongouri, items
user_dbo.find({'equipped': {$exists: true}}).toArray(async function(err, docs) { user_dbo.find({'equipped': {$exists: true}}).toArray(async function(err, docs) {
const doc = docs[0]; const doc = docs[0];
const rawitems = doc.equipped.items; const rawitems = doc.equipped.items;
if (JSON.stringify(rawitems) == '{}') { return thread.send("You don't have any items!"); } const items = rawitems.filter(function(f) { return (f.sect.toLowerCase() == 'hp') });
const items = rawitems.filter(function(f) { return (f.sect.toLowerCase() == 'healing') });
if (JSON.stringify(items) == '[]') {
postActionBar(thread, user_dbo);
return interaction.editReply("You don't have any items!");
} else { console.log(JSON.stringify(items))}
var itemlist = [];
items.forEach(function(item) {
let n = item.name;
let h = (doc.rank - 1) + Math.round(item.cost/10);
itemlist.push({label: n, description: `Restores ${h} health (${item.num})`, value: `${n}`});
});
//Find something to heal with //Find something to heal with
const row = new MessageActionRow() const row = new MessageActionRow()
.addComponents( .addComponents(
new MessageSelectMenu() new MessageSelectMenu()
.setCustomId(`${interaction.user.id}|heal`) .setCustomId(`${interaction.user.id}|heal`)
.setPlaceholder('Nothing selected') .setPlaceholder('Nothing selected')
.addOptions([ .addOptions(itemlist),
{ // .addOptions([
label: 'Select me', // {
description: 'This is a description', // label: 'Select me',
value: 'first_option', // description: 'This is a description',
}, // value: 'first_option',
{ // },
label: 'You can select me too', // {
description: 'This is also a description', // label: 'You can select me too',
value: 'second_option', // description: 'This is also a description',
}, // value: 'second_option',
]), // },
// ])
); );
await interaction.reply({ content: 'Pong!', components: [row] }); await interaction.editReply({ content: 'Please choose a health potion!', components: [row] });
}); });
} }
@@ -113,38 +156,43 @@ function defend(user_dbo, bot, thread, command, mongouri, items) {
}) })
} }
function usePotion(interaction, client, user_dbo, bot, thread, command, mongouri) {
const name = interaction.values[0];
const cursor = user_dbo.find({'equipped.items': {$exists: true}});
let doc = cursor.next().then((result) => {
var allitems = Array.from(result.equipped.items);
let items = allitems.filter((it) => { return it.name == name; })[0];
let ind = allitems.findIndex((it) => { return it.name == name; })
//Apply the item's effects
if (name.toLowerCase().indexOf('hp') != -1) {
let h = (result.rank - 1) + Math.round(items.cost/10);
user_dbo.updateOne({"game": {$exists: true}}, { $set: {'hpmp.hp': (result.hpmp.hp + h)}})
}
//Deal with the item itself
//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}});
})
changeTurn(client, bot, interaction);
postActionBar(thread, user_dbo);
}
function cast() { function cast() {
} }
function postActionBar(thread, user_dbo) {
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('ATTACK')
.setLabel('ATTACK')
.setStyle('DANGER'),
new MessageButton()
.setCustomId('HEAL')
.setLabel('HEAL')
.setStyle('SUCCESS'),
new MessageButton()
.setCustomId('DEFEND')
.setLabel('DEFEND')
.setStyle('PRIMARY'),
new MessageButton()
.setCustomId('ITEMS')
.setLabel('ITEMS')
.setStyle('SECONDARY')
);
thread.send({ content: `Your turn <@${user_dbo.s.namespace.collection}>!`, components: [row] }); async function handle(client, user_dbo, other_dbo, bot, thread, command, mongouri, items, interaction, xp_collection) {
}
function handle(client, user_dbo, other_dbo, bot, thread, command, mongouri, items, interaction, xp_collection) {
if (command == 'initalize') { if (command == 'initalize') {
return postActionBar(thread, user_dbo); return postActionBar(thread, user_dbo);
} else if (command == 'attack') { } else if (command == 'attack') {
@@ -153,10 +201,12 @@ function handle(client, user_dbo, other_dbo, bot, thread, command, mongouri, ite
} else if (command == 'items') { } else if (command == 'items') {
item(); item();
} else if (command == 'heal') { } else if (command == 'heal') {
heal(interaction, user_dbo, bot, thread, command, mongouri, items); 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);
} }
// initiate(user_dbo, other_dbo, command, message); // initiate(user_dbo, other_dbo, command, message);
} }
module.exports = { handle } module.exports = { handle, postActionBar }
+21 -1
View File
@@ -59,4 +59,24 @@ function winGame(client, bot, db, user_dbo, xp_collection, message) {
} }
module.exports = { winGame, loseGame } 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 }
]
dbo.updateMany({}, {$set: {'equipped.items': items}});
}
function getCustomEmoji(bot, name) {
let srv = bot.guilds.cache.get(bot.home_server).emojis.cache;
// console.log(srv);
let emj = srv.find((g) => { return g.name == name });
// message.channel.send(`${emj}`);
return `${emj}`;
}
module.exports = { winGame, loseGame, equipItem, getCustomEmoji }
+4 -1
View File
@@ -7,7 +7,7 @@ let snowflake = require("./addons/snowflake.js");
const STATE = ecoimport.STATE; const STATE = ecoimport.STATE;
const BASE = ecoimport.BASE; const BASE = ecoimport.BASE;
const { winGame, loseGame } = require('./external_game_functions.js'); const { winGame, loseGame, equipItem } = require('./external_game_functions.js');
//Has a list of all games (used to change player state) //Has a list of all games (used to change player state)
const allGames = ['battle']; const allGames = ['battle'];
@@ -152,6 +152,7 @@ function in_game_redirector(bot, interaction, threadname, doc, client, mongouri,
dbo.find({'game': {$exists: true}}).toArray(function (err, docs) { dbo.find({'game': {$exists: true}}).toArray(function (err, docs) {
const game = docs[0].game const game = docs[0].game
switch (game) { switch (game) {
case 'battle': handle(client, dbo, other, bot, thread, interaction.customId.toLowerCase(), mongouri, items, interaction, xp_collection); case 'battle': handle(client, dbo, other, bot, thread, interaction.customId.toLowerCase(), mongouri, items, interaction, xp_collection);
} }
@@ -274,6 +275,8 @@ module.exports ={
getGame(message, args, db); getGame(message, args, db);
} else if (command == 'hp' || command == 'mp') { } else if (command == 'hp' || command == 'mp') {
hpmp(message, command, dbo); hpmp(message, command, dbo);
} else if (command == 'equip') {
equipItem(client, bot, db, dbo, message);
} }
//#endregion //#endregion
+76 -4
View File
@@ -12,11 +12,14 @@ const BASE_LVL_XP = 20;
//Adding integration for development mode //Adding integration for development mode
let token; let token;
let IDM = false; let IDM = false;
let home_server;
if (process.env.token != undefined) { if (process.env.token != undefined) {
//Use "setx NAME VALUE" in the local powershell terminal to set //Use "setx NAME VALUE" in the local powershell terminal to set
token = process.env.token; token = process.env.token;
home_server = process.env.home_server;
} else { } else {
token = require('./config.json').token; token = require('./config.json').token;
home_server = require('./config.json').home_server;
IDM = true; IDM = true;
} }
@@ -40,6 +43,7 @@ const prefix = '!';
bot.prefix = new String; bot.prefix = new String;
bot.prefix = prefix; bot.prefix = prefix;
bot.inDebugMode = IDM; bot.inDebugMode = IDM;
bot.home_server = home_server;
//MongoDB integration //MongoDB integration
@@ -152,6 +156,7 @@ bot.on('ready', async () => {
//Button Section //Button Section
bot.on('interactionCreate', async interaction => { bot.on('interactionCreate', async interaction => {
if (interaction.isButton()) { if (interaction.isButton()) {
const battlecommandlist = ['ATTACK', 'HEAL', 'DEFEND', 'ITEMS']; const battlecommandlist = ['ATTACK', 'HEAL', 'DEFEND', 'ITEMS'];
@@ -178,21 +183,76 @@ bot.on('interactionCreate', async interaction => {
} }
//remove the old interation message //remove the old interation message
interaction.message.delete(); await interaction.message.delete();
interaction.editReply(`<@${interaction.user.id}> used _${interaction.customId.toLowerCase()}_!`); if (interaction.customId.toLowerCase() != 'heal') {
interaction.editReply(`<@${interaction.user.id}> used _${interaction.customId.toLowerCase()}_!`);
}
} else { } else {
console.log("It's not your turn!"); console.log("It's not your turn!");
} }
}); });
}); });
} //else ifs here }//else ifs here
}); });
client.close(); client.close();
} }
else if (interaction.isCommand()) { else if (interaction.isSelectMenu()) {
if (interaction.customId.toLowerCase().indexOf('|heal') != -1) {
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
client.connect(err => {
const id = interaction.customId.substring(0, interaction.customId.indexOf('|'))
if (id != interaction.user.id) { return; }
let current_user = turnManager.getTurn(client, bot, interaction);
current_user.then(function(result) {
const doc = result[1];
const threadname = doc.thread;
const dbo = client.db(interaction.guildId + '[ECON]').collection(id);
dbo.find({ 'state': {$exists: true} }).toArray(async function (err, docs) {
if (interaction.user.id == id) {
await interaction.deferReply();
//Check State
if (docs[0].state == STATE.FIGHTING) {
interaction.customId = 'usepotion';
//Do turn stuff
bot.commands.get('game').in_game_redirector(bot, interaction, threadname, doc, client, mongouri, items, xp_collection);
}
/*
let srv = bot.guilds.cache.get(bot.home_server).emojis.cache;
let sname;
if (interaction.customId.toLowerCase() == 'heal' || interaction.customId.toLowerCase() == 'mp') {
if (interaction.values[0] == 'HP Potion') { sname = 'healing_potion' }
else if (interaction.values[0] == 'MP Potion') { sname = 'mana_potion' }
else if (interaction.values[0] == 'Super HP Potion') { sname = 'superior_healing_potion' }
else if (interaction.values[0] == 'Super MP Potion') { sname = 'superior_mana_potion' }
}
// emj = srv.find((g) => { return g.name == sname });
// console.log(sname, srv);*/
interaction.editReply(`<@${interaction.user.id}> used a _${interaction.values[0]}_!`);
//remove the old interation message
await interaction.message.delete();
} else {
console.log("It's not your turn!");
}
});
});
//Get all chars from after "CUSTOM|" to the end of the str
// let name = item.icon.substr(7, item.icon.length - 6);
});
}
} }
}); });
@@ -229,6 +289,9 @@ bot.on('guildMemberAdd', async (member) => {
bot.on('messageCreate', (message) => { bot.on('messageCreate', (message) => {
//Special case, testing server (still need the emojis)
if (!bot.inDebugMode && message.server.id == bot.home_server) { return; }
//COMMAND AREA //COMMAND AREA
//Check if the prefix exists //Check if the prefix exists
if (!message.content.startsWith(prefix) || message.author.bot) return; if (!message.content.startsWith(prefix) || message.author.bot) return;
@@ -249,6 +312,15 @@ bot.on('messageCreate', (message) => {
message.channel.send({ components: [row] }); message.channel.send({ components: [row] });
} }
//TEMP
if (command == 'emj') {
let srv = bot.guilds.cache.get(bot.home_server).emojis.cache;
// console.log(srv);
emj = srv.find((g) => { return g.name == 'healing_potion' });
// console.log(emj); exit();
message.channel.send(`${emj}`);
}
//Check if the user has sufficient permission //Check if the user has sufficient permission
//Performes the command //Performes the command
//Admin section //Admin section