Compare commits
10 Commits
main
...
v1.1.2-dev
Author | SHA1 | Date | |
---|---|---|---|
|
57769f95ff | ||
|
a8ceecff9a | ||
|
9b2ad681b8 | ||
|
112c671435 | ||
|
74b65a1978 | ||
|
a69bca9259 | ||
|
f5daa186ac | ||
|
d6eac632e8 | ||
|
2627cc24b1 | ||
|
82c20dda68 |
@ -4,7 +4,7 @@
|
||||
},
|
||||
"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.",
|
||||
"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>"
|
||||
|
14
main.js
14
main.js
@ -30,9 +30,14 @@ client.once('ready', () => {
|
||||
fn.collections.slashCommands(client);
|
||||
console.log('Ready!');
|
||||
client.user.setActivity({ name: strings.activity.name, type: ActivityType.Watching });
|
||||
client.channels.fetch(statusChannelId).then(channel => {
|
||||
channel.send(`${new Date().toISOString()} -- \nStartup Sequence Complete`);
|
||||
});
|
||||
if (isDev == 'false') {
|
||||
client.channels.fetch(statusChannelId).then(channel => {
|
||||
channel.send(`${new Date().toISOString()} -- \nStartup Sequence Complete <@481933290912350209>`);
|
||||
});
|
||||
} else {
|
||||
// Dev shit
|
||||
fn.checkReady(client);
|
||||
}
|
||||
});
|
||||
|
||||
// slash-commands
|
||||
@ -53,6 +58,9 @@ client.on('interactionCreate', async interaction => {
|
||||
|
||||
if (interaction.isButton() && interaction.component.customId == 'refresh') {
|
||||
fn.refresh(interaction);
|
||||
} else if (interaction.isButton() && interaction.component.customId == 'resetping') {
|
||||
fn.resetPing(interaction);
|
||||
interaction.reply({ content: "Reset water readiness detection system.", ephemeral: true });
|
||||
}
|
||||
});
|
||||
|
||||
|
185
modules/dbfn.js
185
modules/dbfn.js
@ -81,7 +81,7 @@ module.exports = {
|
||||
if (err) throw `Error connecting to the database: ${err.message}`;
|
||||
});
|
||||
// Get a server's tree information from the database
|
||||
const selectGuildInfoQuery = `SELECT tree_name, tree_height, tree_message_id, tree_channel_id, leaderboard_message_id, leaderboard_channel_id FROM guild_info WHERE guild_id = ${db.escape(guildId)}`;
|
||||
const selectGuildInfoQuery = `SELECT tree_name, tree_height, tree_message_id, tree_channel_id, leaderboard_message_id, leaderboard_channel_id, ping_role_id, ping_channel_id, reminded_status FROM guild_info WHERE guild_id = ${db.escape(guildId)}`;
|
||||
// TODO run this query and return a promise then structure the output into a GuildInfo object. resolve with { "status": , "data": guildInfo }
|
||||
return new Promise((resolve, reject) => {
|
||||
db.query(selectGuildInfoQuery, (err, res) => {
|
||||
@ -97,7 +97,10 @@ module.exports = {
|
||||
"treeMessageId": "123",
|
||||
"treeChannelId": "123",
|
||||
"leaderboardMessageId": "123",
|
||||
"leaderboardChannelId": "123"
|
||||
"leaderboardChannelId": "123",
|
||||
"reminderMessage": "Abc",
|
||||
"reminderChannelId": "123",
|
||||
"remindedStatus": 0
|
||||
};*/
|
||||
if (res.length == 0) {
|
||||
reject("There is no database entry for your guild yet. Try running /setup");
|
||||
@ -111,7 +114,10 @@ module.exports = {
|
||||
"treeMessageId": row.tree_message_id,
|
||||
"treeChannelId": row.tree_channel_id,
|
||||
"leaderboardMessageId": row.leaderboard_message_id,
|
||||
"leaderboardChannelId": row.leaderboard_channel_id
|
||||
"leaderboardChannelId": row.leaderboard_channel_id,
|
||||
"pingRoleId": row.ping_role_id,
|
||||
"pingChannelId": row.ping_channel_id,
|
||||
"remindedStatus": row.reminded_status
|
||||
};
|
||||
db.end();
|
||||
resolve({ "status": "Successfully fetched guild information", "data": guildInfo });
|
||||
@ -300,5 +306,178 @@ module.exports = {
|
||||
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 });
|
||||
});
|
||||
});
|
||||
},
|
||||
setReminderInfo(guildId, reminderMessage, reminderChannelId) {
|
||||
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 insertReminderInfoQuery = `UPDATE guild_info SET ping_role_id = ${db.escape(reminderMessage)}, ping_channel_id = ${db.escape(reminderChannelId)} WHERE guild_id = ${db.escape(guildId)}`;
|
||||
// TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard }
|
||||
return new Promise((resolve, reject) => {
|
||||
db.query(insertReminderInfoQuery, (err, res) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
db.end();
|
||||
reject("Error updating the reminder info: " + err.message);
|
||||
return;
|
||||
}
|
||||
db.end();
|
||||
resolve({ "status": `Successfully set the reminder message to "${reminderMessage}" in <#${reminderChannelId}>`, "data": res });
|
||||
});
|
||||
});
|
||||
},
|
||||
setRemindedStatus(guildId, remindedStatus) {
|
||||
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 setRemindedStatusQuery = `UPDATE guild_info SET reminded_status = ${db.escape(remindedStatus)} WHERE guild_id = ${db.escape(guildId)}`;
|
||||
// TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard }
|
||||
return new Promise((resolve, reject) => {
|
||||
db.query(setRemindedStatusQuery, (err, res) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
db.end();
|
||||
reject("Error updating the reminded status: " + err.message);
|
||||
return;
|
||||
}
|
||||
db.end();
|
||||
resolve({ "status": `Successfully set the reminded status to ${remindedStatus}`, "data": res });
|
||||
});
|
||||
});
|
||||
},
|
||||
setReminderOptIn(guildId, optIn) {
|
||||
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 setReminderOptInQuery = `UPDATE guild_info SET reminder_optin = ${db.escape(optIn)} WHERE guild_id = ${db.escape(guildId)}`;
|
||||
// TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard }
|
||||
return new Promise((resolve, reject) => {
|
||||
db.query(setReminderOptInQuery, (err, res) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
db.end();
|
||||
reject("Error updating the reminder opt-in status: " + err.message);
|
||||
return;
|
||||
}
|
||||
db.end();
|
||||
resolve({ "status": `Successfully set the reminder opt-in status to ${optIn}`, "data": res });
|
||||
});
|
||||
});
|
||||
},
|
||||
getOptedInGuilds() {
|
||||
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}`;
|
||||
});
|
||||
// Get a server's tree information from the database
|
||||
const getOptedInGuildsQuery = `SELECT guild_id, tree_name, tree_height, tree_message_id, tree_channel_id, leaderboard_message_id, leaderboard_channel_id, ping_role_id, ping_channel_id, reminded_status FROM guild_info WHERE reminder_optin = 1 AND reminded_status = 0`;
|
||||
// TODO run this query and return a promise then structure the output into a GuildInfo object. resolve with { "status": , "data": guildInfo }
|
||||
return new Promise((resolve, reject) => {
|
||||
db.query(getOptedInGuildsQuery, (err, res) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
reject("Error fetching guild information: " + err.message);
|
||||
db.end();
|
||||
return;
|
||||
}
|
||||
/*const guildInfo = { "guildId": "123",
|
||||
"treeName": "name",
|
||||
"treeHeight": 123,
|
||||
"treeMessageId": "123",
|
||||
"treeChannelId": "123",
|
||||
"leaderboardMessageId": "123",
|
||||
"leaderboardChannelId": "123",
|
||||
"reminderMessage": "Abc",
|
||||
"reminderChannelId": "123",
|
||||
"remindedStatus": 0
|
||||
};*/
|
||||
if (res.length == 0) {
|
||||
resolve({"status": "No servers have opted in yet"});
|
||||
db.end();
|
||||
return;
|
||||
}
|
||||
row = res[0];
|
||||
let guilds = [];
|
||||
res.forEach(row => {
|
||||
guilds.push({
|
||||
"guildId": row.guild_id,
|
||||
"treeName": row.tree_name,
|
||||
"treeHeight": row.tree_height,
|
||||
"treeMessageId": row.tree_message_id,
|
||||
"treeChannelId": row.tree_channel_id,
|
||||
"leaderboardMessageId": row.leaderboard_message_id,
|
||||
"leaderboardChannelId": row.leaderboard_channel_id,
|
||||
"reminderMessage": row.ping_role_id,
|
||||
"reminderChannelId": row.ping_channel_id,
|
||||
"remindedStatus": row.reminded_status
|
||||
});
|
||||
});
|
||||
db.end();
|
||||
resolve({ "status": "Successfully fetched guild information", "data": guilds });
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
@ -17,6 +17,7 @@ const config = require('../data/config.json');
|
||||
const strings = require('../data/strings.json');
|
||||
const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => file.endsWith('.js'));
|
||||
const dbfn = require('./dbfn.js');
|
||||
const { finished } = require('stream');
|
||||
|
||||
dbfn.createGuildTables().then(res => {
|
||||
console.log(res.status);
|
||||
@ -41,17 +42,26 @@ const functions = {
|
||||
}
|
||||
},
|
||||
builders: {
|
||||
refreshAction() {
|
||||
async refreshAction(guildId) {
|
||||
// Create the button to go in the Action Row
|
||||
const refreshButton = new ButtonBuilder()
|
||||
.setCustomId('refresh')
|
||||
.setLabel('Refresh')
|
||||
.setStyle(ButtonStyle.Primary);
|
||||
const resetPingButton = new ButtonBuilder()
|
||||
.setCustomId('resetping')
|
||||
.setLabel('Reset Ping')
|
||||
.setStyle(ButtonStyle.Secondary);
|
||||
// Create the Action Row with the Button in it, to be sent with the Embed
|
||||
const refreshActionRow = new ActionRowBuilder()
|
||||
let refreshActionRow = new ActionRowBuilder()
|
||||
.addComponents(
|
||||
refreshButton
|
||||
);
|
||||
const getGuildInfoResponse = await dbfn.getGuildInfo(guildId);
|
||||
const guildInfo = getGuildInfoResponse.data;
|
||||
if (guildInfo.reminderMessage != "" && guildInfo.reminderChannelId != "") {
|
||||
refreshActionRow.addComponents(resetPingButton);
|
||||
}
|
||||
return refreshActionRow;
|
||||
},
|
||||
comparisonEmbed(content, refreshActionRow) {
|
||||
@ -64,6 +74,16 @@ const functions = {
|
||||
const messageContents = { embeds: [embed], components: [refreshActionRow] };
|
||||
return messageContents;
|
||||
},
|
||||
reminderEmbed(content, guildInfo) {
|
||||
// Create the embed using the content passed to this function
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(strings.embeds.color)
|
||||
.setTitle('Water Reminder')
|
||||
.setDescription(`${content}\n[Your Tree](https://discord.com/channels/${guildInfo.guildId}/${guildInfo.treeChannelId}/${guildInfo.treeMessageId})`)
|
||||
.setFooter({text: `This message will self-destruct in 60 seconds...`});
|
||||
const messageContents = { embeds: [embed] };
|
||||
return messageContents;
|
||||
},
|
||||
helpEmbed(content, private) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(strings.embeds.color)
|
||||
@ -173,49 +193,56 @@ const functions = {
|
||||
});
|
||||
|
||||
},
|
||||
compare(interaction) {
|
||||
return new Promise((resolve, reject) => {
|
||||
dbfn.getGuildInfo(interaction.guildId).then(res => {
|
||||
const guildInfo = res.data;
|
||||
guildInfo.guildId = interaction.guildId;
|
||||
|
||||
let treeHeight = parseFloat(guildInfo.treeHeight).toFixed(1);
|
||||
dbfn.getLeaderboard(interaction.guildId).then(res => {
|
||||
const leaderboard = res.data;
|
||||
|
||||
let replyString = 'Current Tree Height: ' + treeHeight + 'ft\n\n';
|
||||
leaderboard.reverse().forEach(treeRanking => {
|
||||
let difference = parseFloat(treeRanking.treeHeight).toFixed(1) - treeHeight;
|
||||
let decimal = (treeRanking.treeHeight % 1).toFixed(1);
|
||||
let growthIndicator = "";
|
||||
if (decimal > 0) {
|
||||
growthIndicator += "[+]";
|
||||
}
|
||||
const absDifference = parseFloat(Math.abs(difference)).toFixed(1);
|
||||
if (treeRanking.hasPin) {
|
||||
replyString += "This is your tree. ";
|
||||
} else if ((treeRanking.treeHeight == treeHeight) && (treeRanking.treeName == guildInfo.treeName)) {
|
||||
replyString += "This might be your tree. Same height, same name. ";
|
||||
} else {
|
||||
if (difference > 0) {
|
||||
replyString += `#${treeRanking.treeRank} - ${absDifference}ft${growthIndicator} shorter `;
|
||||
} else if (difference < 0) {
|
||||
replyString += `#${treeRanking.treeRank} - ${absDifference}ft${growthIndicator} taller `;
|
||||
} else if (difference == 0) {
|
||||
replyString += `#${treeRanking.treeRank} - Same Height${growthIndicator} `;
|
||||
}
|
||||
}
|
||||
replyString += `[${functions.getWaterTime(treeRanking.treeHeight)} mins]\n`;
|
||||
});
|
||||
resolve('Here\'s how your tree compares: \n' + replyString);
|
||||
}).catch(err => {
|
||||
console.error(err);
|
||||
});
|
||||
}).catch(err => {
|
||||
reject(err);
|
||||
return;
|
||||
});
|
||||
});
|
||||
async compare(interaction) {
|
||||
try {
|
||||
// fetch the guild's settings from the database
|
||||
const guildInfoResponse = await dbfn.getGuildInfo(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
|
||||
const getLeaderboardResponse = await dbfn.getLeaderboard(interaction.guildId);
|
||||
const leaderboard = getLeaderboardResponse.data; // [ { treeName: "Name", treeHeight: 1234.5, treeRank: 67 }, {...}, {...} ]
|
||||
|
||||
// Prepare the beginning of the comparison message
|
||||
let comparisonReplyString = `Here\'s how your tree compares: \nCurrent Tree Height: ${guildInfo.treeHeight}ft\n\n`;
|
||||
// Iterate over the leaderboard entries, backwards
|
||||
for (let i = leaderboard.length - 1; i >= 0; i--) {
|
||||
const leaderboardEntry = leaderboard[i];
|
||||
// Setup the status indicator, default to blank, we'll change it later
|
||||
let statusIndicator = "``[";
|
||||
if ((leaderboardEntry.treeHeight % 1).toFixed(1) > 0) statusIndicator += "💧|";
|
||||
|
||||
// Get the data for this tree from 24 hours ago
|
||||
const get24hTreeResponse = await dbfn.get24hTree(interaction.guildId, leaderboardEntry.treeName);
|
||||
const dayAgoTree = get24hTreeResponse.data;
|
||||
const hist24hDifference = (leaderboardEntry.treeHeight - dayAgoTree.treeHeight).toFixed(1);
|
||||
statusIndicator += `+${hist24hDifference}ft|`
|
||||
|
||||
// Get the 24h watering time for this tree
|
||||
const totalWaterTime = await functions.timeToHeight(dayAgoTree.treeHeight, leaderboardEntry.treeHeight);
|
||||
statusIndicator += `${totalWaterTime}]\`\``;
|
||||
|
||||
// Determine if this tree is the guild's tree
|
||||
if (leaderboardEntry.hasPin) {
|
||||
comparisonReplyString += `#${leaderboardEntry.treeRank} - This is your tree`;
|
||||
} else { // If it's another guild's tree
|
||||
// Calculate the current height difference
|
||||
const currentHeightDifference = guildInfo.treeHeight - leaderboardEntry.treeHeight;
|
||||
|
||||
if (currentHeightDifference > 0) { // Guild Tree is taller than the leaderboard tree
|
||||
comparisonReplyString += `#${leaderboardEntry.treeRank} - ${currentHeightDifference.toFixed(1)}ft taller`;
|
||||
} else {
|
||||
comparisonReplyString += `#${leaderboardEntry.treeRank} - ${Math.abs(currentHeightDifference).toFixed(1)}ft shorter`;
|
||||
}
|
||||
}
|
||||
// Build a string using the current leaderboard entry and the historic entry from 24 hours ago
|
||||
comparisonReplyString += `\n${statusIndicator}\n`;
|
||||
// if (process.env.isDev == 'true') comparisonReplyString += `Current Height: ${leaderboardEntry.treeHeight} 24h Ago Height: ${dayAgoTree.treeHeight}\n`;
|
||||
}
|
||||
return comparisonReplyString;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return 'An error occurred while comparing the trees.';
|
||||
}
|
||||
}
|
||||
},
|
||||
tree: {
|
||||
@ -262,8 +289,9 @@ const functions = {
|
||||
refresh(interaction) {
|
||||
functions.rankings.parse(interaction).then(r1 => {
|
||||
functions.tree.parse(interaction).then(r2 => {
|
||||
functions.rankings.compare(interaction).then(res => {
|
||||
const embed = functions.builders.comparisonEmbed(res, functions.builders.refreshAction())
|
||||
functions.rankings.compare(interaction).then(async res => {
|
||||
const refreshActionRow = await functions.builders.refreshAction(interaction.guildId);
|
||||
const embed = functions.builders.comparisonEmbed(res, refreshActionRow)
|
||||
interaction.update(embed);
|
||||
}).catch(err => {
|
||||
console.error(err);
|
||||
@ -304,27 +332,100 @@ const functions = {
|
||||
});
|
||||
},
|
||||
getWaterTime(size) {
|
||||
const seconds = Math.floor(Math.pow(size * 0.07 + 5, 1.1));
|
||||
return (Math.floor((Math.pow(size * 0.07 + 5, 1.1))) / 60).toFixed(2);
|
||||
return Math.floor(Math.pow(size * 0.07 + 5, 1.1)); // Seconds
|
||||
},
|
||||
timeToHeight(interaction, destHeight) {
|
||||
timeToHeight(beginHeight, destHeight) {
|
||||
return new Promise((resolve, reject) => {
|
||||
dbfn.getGuildInfo(interaction.guildId).then(res => {
|
||||
let guildInfo = res.data;
|
||||
let currentHeight = parseInt(guildInfo.treeHeight);
|
||||
let time = 0;
|
||||
for (let i = currentHeight; i < destHeight; i++) {
|
||||
const waterTime = parseFloat(functions.getWaterTime(i));
|
||||
console.log("Height: " + i + "Time: " + waterTime);
|
||||
time += waterTime;
|
||||
}
|
||||
resolve(time.toFixed(2));
|
||||
}).catch(err => {
|
||||
console.error(err);
|
||||
reject(err);
|
||||
return;
|
||||
})
|
||||
let time = 0;
|
||||
for (let i = beginHeight; i < destHeight; i++) {
|
||||
const waterTime = parseFloat(functions.getWaterTime(i));
|
||||
// console.log("Height: " + i + "Time: " + waterTime);
|
||||
time += waterTime;
|
||||
}
|
||||
|
||||
// 60 secs in min
|
||||
// 3600 secs in hr
|
||||
// 86400 sec in day
|
||||
|
||||
let units = " secs";
|
||||
if ( 60 < time && time <= 3600 ) { // Minutes
|
||||
time = parseFloat(time / 60).toFixed(1);
|
||||
units = " mins";
|
||||
} else if ( 3600 < time && time <= 86400 ) {
|
||||
time = parseFloat(time / 3600).toFixed(1);
|
||||
units = " hrs";
|
||||
} else if ( 86400 < time ) {
|
||||
time = parseFloat(time / 86400).toFixed(1);
|
||||
units = " days";
|
||||
}
|
||||
resolve(time + units);
|
||||
});
|
||||
},
|
||||
sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
},
|
||||
async sendReminder(guildInfo, guild) {
|
||||
const { guildId, reminderChannelId, reminderMessage } = guildInfo;
|
||||
const reminderChannel = await guild.channels.fetch(reminderChannelId);
|
||||
const reminderEmbed = functions.builders.reminderEmbed(reminderMessage, guildInfo);
|
||||
reminderChannel.send(reminderEmbed).then(async m => {
|
||||
await dbfn.setRemindedStatus(guildId, 1);
|
||||
if (m.deletable) {
|
||||
setTimeout(function() {
|
||||
m.delete();
|
||||
}, 60000);
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error(err);
|
||||
});
|
||||
},
|
||||
async setReminder(interaction, ms) {
|
||||
setTimeout(this.sendReminder(interaction), ms);
|
||||
},
|
||||
async checkReady(client) { // Check if the guilds trees are ready to water
|
||||
// TODO This is hard coded for the dev server, need to change it to lookup each server and iterate over them
|
||||
// Would also be helpful to have an opt-in or opt-out ability for water checks
|
||||
try {
|
||||
const getOptedInGuildsResponse = await dbfn.getOptedInGuilds();
|
||||
if (getOptedInGuildsResponse.status != "No servers have opted in yet") {
|
||||
const guilds = getOptedInGuildsResponse.data;
|
||||
guilds.forEach(async guildInfo => {
|
||||
const { guildId, treeChannelId, treeMessageId, remindedStatus } = guildInfo;
|
||||
|
||||
if (remindedStatus == 0) {
|
||||
const guild = await client.guilds.fetch(guildId);
|
||||
const treeChannel = await guild.channels.fetch(treeChannelId);
|
||||
const treeMessage = await treeChannel.messages.fetch(treeMessageId);
|
||||
const readyToWater = treeMessage.embeds[0].description.includes('Ready to be watered');
|
||||
if (readyToWater) {
|
||||
console.log("Ready to water");
|
||||
this.sendReminder(guildInfo, guild);
|
||||
this.sleep(5000).then(() => {
|
||||
this.checkReady(client);
|
||||
});
|
||||
} else {
|
||||
console.log("Not ready to water");
|
||||
this.sleep(5000).then(() => {
|
||||
this.checkReady(client);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// console.log(getOptedInGuildsResponse.status);
|
||||
this.sleep(5000).then(() => {
|
||||
this.checkReady(client);
|
||||
});
|
||||
}
|
||||
} catch(err) {
|
||||
console.error(err);
|
||||
this.sleep(30000).then(() => {
|
||||
this.checkReady(client);
|
||||
});
|
||||
}
|
||||
},
|
||||
resetPing(interaction) {
|
||||
dbfn.setRemindedStatus(interaction.guildId, 0);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "silvanus",
|
||||
"version": "1.1.1",
|
||||
"version": "1.1.3",
|
||||
"description": "Grow A Tree Comparison Tool",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
@ -7,8 +7,9 @@ module.exports = {
|
||||
.setDescription('See how your tree compares to other trees!'),
|
||||
async execute(interaction) {
|
||||
interaction.deferReply().then(() => {
|
||||
fn.rankings.compare(interaction).then(res => {
|
||||
const embed = fn.builders.comparisonEmbed(res, fn.builders.refreshAction());
|
||||
fn.rankings.compare(interaction).then(async res => {
|
||||
const refreshActionRow = await fn.builders.refreshAction(interaction.guildId);
|
||||
const embed = fn.builders.comparisonEmbed(res, refreshActionRow);
|
||||
interaction.editReply(embed).catch(err => {
|
||||
console.error(err);
|
||||
});
|
||||
|
18
slash-commands/setping.js
Normal file
18
slash-commands/setping.js
Normal file
@ -0,0 +1,18 @@
|
||||
const { SlashCommandBuilder } = require('discord.js');
|
||||
const dbfn = require('../modules/dbfn.js');
|
||||
const fn = require('../modules/functions.js');
|
||||
|
||||
module.exports = {
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('setping')
|
||||
.setDescription('Run this command when you water your tree to have a reminder sent.'),
|
||||
async execute(interaction) {
|
||||
await interaction.deferReply({ ephemeral: true });
|
||||
const getGuildInfoResponse = await dbfn.getGuildInfo(interaction.guildId);
|
||||
const guildInfo = getGuildInfoResponse.data;
|
||||
const reminderTimeS = fn.getWaterTime(guildInfo.treeHeight);
|
||||
const reminderTimeMs = reminderTimeS * 1000;
|
||||
fn.setReminder(interaction, reminderTimeMs, guildInfo.pingRoleId);
|
||||
interaction.editReply("A reminder has been set.");
|
||||
},
|
||||
};
|
24
slash-commands/setpingrole.js
Normal file
24
slash-commands/setpingrole.js
Normal file
@ -0,0 +1,24 @@
|
||||
const { SlashCommandBuilder } = require('discord.js');
|
||||
const dbfn = require('../modules/dbfn.js');
|
||||
const fn = require('../modules/functions.js');
|
||||
|
||||
module.exports = {
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('setpingrole')
|
||||
.setDescription('Set the role to ping when you run /setping')
|
||||
.addRoleOption(o =>
|
||||
o.setName('pingrole')
|
||||
.setDescription('The role to ping')
|
||||
.setRequired(true)),
|
||||
async execute(interaction) {
|
||||
try {
|
||||
await interaction.deferReply({ ephemeral: true });
|
||||
const pingRole = interaction.options.getRole('pingrole');
|
||||
const setPingRoleResponse = await dbfn.setPingRole(interaction.guildId, pingRole.id);
|
||||
interaction.editReply(setPingRoleResponse.status);
|
||||
} catch(err) {
|
||||
console.error(err);
|
||||
await interaction.editReply(fn.builders.errorEmbed(err));
|
||||
}
|
||||
},
|
||||
};
|
@ -1,4 +1,5 @@
|
||||
const { SlashCommandBuilder } = require('discord.js');
|
||||
const dbfn = require('../modules/dbfn.js');
|
||||
const fn = require('../modules/functions.js');
|
||||
|
||||
module.exports = {
|
||||
@ -6,14 +7,19 @@ module.exports = {
|
||||
.setName('timetoheight')
|
||||
.setDescription('Calculate how long it would take to reach a given height')
|
||||
.addStringOption(o =>
|
||||
o.setName('height')
|
||||
.setDescription('Tree height in feet, numbers ONLY')
|
||||
o.setName('beginheight')
|
||||
.setDescription('Begining tree height in feet, numbers ONLY')
|
||||
.setRequired(true))
|
||||
.addStringOption(o =>
|
||||
o.setName('endheight')
|
||||
.setDescription('Ending tree height in feet, numbers ONLY')
|
||||
.setRequired(true)),
|
||||
async execute(interaction) {
|
||||
await interaction.deferReply();
|
||||
const destTreeHeight = interaction.options.getString('height');
|
||||
fn.timeToHeight(interaction, destTreeHeight).then(res => {
|
||||
interaction.editReply(`It will take you ${res} minutes to reach ${destTreeHeight}ft.`);
|
||||
const beginHeight = interaction.options.getString('beginheight');
|
||||
const endHeight = interaction.options.getString('endheight');
|
||||
fn.timeToHeight(beginHeight, endHeight).then(res => {
|
||||
interaction.editReply(`It will take a tree that is ${beginHeight}ft tall ${res} to reach ${endHeight}ft.`);
|
||||
}).catch(err => {
|
||||
interaction.editReply("Error: " + err);
|
||||
console.error(err);
|
||||
|
Loading…
Reference in New Issue
Block a user