Add 24h growth indicator

This commit is contained in:
Skylar Grant 2023-01-25 23:11:49 -05:00
parent e64fa099c1
commit 82c20dda68
4 changed files with 86 additions and 46 deletions

View File

@ -4,7 +4,7 @@
}, },
"help": { "help": {
"title": "Silvanus Help", "title": "Silvanus Help",
"info": "Silvanus is the ultimate Grow A Tree companion bot! Quickly compare your server's tree to others on the leaderboard with automatic calculation of tree height differences, active growth detection, watering time calculations, and more! Get started with </help:1065346941166297129> and </setup:1065407649363005561>, then check out </compare:1065346941166297128>.\n\nImportant Note: Silvanus is only as up-to-date as your server's Tree and Tallest Trees messages. Make sure to refresh them before refreshing Silvanus' Compare message.\n\nFor the best experience we recommend the use of a single </tree:972648557796524032> and </top trees:1051840665362894950> message, otherwise make sure to run </setup:1065407649363005561> each time you run </compare:1065346941166297128>.", "info": "Silvanus is the ultimate Grow A Tree companion bot! Quickly compare your server's tree to others on the leaderboard with automatic calculation of tree height differences, active growth detection, watering time calculations, and more! Get started with </help:1065346941166297129> and </setup:1065407649363005561>, then check out </compare:1065346941166297128>.\n\nImportant Note: Silvanus is only as up-to-date as your server's Tree and Tallest Trees messages. Make sure to refresh them before refreshing Silvanus' Compare message.\n\nFor the best experience we recommend the use of a single </tree:972648557796524032> and </top trees:1051840665362894950> message, otherwise make sure to run </setup:1065407649363005561> each time you run </compare:1065346941166297128>.\nStatus Indicator: ``[ Active Growth Indicator: 💧 | 24 Hour Observed Growth | Wait Time Between Waters ]``",
"setup": "To begin analyzing your Tree, first you must set up the reference messages.\n\n1. Run </setup:1065407649363005561> in the channel(s) that contain your server's tree and leaderboard messages.\n2. Now simply run </compare:1065346941166297128> where you want your analysis to be visible.", "setup": "To begin analyzing your Tree, first you must set up the reference messages.\n\n1. Run </setup:1065407649363005561> in the channel(s) that contain your server's tree and leaderboard messages.\n2. Now simply run </compare:1065346941166297128> where you want your analysis to be visible.",
"permissions": "At a minimum, Silvanus requires permissions to `Send Messages` and `Send Messages in Threads` if applicable. If Analyzer is given permission to `Manage Messages`, the bot will delete the `.settree` and `.setranks` messages to reduce spam.", "permissions": "At a minimum, Silvanus requires permissions to `Send Messages` and `Send Messages in Threads` if applicable. If Analyzer is given permission to `Manage Messages`, the bot will delete the `.settree` and `.setranks` messages to reduce spam.",
"allCommands": "</compare:1065346941166297128> | </setup:1065407649363005561> | </watertime:1066970330029113444> | </setupinfo:1065413032374706196> | </reset:1065412317052944476> | </help:1065346941166297129>" "allCommands": "</compare:1065346941166297128> | </setup:1065407649363005561> | </watertime:1066970330029113444> | </setupinfo:1065413032374706196> | </reset:1065412317052944476> | </help:1065346941166297129>"

View File

@ -300,5 +300,38 @@ module.exports = {
resolve({ "status": "Successfully uploaded the leaderboard", "data": res }); resolve({ "status": "Successfully uploaded the leaderboard", "data": res });
}); });
}); });
} },
get24hTree(guildId, treeName) {
const db = mysql.createConnection({
host : process.env.DBHOST,
user : process.env.DBUSER,
password : process.env.DBPASS,
database : process.env.DBNAME,
port : process.env.DBPORT
});
db.connect((err) => {
if (err) throw `Error connecting to the database: ${err.message}`;
});
// Returns a Promise, resolve({ "status": "", "data": leaderboard })
const select24hTreeQuery = `SELECT id, tree_name, tree_rank, tree_height, has_pin FROM leaderboard WHERE guild_id = ${db.escape(guildId)} AND tree_name = ${db.escape(treeName)} AND timestamp > date_sub(now(), interval 1 day) ORDER BY id ASC LIMIT 1`;
// TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard }
return new Promise((resolve, reject) => {
db.query(select24hTreeQuery, (err, res) => {
if (err) {
console.error(err);
db.end();
reject("Error fetching the historic 24hr tree height: " + err.message);
return;
}
const hist24hTree = {
"treeName": res[0].tree_name,
"treeRank": res[0].tree_rank,
"treeHeight": res[0].tree_height,
"hasPin": res[0].has_pin
}
db.end();
resolve({ "status": "Successfully fetched historic 24hr tree.", "data": hist24hTree });
});
});
}
}; };

View File

@ -17,6 +17,7 @@ const config = require('../data/config.json');
const strings = require('../data/strings.json'); const strings = require('../data/strings.json');
const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => file.endsWith('.js')); const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => file.endsWith('.js'));
const dbfn = require('./dbfn.js'); const dbfn = require('./dbfn.js');
const { finished } = require('stream');
dbfn.createGuildTables().then(res => { dbfn.createGuildTables().then(res => {
console.log(res.status); console.log(res.status);
@ -173,49 +174,55 @@ const functions = {
}); });
}, },
compare(interaction) { async compare(interaction) {
return new Promise((resolve, reject) => { try {
dbfn.getGuildInfo(interaction.guildId).then(res => { // fetch the guild's settings from the database
const guildInfo = res.data; const guildInfoResponse = await dbfn.getGuildInfo(interaction.guildId);
guildInfo.guildId = interaction.guildId; const guildInfo = guildInfoResponse.data; // { "guildId": "123","treeName": "name","treeHeight": 123,"treeMessageId": "123","treeChannelId": "123","leaderboardMessageId": "123","leaderboardChannelId": "123"};
// Get the most recent leaderboard listing from the database
let treeHeight = parseFloat(guildInfo.treeHeight).toFixed(1); const getLeaderboardResponse = await dbfn.getLeaderboard(interaction.guildId);
dbfn.getLeaderboard(interaction.guildId).then(res => { const leaderboard = getLeaderboardResponse.data; // [ { treeName: "Name", treeHeight: 1234.5, treeRank: 67 }, {...}, {...} ]
const leaderboard = res.data;
// Prepare the beginning of the comparison message
let replyString = 'Current Tree Height: ' + treeHeight + 'ft\n\n'; let comparisonReplyString = `Here\'s how your tree compares: \nCurrent Tree Height: ${guildInfo.treeHeight}ft\n\n`;
leaderboard.reverse().forEach(treeRanking => { // Iterate over the leaderboard entries, backwards
let difference = parseFloat(treeRanking.treeHeight).toFixed(1) - treeHeight; for (let i = leaderboard.length - 1; i >= 0; i--) {
let decimal = (treeRanking.treeHeight % 1).toFixed(1); const leaderboardEntry = leaderboard[i];
let growthIndicator = ""; // Setup the status indicator, default to blank, we'll change it later
if (decimal > 0) { let statusIndicator = "``[";
growthIndicator += "[+]"; if ((leaderboardEntry.treeHeight % 1).toFixed(1) > 0) statusIndicator += "💧|";
}
const absDifference = parseFloat(Math.abs(difference)).toFixed(1); // Get the data for this tree from 24 hours ago
if (treeRanking.hasPin) { const get24hTreeResponse = await dbfn.get24hTree(interaction.guildId, leaderboardEntry.treeName);
replyString += "This is your tree. "; const dayAgoTree = get24hTreeResponse.data;
} else if ((treeRanking.treeHeight == treeHeight) && (treeRanking.treeName == guildInfo.treeName)) { const hist24hDifference = (leaderboardEntry.treeHeight - dayAgoTree.treeHeight).toFixed(1);
replyString += "This might be your tree. Same height, same name. "; statusIndicator += `+${hist24hDifference}ft|`
} else {
if (difference > 0) { // Get the watering time for this tree
replyString += `#${treeRanking.treeRank} - ${absDifference}ft${growthIndicator} shorter `; const waterTime = functions.getWaterTime(leaderboardEntry.treeHeight);
} else if (difference < 0) { statusIndicator += `${waterTime} mins]\`\``;
replyString += `#${treeRanking.treeRank} - ${absDifference}ft${growthIndicator} taller `;
} else if (difference == 0) { // Determine if this tree is the guild's tree
replyString += `#${treeRanking.treeRank} - Same Height${growthIndicator} `; if (leaderboardEntry.hasPin) {
} comparisonReplyString += `#{leaderboardEntry.treeRank} - This is your tree`;
} } else { // If it's another guild's tree
replyString += `[${functions.getWaterTime(treeRanking.treeHeight)} mins]\n`; // Calculate the current height difference
}); const currentHeightDifference = guildInfo.treeHeight - leaderboardEntry.treeHeight;
resolve('Here\'s how your tree compares: \n' + replyString);
}).catch(err => { if (currentHeightDifference > 0) { // Guild Tree is taller than the leaderboard tree
console.error(err); comparisonReplyString += `#${leaderboardEntry.treeRank} - ${currentHeightDifference}ft taller`;
}); } else {
}).catch(err => { comparisonReplyString += `#${leaderboardEntry.treeRank} - ${Math.abs(currentHeightDifference)}ft shorter`;
reject(err); }
return; // Build a string using the current leaderboard entry and the historic entry from 24 hours ago
}); comparisonReplyString += ` ${statusIndicator}\n`;
}); }
}
return comparisonReplyString;
} catch (err) {
console.error(err);
return 'An error occurred while comparing the trees.';
}
} }
}, },
tree: { tree: {

View File

@ -1,6 +1,6 @@
{ {
"name": "silvanus", "name": "silvanus",
"version": "1.1.1", "version": "1.1.2",
"description": "Grow A Tree Comparison Tool", "description": "Grow A Tree Comparison Tool",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {