diff --git a/main.js b/main.js index a41eefd..fef604a 100644 --- a/main.js +++ b/main.js @@ -24,6 +24,7 @@ const client = new Client({ // Various imports const fn = require('./modules/functions.js'); const strings = require('./data/strings.json'); +const dbfn = require('./modules/dbfn.js'); const isDev = process.env.isDev; client.once('ready', () => { @@ -55,16 +56,22 @@ client.on('interactionCreate', async interaction => { } if (interaction.isButton() && interaction.component.customId == 'refresh') { + // console.log(JSON.stringify(interaction)); await fn.refresh(interaction).catch(err => { interaction.channel.send(fn.builders.errorEmbed(err)); }); } else if (interaction.isButton() && interaction.component.customId == 'resetping') { await fn.resetPing(interaction); await fn.refresh(interaction).catch(err => { - interaction.update(fn.builders.errorEmbed(err)); + interaction.channel.send(fn.builders.errorEmbed(err)); }); } else if (interaction.isButton() && interaction.component.customId == 'deleteping') { if (interaction.message.deletable) { + await dbfn.setRemindedStatus(interaction.guildId, 0); + await dbfn.getGuildInfo(interaction.guildId).then(async res => { + const guildInfo = res.data; + await fn.refreshComparisonMessage(interaction.client, guildInfo); + }); await interaction.message.delete().catch(err => { console.error(err); }); diff --git a/modules/dbfn.js b/modules/dbfn.js index c0a54a9..5eac3e4 100644 --- a/modules/dbfn.js +++ b/modules/dbfn.js @@ -509,7 +509,7 @@ module.exports = { if (err) throw `Error connecting to the database: ${err.message}`; }); // Returns a Promise, resolve({ "status": "", "data": leaderboard }) - const setComparisonMessageQuery = `UPDATE guild_info SET comparison_message_id = ${db.escape(comparisonMessage.id)}, comparison_channel_id = ${db.escape(comparisonMessage.interaction.channelId)} WHERE guild_id = ${db.escape(guildId)}`; + const setComparisonMessageQuery = `UPDATE guild_info SET comparison_message_id = ${db.escape(comparisonMessage.id)}, comparison_channel_id = ${db.escape(comparisonMessage.channel.id)} WHERE guild_id = ${db.escape(guildId)}`; // console.log(JSON.stringify(comparisonMessage)); // TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard } return new Promise((resolve, reject) => { diff --git a/modules/functions.js b/modules/functions.js index 833337e..90d2e97 100644 --- a/modules/functions.js +++ b/modules/functions.js @@ -53,6 +53,7 @@ const functions = { return actionRow; }, comparisonActionRow(guildInfo) { + // console.log(guildInfo); // Create the button to go in the Action Row const refreshButton = new ButtonBuilder() .setCustomId('refresh') @@ -471,7 +472,10 @@ const functions = { const comparedRankings = await this.rankings.compare(interaction, guildInfo); const embed = this.builders.comparisonEmbed(comparedRankings, guildInfo); - await interaction.update(embed); + await interaction.update(embed).then(async interactionResponse => { + // console.log(interactionResponse.interaction.message); + await dbfn.setComparisonMessage(interactionResponse.interaction.message, interaction.guildId); + }); } else { await interaction.update(this.builders.errorEmbed(findMessagesResponse.status)); } @@ -533,7 +537,13 @@ const functions = { }); }, sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); + // console.log(`Begin Sleep: ${new Date(Date.now()).getSeconds()}`); + return new Promise(resolve => { + setTimeout(function () { + resolve(); + // console.log(`End Sleep: ${new Date(Date.now()).getSeconds()}`); + }, ms); + }); }, async sendReminder(guildInfo, guild) { const { guildId, reminderChannelId, reminderMessage } = guildInfo; @@ -550,6 +560,8 @@ const functions = { setTimeout(this.sendReminder(interaction), ms); }, async checkReady(client) { // Check if the guilds trees are ready to water + // let time = new Date(Date.now()); + // console.log("Ready check " + time.getSeconds()); try { // Get the guildInfos for each guild that is opted in and waiting to send a reminder const getOptedInGuildsResponse = await dbfn.getOptedInGuilds(); @@ -583,8 +595,8 @@ const functions = { // Obviously if the tree says it's Ready to be watered, it's ready if (description.includes("Ready to be watered")) { readyToWater = true; - // But sometimes the tree doesn't refresh the embed, in that case we'll do a secondary check using the - // timestamp included in the embed. + // But sometimes the tree doesn't refresh the embed, in that case we'll do a secondary check using the + // timestamp included in the embed. } else { const beginWaterTimestamp = description.indexOf(""); @@ -598,32 +610,32 @@ const functions = { if (readyToWater) { // Send the reminder message await this.sendReminder(guildInfo, guild); + guildInfo.remindedStatus = 1; + await this.refreshComparisonMessage(client, guildInfo); } - } else { - // 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); - // comparisonMessage.edit({ embeds: [embed], components: [actionRow] }); } } - // Wait for .5 seconds before looping back to check other trees - this.sleep(500).then(async () => { - await this.checkReady(client); - }); + await this.sleep(5000); + this.checkReady(client); } else { // console.log(getOptedInGuildsResponse.status); - this.sleep(5000).then(async () => { - await this.checkReady(client); - }); - return; + await this.sleep(5000); + this.checkReady(client); } } catch (err) { console.error(err); - this.sleep(5000).then(async () => { - await this.checkReady(client); - }); + await this.sleep(30000); + this.checkReady(client); + } + }, + 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; } }, diff --git a/slash-commands/compare.js b/slash-commands/compare.js index b14d4c5..e631f86 100644 --- a/slash-commands/compare.js +++ b/slash-commands/compare.js @@ -22,7 +22,9 @@ module.exports = { const comparedRankings = await fn.rankings.compare(interaction, guildInfo); const embed = fn.builders.comparisonEmbed(comparedRankings, guildInfo); - await interaction.editReply(embed); + await interaction.editReply(embed).then(async message => { + await dbfn.setComparisonMessage(message, interaction.guildId); + }); } else { await interaction.editReply(fn.builders.errorEmbed(findMessagesResponse.status)); } @@ -51,7 +53,9 @@ module.exports = { // Build the string that shows the comparison // TODO Move the string building section to fn.builders? const comparedRankings = await fn.rankings.compare(interaction, guildInfo); const embed = fn.builders.comparisonEmbed(comparedRankings, guildInfo); - await interaction.editReply(embed); + await interaction.editReply(embed).then(async message => { + await dbfn.setComparisonMessage(message.id, interaction.guildId); + }); } else { await interaction.editReply(fn.builders.errorEmbed(findMessagesResponse.status)); }