From 2694d4b691772ed48ee5c053101a61200234cec9 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sun, 12 Feb 2023 17:16:03 -0500 Subject: [PATCH] Tentative deploy --- modules/CustomClasses.js | 18 ++++++++++++++++++ modules/dbfn.js | 29 +++++++++++++++++++++++++++++ modules/functions.js | 18 ------------------ slash-commands/setupinfo.js | 23 ++++++++++++----------- 4 files changed, 59 insertions(+), 29 deletions(-) diff --git a/modules/CustomClasses.js b/modules/CustomClasses.js index f528fdf..ff7d714 100644 --- a/modules/CustomClasses.js +++ b/modules/CustomClasses.js @@ -138,5 +138,23 @@ module.exports = { break; } } + generateSetupInfo() { + let setupInfoParts = [ + `Here is your server's configuration:`, + `Tree Name: ${this.treeName}`, + `Tree Height: ${this.treeHeight}`, + `Tree Channel: <#${this.treeChannelId}>`, + `[Tree Link](https://discord.com/channels/${this.guildId}/${this.treeChannelId}/${this.treeMessageId})`, + `Leaderboard Channel: <#${this.leaderboardChannelId}>`, + `[Leaderboard Link](https://discord.com/channels/${this.guildId}/${this.leaderboardChannelId}/${this.leaderboardMessageId})`, + `Notification Watch Channel: <#${this.watchChannelId}>`, + `Notification Relay Channel: <#${this.reminderChannelId}>`, + `Water Message: ${this.waterMessage}`, + `Fruit Message: ${this.fruitMessage}`, + `Water Role: <@&${this.waterRoleId}>`, + `Fruit Role: <@&${this.fruitRoleId}>` + ] + return setupInfoParts.join('\n'); + } } } \ No newline at end of file diff --git a/modules/dbfn.js b/modules/dbfn.js index 3729f80..8168061 100644 --- a/modules/dbfn.js +++ b/modules/dbfn.js @@ -145,6 +145,35 @@ module.exports = { }); }); }, + setTreeInfo(guildInfo) { + 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": null }) + // guildInfo = { "guildId": "123", "treeName": "name", "treeHeight": 123, "treeMessageId": "123", "treeChannelId": "123", "leaderboardMessageId": "123", "leaderboardChannelId": "123"} + // Set a server's tree information in the database) + const insertGuildInfoQuery = `INSERT INTO guild_info (guild_id, tree_name, tree_height, tree_message_id, tree_channel_id) VALUES (${db.escape(guildInfo.guildId)}, ${db.escape(guildInfo.treeName)}, ${db.escape(guildInfo.treeHeight)},${db.escape(guildInfo.treeMessageId)}, ${db.escape(guildInfo.treeChannelId)}) ON DUPLICATE KEY UPDATE tree_name = ${db.escape(guildInfo.treeName)},tree_height = ${db.escape(guildInfo.treeHeight)},tree_message_id = ${db.escape(guildInfo.treeMessageId)},tree_channel_id = ${db.escape(guildInfo.treeChannelId)}`; + // TODO run this query and return a promise, then resolve with { "status": , "data": null } + return new Promise((resolve, reject) => { + db.query(insertGuildInfoQuery, (err, res) => { + if (err) { + console.error(err); + db.end(); + reject("Error setting the guild info: " + err.message); + return; + } + db.end(); + resolve({ "status": "Successfully set the guild information", "data": null }); + }); + }); + }, setLeaderboardInfo(guildInfo) { const db = mysql.createConnection({ host: process.env.DBHOST, diff --git a/modules/functions.js b/modules/functions.js index 7fdab79..df5b86e 100644 --- a/modules/functions.js +++ b/modules/functions.js @@ -549,13 +549,6 @@ const functions = { }); }); }, - getInfo(interaction) { - let guildInfo = interaction.client.guildInfos.get(interaction.guild.id); - let guildInfoString = ""; - guildInfoString += `Tree Message: https://discord.com/channels/${guildId}/${guildInfo.treeChannelId}/${guildInfo.treeMessageId}\n`; - guildInfoString += `Rank Message: https://discord.com/channels/${guildId}/${guildInfo.leaderboardChannelId}/${guildInfo.leaderboardMessageId}\n`; - return `Here is your servers setup info:\n${guildInfoString}`; - }, getWaterTime(size) { return Math.floor(Math.pow(size * 0.07 + 5, 1.1)); // Seconds }, @@ -602,17 +595,6 @@ const functions = { console.error(err); }); }, - async refreshComparisonMessage(client, guildInfo) { - if (guildInfo.comparisonChannelId != "" && guildInfo.comparisonMessageId != "") { - const guild = await client.guilds.fetch(guildInfo.guildId); - const comparisonChannel = await guild.channels.fetch(guildInfo.comparisonChannelId); - const comparisonMessage = await comparisonChannel.messages.fetch(guildInfo.comparisonMessageId); - const embed = comparisonMessage.embeds[0]; - const actionRow = this.builders.actionRows.comparisonActionRow(guildInfo); - await comparisonMessage.edit({ components: [actionRow] }); - return; - } - }, async setupCollectors(client) { let guildInfos = client.guildInfos; guildInfos.set("collectors", []); diff --git a/slash-commands/setupinfo.js b/slash-commands/setupinfo.js index b2adf67..9f24358 100644 --- a/slash-commands/setupinfo.js +++ b/slash-commands/setupinfo.js @@ -5,17 +5,18 @@ module.exports = { data: new SlashCommandBuilder() .setName('setupinfo') .setDescription('View information about how the bot is set up in your server'), - execute(interaction) { - interaction.deferReply({ephemeral: true}).then(() => { - fn.getInfo(interaction).then(res => { - const embed = fn.builders.embed(res); - interaction.editReply(embed); - }).catch(err => { - interaction.editReply(err); - console.error(err); - }); - }).catch(err => { + async execute(interaction) { + await interaction.deferReply({ ephemeral: true }).catch(err => console.error(err)); + try { + if (interaction.client.guildInfos.has(interaction.guildId)) { + let guildInfo = interaction.client.guildInfos.get(interaction.guildId); + await interaction.editReply(fn.builders.embed(guildInfo.generateSetupInfo())); + } else { + await interaction.editReply(fn.builders.errorEmbed("Guild doesn't exist in database!")); + } + } catch (err) { console.error(err); - }); + await interaction.editReply(fn.builders.errorEmbed("There was an error running the command.")); + } }, }; \ No newline at end of file