Changing the way reminders are deleted

This commit is contained in:
Skylar Grant 2023-01-30 19:28:12 -05:00
parent db7af90cae
commit 73541fbec6
4 changed files with 178 additions and 116 deletions

14
main.js
View File

@ -55,12 +55,20 @@ client.on('interactionCreate', async interaction => {
}
if (interaction.isButton() && interaction.component.customId == 'refresh') {
fn.refresh(interaction).catch(err => {
await fn.refresh(interaction).catch(err => {
interaction.update(fn.builders.errorEmbed(err));
});
} else if (interaction.isButton() && interaction.component.customId == 'resetping') {
fn.resetPing(interaction);
interaction.reply({ content: "Water Readiness Detection System: [ARMED]", ephemeral: true });
await fn.resetPing(interaction);
await fn.refresh(interaction).catch(err => {
interaction.update(fn.builders.errorEmbed(err));
});
} else if (interaction.isButton() && interaction.component.customId == 'deleteping') {
if (interaction.message.deletable) {
await interaction.message.delete().catch(err => {
console.error(err);
});
}
}
});

View File

@ -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, ping_role_id, ping_channel_id, reminded_status 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, reminder_optin 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) => {
@ -108,7 +108,8 @@ module.exports = {
return;
}
row = res[0];
const guildInfo = { "guildId": guildId,
const guildInfo = {
"guildId": guildId,
"treeName": row.tree_name,
"treeHeight": row.tree_height,
"treeMessageId": row.tree_message_id,
@ -117,7 +118,8 @@ module.exports = {
"leaderboardChannelId": row.leaderboard_channel_id,
"reminderMessage": row.ping_role_id,
"reminderChannelId": row.ping_channel_id,
"remindedStatus": row.reminded_status
"remindedStatus": row.reminded_status,
"reminderOptIn": row.reminder_optin
};
db.end();
resolve({ "status": "Successfully fetched guild information", "data": guildInfo });
@ -487,5 +489,32 @@ module.exports = {
resolve({ "status": "Successfully fetched guild information", "data": guilds });
});
});
},
setComparisonMessage(comparisonMessageId, guildId) {
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 comparison_message_id = ${db.escape(comparisonMessageId)} 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 comparison message ID: " + err.message);
return;
}
db.end();
resolve({ "status": `Successfully set the comparison message ID: ${comparisonMessageId}`, "data": res });
});
});
}
};

View File

@ -42,6 +42,43 @@ const functions = {
}
},
builders: {
actionRows: {
reminderActionRow() {
const deleteButton = new ButtonBuilder()
.setCustomId('deleteping')
.setEmoji('♻️')
.setStyle(ButtonStyle.Danger);
const actionRow = new ActionRowBuilder()
.addComponents(deleteButton);
return actionRow;
},
comparisonActionRow(guildInfo) {
// Create the button to go in the Action Row
const refreshButton = new ButtonBuilder()
.setCustomId('refresh')
.setEmoji('🔄')
.setStyle(ButtonStyle.Primary);
// Create the Action Row with the Button in it, to be sent with the Embed
let refreshActionRow = new ActionRowBuilder()
.addComponents(
refreshButton
);
if (guildInfo.reminderOptIn == 1 && guildInfo.remindedStatus == 1) {
const resetPingButton = new ButtonBuilder()
.setCustomId('resetping')
.setLabel('Reset Ping')
.setStyle(ButtonStyle.Secondary);
refreshActionRow.addComponents(resetPingButton);
} else if (guildInfo.reminderOptIn == 1 && guildInfo.remindedStatus == 0) {
const resetPingButton = new ButtonBuilder()
.setCustomId('resetping')
.setLabel('[Armed]')
.setStyle(ButtonStyle.Secondary);
refreshActionRow.addComponents(resetPingButton);
}
return refreshActionRow;
}
},
async refreshAction(guildId) {
// Create the button to go in the Action Row
const refreshButton = new ButtonBuilder()
@ -64,14 +101,14 @@ const functions = {
}
return refreshActionRow;
},
comparisonEmbed(content, refreshActionRow) {
comparisonEmbed(content, guildInfo) {
// Create the embed using the content passed to this function
const embed = new EmbedBuilder()
.setColor(strings.embeds.color)
.setTitle('Tree Growth Comparison')
.setTitle('Tallest Trees Comparison')
.setDescription(content)
.setFooter({ text: `v${package.version} - ${strings.embeds.footer}` });
const messageContents = { embeds: [embed], components: [refreshActionRow] };
const messageContents = { embeds: [embed], components: [this.actionRows.comparisonActionRow(guildInfo)] };
return messageContents;
},
reminderEmbed(content, guildInfo) {
@ -80,8 +117,8 @@ const functions = {
.setColor(strings.embeds.color)
.setTitle('Water Reminder')
.setDescription(`[Click here to go to your Tree](https://discord.com/channels/${guildInfo.guildId}/${guildInfo.treeChannelId}/${guildInfo.treeMessageId})`)
.setFooter({ text: `This message will self-destruct in 60 seconds.` });
const messageContents = { content: content, embeds: [embed] };
.setFooter({ text: `Click ♻️ to delete this message` });
const messageContents = { content: content, embeds: [embed], components: [this.actionRows.reminderActionRow()] };
return messageContents;
},
helpEmbed(content, private) {
@ -432,10 +469,8 @@ const functions = {
await this.rankings.parse(interaction, guildInfo);
// Build the string that shows the comparison // TODO Move the string building section to fn.builders?
const comparedRankings = await this.rankings.compare(interaction, guildInfo);
// Build the Action Row that will contain the Refresh and Reset Ping buttons
const compareActionRow = await this.builders.refreshAction(interaction.guildId);
const embed = this.builders.comparisonEmbed(comparedRankings, compareActionRow);
const embed = this.builders.comparisonEmbed(comparedRankings, guildInfo);
await interaction.update(embed);
} else {
await interaction.update(this.builders.errorEmbed(findMessagesResponse.status));
@ -506,11 +541,7 @@ const functions = {
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);
}
return 1;
}).catch(err => {
console.error(err);
});
@ -560,8 +591,8 @@ const functions = {
});
}
},
resetPing(interaction) {
dbfn.setRemindedStatus(interaction.guildId, 0);
async resetPing(interaction) {
await dbfn.setRemindedStatus(interaction.guildId, 0);
}
};

View File

@ -20,10 +20,8 @@ module.exports = {
await fn.rankings.parse(interaction, guildInfo);
// Build the string that shows the comparison // TODO Move the string building section to fn.builders?
const comparedRankings = await fn.rankings.compare(interaction, guildInfo);
// Build the Action Row that will contain the Refresh and Reset Ping buttons
const compareActionRow = await fn.builders.refreshAction(interaction.guildId);
const embed = fn.builders.comparisonEmbed(comparedRankings, compareActionRow);
const embed = fn.builders.comparisonEmbed(comparedRankings, guildInfo);
await interaction.editReply(embed);
} else {
await interaction.editReply(fn.builders.errorEmbed(findMessagesResponse.status));
@ -52,11 +50,7 @@ module.exports = {
if (findMessagesResponse.code == 1) {
// Build the string that shows the comparison // TODO Move the string building section to fn.builders?
const comparedRankings = await fn.rankings.compare(interaction, guildInfo);
// Build the Action Row that will contain the Refresh and Reset Ping buttons
const compareActionRow = await fn.builders.refreshAction(interaction.guildId);
const embed = fn.builders.comparisonEmbed(comparedRankings, compareActionRow);
const embed = fn.builders.comparisonEmbed(comparedRankings, guildInfo);
await interaction.editReply(embed);
} else {
await interaction.editReply(fn.builders.errorEmbed(findMessagesResponse.status));