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
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);
});

View File

@ -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) => {

View File

@ -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();
@ -598,32 +610,32 @@ const functions = {
if (readyToWater) {
// Send the reminder message
await this.sendReminder(guildInfo, guild);
}
} 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] });
guildInfo.remindedStatus = 1;
await this.refreshComparisonMessage(client, guildInfo);
}
}
// 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;
}
},

View File

@ -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));
}