Updates to water reminders

This commit is contained in:
Skylar Grant 2023-01-31 22:30:20 -05:00
parent 7dfe6a19af
commit b2fbb5012c
4 changed files with 49 additions and 26 deletions

View File

@ -24,6 +24,7 @@ const client = new Client({
// Various imports // Various imports
const fn = require('./modules/functions.js'); const fn = require('./modules/functions.js');
const strings = require('./data/strings.json'); const strings = require('./data/strings.json');
const dbfn = require('./modules/dbfn.js');
const isDev = process.env.isDev; const isDev = process.env.isDev;
client.once('ready', () => { client.once('ready', () => {
@ -55,16 +56,22 @@ client.on('interactionCreate', async interaction => {
} }
if (interaction.isButton() && interaction.component.customId == 'refresh') { if (interaction.isButton() && interaction.component.customId == 'refresh') {
// console.log(JSON.stringify(interaction));
await fn.refresh(interaction).catch(err => { await fn.refresh(interaction).catch(err => {
interaction.channel.send(fn.builders.errorEmbed(err)); interaction.channel.send(fn.builders.errorEmbed(err));
}); });
} else if (interaction.isButton() && interaction.component.customId == 'resetping') { } else if (interaction.isButton() && interaction.component.customId == 'resetping') {
await fn.resetPing(interaction); await fn.resetPing(interaction);
await fn.refresh(interaction).catch(err => { 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') { } else if (interaction.isButton() && interaction.component.customId == 'deleteping') {
if (interaction.message.deletable) { 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 => { await interaction.message.delete().catch(err => {
console.error(err); console.error(err);
}); });

View File

@ -509,7 +509,7 @@ module.exports = {
if (err) throw `Error connecting to the database: ${err.message}`; if (err) throw `Error connecting to the database: ${err.message}`;
}); });
// Returns a Promise, resolve({ "status": "", "data": leaderboard }) // 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)); // console.log(JSON.stringify(comparisonMessage));
// TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard } // TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -53,6 +53,7 @@ const functions = {
return actionRow; return actionRow;
}, },
comparisonActionRow(guildInfo) { comparisonActionRow(guildInfo) {
// console.log(guildInfo);
// Create the button to go in the Action Row // Create the button to go in the Action Row
const refreshButton = new ButtonBuilder() const refreshButton = new ButtonBuilder()
.setCustomId('refresh') .setCustomId('refresh')
@ -471,7 +472,10 @@ const functions = {
const comparedRankings = await this.rankings.compare(interaction, guildInfo); const comparedRankings = await this.rankings.compare(interaction, guildInfo);
const embed = this.builders.comparisonEmbed(comparedRankings, 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 { } else {
await interaction.update(this.builders.errorEmbed(findMessagesResponse.status)); await interaction.update(this.builders.errorEmbed(findMessagesResponse.status));
} }
@ -533,7 +537,13 @@ const functions = {
}); });
}, },
sleep(ms) { 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) { async sendReminder(guildInfo, guild) {
const { guildId, reminderChannelId, reminderMessage } = guildInfo; const { guildId, reminderChannelId, reminderMessage } = guildInfo;
@ -550,6 +560,8 @@ const functions = {
setTimeout(this.sendReminder(interaction), ms); setTimeout(this.sendReminder(interaction), ms);
}, },
async checkReady(client) { // Check if the guilds trees are ready to water 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 { try {
// Get the guildInfos for each guild that is opted in and waiting to send a reminder // Get the guildInfos for each guild that is opted in and waiting to send a reminder
const getOptedInGuildsResponse = await dbfn.getOptedInGuilds(); const getOptedInGuildsResponse = await dbfn.getOptedInGuilds();
@ -598,32 +610,32 @@ const functions = {
if (readyToWater) { if (readyToWater) {
// Send the reminder message // Send the reminder message
await this.sendReminder(guildInfo, guild); await this.sendReminder(guildInfo, guild);
} guildInfo.remindedStatus = 1;
} else { await this.refreshComparisonMessage(client, guildInfo);
// 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.sleep(5000);
await this.checkReady(client); this.checkReady(client);
});
} else { } else {
// console.log(getOptedInGuildsResponse.status); // console.log(getOptedInGuildsResponse.status);
this.sleep(5000).then(async () => { await this.sleep(5000);
await this.checkReady(client); this.checkReady(client);
});
return;
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
this.sleep(5000).then(async () => { await this.sleep(30000);
await this.checkReady(client); 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; return;
} }
}, },

View File

@ -22,7 +22,9 @@ module.exports = {
const comparedRankings = await fn.rankings.compare(interaction, guildInfo); const comparedRankings = await fn.rankings.compare(interaction, guildInfo);
const embed = fn.builders.comparisonEmbed(comparedRankings, 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 { } else {
await interaction.editReply(fn.builders.errorEmbed(findMessagesResponse.status)); 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? // 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 comparedRankings = await fn.rankings.compare(interaction, guildInfo);
const embed = fn.builders.comparisonEmbed(comparedRankings, 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 { } else {
await interaction.editReply(fn.builders.errorEmbed(findMessagesResponse.status)); await interaction.editReply(fn.builders.errorEmbed(findMessagesResponse.status));
} }