From 9b2ad681b88ec87d5542d8a21e0ef6cf2a620420 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Thu, 26 Jan 2023 22:31:48 -0500 Subject: [PATCH] Add a ping reminder and setup command for it --- modules/dbfn.js | 37 +++++++++++++++++++++++++++++++---- modules/functions.js | 12 ++++++++++++ slash-commands/setping.js | 18 +++++++++++++++++ slash-commands/setpingrole.js | 24 +++++++++++++++++++++++ 4 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 slash-commands/setping.js create mode 100644 slash-commands/setpingrole.js diff --git a/modules/dbfn.js b/modules/dbfn.js index c4afba6..baca2c6 100644 --- a/modules/dbfn.js +++ b/modules/dbfn.js @@ -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 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 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) => { @@ -97,7 +97,8 @@ module.exports = { "treeMessageId": "123", "treeChannelId": "123", "leaderboardMessageId": "123", - "leaderboardChannelId": "123" + "leaderboardChannelId": "123", + "pingRoleId": "123" };*/ if (res.length == 0) { reject("There is no database entry for your guild yet. Try running /setup"); @@ -111,7 +112,8 @@ module.exports = { "treeMessageId": row.tree_message_id, "treeChannelId": row.tree_channel_id, "leaderboardMessageId": row.leaderboard_message_id, - "leaderboardChannelId": row.leaderboard_channel_id + "leaderboardChannelId": row.leaderboard_channel_id, + "pingRoleId": row.ping_role_id }; db.end(); resolve({ "status": "Successfully fetched guild information", "data": guildInfo }); @@ -333,5 +335,32 @@ module.exports = { resolve({ "status": "Successfully fetched historic 24hr tree.", "data": hist24hTree }); }); }); - } + }, + setPingRole(guildId, pingRoleId) { + 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 insertPingRoleQuery = `UPDATE guild_info SET ping_role_id = ${db.escape(pingRoleId)} 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(insertPingRoleQuery, (err, res) => { + if (err) { + console.error(err); + db.end(); + reject("Error updating the ping role ID: " + err.message); + return; + } + db.end(); + resolve({ "status": `Successfully set the ping role to <@&${pingRoleId}>.`, "data": res }); + }); + }); + } }; \ No newline at end of file diff --git a/modules/functions.js b/modules/functions.js index bdf0edb..11667a9 100644 --- a/modules/functions.js +++ b/modules/functions.js @@ -340,6 +340,18 @@ const functions = { } resolve(time + units); }); + }, + setReminder(interaction, time, pingRoleId) { + const reminderChannel = interaction.channel; + setTimeout(function () { + reminderChannel.send(`<@&${pingRoleId}>`).then(m => { + if (m.deletable) { + setTimeout(function() { + m.delete(); + }, 60000); + } + }); + }, time); } }; diff --git a/slash-commands/setping.js b/slash-commands/setping.js new file mode 100644 index 0000000..4d13791 --- /dev/null +++ b/slash-commands/setping.js @@ -0,0 +1,18 @@ +const { SlashCommandBuilder } = require('discord.js'); +const dbfn = require('../modules/dbfn.js'); +const fn = require('../modules/functions.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('setping') + .setDescription('Run this command when you water your tree to have a reminder sent.'), + async execute(interaction) { + await interaction.deferReply({ ephemeral: true }); + const getGuildInfoResponse = await dbfn.getGuildInfo(interaction.guildId); + const guildInfo = getGuildInfoResponse.data; + const reminderTimeS = fn.getWaterTime(guildInfo.treeHeight); + const reminderTimeMs = reminderTimeS * 1000; + fn.setReminder(interaction, reminderTimeMs, guildInfo.pingRoleId); + interaction.editReply("A reminder has been set."); + }, +}; \ No newline at end of file diff --git a/slash-commands/setpingrole.js b/slash-commands/setpingrole.js new file mode 100644 index 0000000..a79fc44 --- /dev/null +++ b/slash-commands/setpingrole.js @@ -0,0 +1,24 @@ +const { SlashCommandBuilder } = require('discord.js'); +const dbfn = require('../modules/dbfn.js'); +const fn = require('../modules/functions.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('setpingrole') + .setDescription('Set the role to ping when you run /setping') + .addRoleOption(o => + o.setName('pingrole') + .setDescription('The role to ping') + .setRequired(true)), + async execute(interaction) { + try { + await interaction.deferReply({ ephemeral: true }); + const pingRole = interaction.options.getRole('pingrole'); + const setPingRoleResponse = await dbfn.setPingRole(interaction.guildId, pingRole.id); + interaction.editReply(setPingRoleResponse.status); + } catch(err) { + console.error(err); + await interaction.editReply(fn.builders.errorEmbed(err)); + } + }, +}; \ No newline at end of file