From dbd66c248c0e6a223d78a66e00fefc65ccaacd7b Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Fri, 10 Mar 2023 21:16:32 -0500 Subject: [PATCH] Testing new tree update detection --- data/strings.json | 3 +++ main.js | 5 ++++ modules/CustomClasses.js | 12 ++++++++- modules/functions.js | 53 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/data/strings.json b/data/strings.json index a5b077f..c6679fe 100755 --- a/data/strings.json +++ b/data/strings.json @@ -70,5 +70,8 @@ "water": "is ready to be watered again!", "fruit": "Fruit is appearing!" }, + "ids": { + "growATree": "972637072991068220" + }, "temp": {} } \ No newline at end of file diff --git a/main.js b/main.js index a81e1e0..1ba7f95 100755 --- a/main.js +++ b/main.js @@ -86,6 +86,11 @@ client.on('interactionCreate', async interaction => { } }); +client.on('messageUpdate', async message => { + await fn.sleep(50); + await fn.messages.updateHandler(message); +}); + async function checkRateLimits(hi) { const axios = require('axios'); diff --git a/modules/CustomClasses.js b/modules/CustomClasses.js index abdc8fe..6762c84 100755 --- a/modules/CustomClasses.js +++ b/modules/CustomClasses.js @@ -153,7 +153,17 @@ module.exports = { } return queryParts.join(''); break; - default: + case "setTreeInfo": + queryParts = [ + `INSERT INTO guild_info (`, + `guild_id, tree_name, tree_height`, + `) VALUES (`, + `${db.escape(this.guildId)}, ${db.escape(this.treeName)}, ${db.escape(this.treeHeight)}`, + `) ON DUPLICATE KEY UPDATE tree_name = ${db.escape(this.treeName)}, `, + `tree_height = ${db.escape(this.treeHeight)}` + ]; + return queryParts.join(''); + default: break; } } diff --git a/modules/functions.js b/modules/functions.js index 9144d2a..b793ef3 100755 --- a/modules/functions.js +++ b/modules/functions.js @@ -324,7 +324,6 @@ const functions = { }, tree: { parse(interaction, guildInfo) { - let input; return new Promise((resolve, reject) => { if (guildInfo == undefined) { reject(`The guild entry hasn't been created yet. [${interaction.guildId || interaction.commandGuildId}]`); @@ -337,6 +336,7 @@ const functions = { reject("This doesn't appear to be a valid ``/tree`` message."); return; } + let input; input = m.embeds[0].data.description; let treeName = m.embeds[0].data.title; let lines = input.split('\n'); @@ -493,12 +493,61 @@ const functions = { }, isTree(message) { if (message.embeds.length > 0) { - return message.embeds[0].data.description.includes("Your tree is"); + // Grab the description and title + const {description, title} = message.embeds[0].data; + // Make sure it's a tree message + if (description.includes("Your tree is")) { + // Grab the name + const treeName = title; + // Grab the tree's height + const indices = [description.indexOf("Your tree is ") + 13, description.indexOf("ft")]; + const treeHeightStr = description.slice(indices[0], indices[1]); + const treeHeightFloat = parseFloat(treeHeightStr).toFixed(1); + + // Return the info gathered + return { + treeName: treeName, + treeHeight: treeHeightFloat + }; + } + } else { + return false; } }, isLeaderboard(message) { if (message.embeds.length > 0) { return message.embeds[0].data.title == "Tallest Trees"; + } else { + return false; + } + }, + async updateHandler(message) { + if (message.partial) { + message = await message.fetch(); + } + // Make sure the message is from Grow A Tree + if (message.author.id != strings.ids.growATree) return; + // Check and store the message types + const isLeaderboard = this.isLeaderboard(message); + const isTree = this.isTree(message); + // Check if the message is a leaderboard + if (isLeaderboard) { + // Need to actually handle this later + // console.log("I've seen a leaderboard update."); + } else if (isTree) { // Check if the message is a tree + // console.log(`I've seen a tree update: ${isTree.treeName}: ${isTree.treeHeight}ft`); + let guildInfo; + if (message.client.guildInfos.has(message.guildId)) { + guildInfo = message.client.guildInfos.get(message.guildId); + guildInfo.setName(isTree.treeName) + .setHeight(isTree.treeHeight); + } else { + guildInfo = new GuildInfo().setId(message.guildId) + .setName(isTree.treeName) + .setHeight(isTree.treeHeight); + } + const query = guildInfo.queryBuilder("setTreeInfo"); + await dbfn.setGuildInfo(query); } } },