From e9708f8045551c4cb2b1512e62849dc331426518 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sun, 12 Feb 2023 15:29:38 -0500 Subject: [PATCH] Role menu ready for testing --- data/strings.json | 11 +- main.js | 33 ++++-- modules/dbfn.js | 229 ------------------------------------- modules/functions.js | 79 +++++++++++++ slash-commands/rolemenu.js | 8 +- 5 files changed, 119 insertions(+), 241 deletions(-) diff --git a/data/strings.json b/data/strings.json index c6f6084..2760e2c 100644 --- a/data/strings.json +++ b/data/strings.json @@ -19,7 +19,16 @@ }, "embeds": { "footer": "Silvanus is not affiliated with Grow A Tree or Limbo Labs", - "color": "0x55FF55" + "color": "0x55FF55", + "roleMenuTitle": "Role Menu", + "treeRoleMenu": [ + "Use the buttons below to give yourself roles.\n\n", + "``💧`` - ", + ": Get notifications when the tree is ready to be watered.", + "\n``🍎`` - ", + ": Get notifications when fruit is falling from the tree." + ], + "roleMenuFooter": "Tip: Tap the button again to remove the role." }, "emoji": { "joint": "<:joint:862082955902976000>", diff --git a/main.js b/main.js index 92e8eed..740156f 100644 --- a/main.js +++ b/main.js @@ -56,16 +56,29 @@ 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 == 'deleteping') { - if (interaction.message.deletable) { - await interaction.message.delete().catch(err => { - console.error(err); - }); + if (interaction.isButton()) { + switch (interaction.component.customId) { + case 'refresh': + // console.log(JSON.stringify(interaction)); + await fn.refresh(interaction).catch(err => { + interaction.channel.send(fn.builders.errorEmbed(err)); + }); + break; + case 'deleteping': + if (interaction.message.deletable) { + await interaction.message.delete().catch(err => { + console.error(err); + }); + } + break; + case 'waterpingrole': + await interaction.reply(fn.buttonHandlers.waterPing(interaction)).catch(err => console.error(err)); + break; + case 'fruitpingrole': + await interaction.reply(fn.buttonHandlers.fruitPing(interaction)).catch(err => console.error(err)); + break; + default: + break; } } }); diff --git a/modules/dbfn.js b/modules/dbfn.js index e41e73f..086a224 100644 --- a/modules/dbfn.js +++ b/modules/dbfn.js @@ -144,35 +144,6 @@ module.exports = { }); }); }, - setTreeInfo(guildInfo) { - 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": null }) - // guildInfo = { "guildId": "123", "treeName": "name", "treeHeight": 123, "treeMessageId": "123", "treeChannelId": "123", "leaderboardMessageId": "123", "leaderboardChannelId": "123"} - // Set a server's tree information in the database) - const insertGuildInfoQuery = `INSERT INTO guild_info (guild_id, tree_name, tree_height, tree_message_id, tree_channel_id) VALUES (${db.escape(guildInfo.guildId)}, ${db.escape(guildInfo.treeName)}, ${db.escape(guildInfo.treeHeight)},${db.escape(guildInfo.treeMessageId)}, ${db.escape(guildInfo.treeChannelId)}) ON DUPLICATE KEY UPDATE tree_name = ${db.escape(guildInfo.treeName)},tree_height = ${db.escape(guildInfo.treeHeight)},tree_message_id = ${db.escape(guildInfo.treeMessageId)},tree_channel_id = ${db.escape(guildInfo.treeChannelId)}`; - // TODO run this query and return a promise, then resolve with { "status": , "data": null } - return new Promise((resolve, reject) => { - db.query(insertGuildInfoQuery, (err, res) => { - if (err) { - console.error(err); - db.end(); - reject("Error setting the guild info: " + err.message); - return; - } - db.end(); - resolve({ "status": "Successfully set the guild information", "data": null }); - }); - }); - }, setLeaderboardInfo(guildInfo) { const db = mysql.createConnection({ host: process.env.DBHOST, @@ -338,205 +309,5 @@ module.exports = { resolve({ "status": "Successfully fetched historic 24hr tree.", "data": hist24hTree }); }); }); - }, - setReminderInfo(guildInfo) { - 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 insertReminderInfoQuery = `UPDATE guild_info SET waterMessage = ${db.escape(guildInfo.waterMessage)}, ping_channel_id = ${db.escape(reminderChannelId)}, fruit_message = ${db.escape(guildInfo.fruitMessage)} 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(insertReminderInfoQuery, (err, res) => { - if (err) { - console.error(err); - db.end(); - reject("Error updating the reminder info: " + err.message); - return; - } - db.end(); - resolve({ "status": `Your notification relay has been set up.\nWater Message: "${guildInfo.waterMessage}"\nFruit Message: "${guildInfo.fruitMessage}"\nRelay Channel: <#${guildInfo.reminderChannelId}>\nWatch Channel: <#${guildInfo.notificationChannelId}>`, "data": res }); - }); - }); - }, - setRemindedStatus(guildId, remindedStatus) { - 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 reminded_status = ${db.escape(remindedStatus)} 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 reminded status: " + err.message); - return; - } - db.end(); - resolve({ "status": `Successfully set the reminded status to ${remindedStatus}`, "data": res }); - // console.log("Boop: " + remindedStatus); - }); - }); - }, - setReminderOptIn(guildId, optIn) { - 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 setReminderOptInQuery = `UPDATE guild_info SET reminder_optin = ${db.escape(optIn)} 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(setReminderOptInQuery, (err, res) => { - if (err) { - console.error(err); - db.end(); - reject("Error updating the reminder opt-in status: " + err.message); - return; - } - db.end(); - resolve({ "status": `Successfully set the reminder opt-in status to ${optIn}`, "data": res }); - }); - }); - }, - getOptedInGuilds() { - 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}`; - }); - // Get a server's tree information from the database - const getOptedInGuildsQuery = `SELECT * FROM guild_info WHERE reminder_optin = 1`; - // 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(getOptedInGuildsQuery, (err, res) => { - if (err) { - console.error(err); - reject("Error fetching guild information: " + err.message); - db.end(); - return; - } - /*const guildInfo = { "guildId": "123", - "treeName": "name", - "treeHeight": 123, - "treeMessageId": "123", - "treeChannelId": "123", - "leaderboardMessageId": "123", - "leaderboardChannelId": "123", - "reminderMessage": "Abc", - "reminderChannelId": "123", - "remindedStatus": 0, - "comparisonMessageId": "123" - };*/ - if (res.length == 0) { - resolve({ "status": "No servers have opted in yet" }); - db.end(); - return; - } - row = res[0]; - let guilds = []; - res.forEach(row => { - guilds.push({ - "guildId": row.guild_id, - "treeName": row.tree_name, - "treeHeight": row.tree_height, - "treeMessageId": row.tree_message_id, - "treeChannelId": row.tree_channel_id, - "leaderboardMessageId": row.leaderboard_message_id, - "leaderboardChannelId": row.leaderboard_channel_id, - "reminderMessage": row.ping_role_id, - "reminderChannelId": row.ping_channel_id, - "remindedStatus": row.reminded_status, - "comparisonMessageId": row.comparison_message_id, - "comparisonChannelId": row.comparison_channel_id - }); - }); - db.end(); - resolve({ "status": "Successfully fetched guild information", "data": guilds }); - }); - }); - }, - setComparisonMessage(comparisonMessage, 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 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) => { - db.query(setComparisonMessageQuery, (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: ${comparisonMessage}`, "data": res }); - }); - }); - }, - setNotificationChannel(id, 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 query = `UPDATE guild_info SET notification_channel_id = ${db.escape(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) => { - db.query(query, (err, res) => { - if (err) { - console.error(err); - db.end(); - reject("Error updating the notification channel ID: " + err.message); - return; - } - db.end(); - resolve({ "status": `Successfully set the notification channel ID: ${comparisonMessage}`, "data": res }); - }); - }); } }; \ No newline at end of file diff --git a/modules/functions.js b/modules/functions.js index 75d90de..57494f8 100644 --- a/modules/functions.js +++ b/modules/functions.js @@ -69,6 +69,49 @@ const functions = { refreshButton ); return refreshActionRow; + }, + treeRoleMenu() { + return new ActionRowBuilder() + .addComponents( + this.buttons.waterPing(), + this.buttons.fruitPing() + ); + }, + buttons: { + acceptRules() { + return new ButtonBuilder() + .setCustomId('acceptrules') + .setLabel(`${strings.emoji.confirm} Accept Rules`) + .setStyle(ButtonStyle.Primary); + }, + waterPing() { + return new ButtonBuilder() + .setCustomId('waterpingrole') + .setLabel(strings.emoji.water) + .setStyle(ButtonStyle.Primary); + }, + fruitPing() { + return new ButtonBuilder() + .setCustomId('fruitpingrole') + .setLabel(strings.emoji.fruit) + .setStyle(ButtonStyle.Primary); + } + } + }, + embeds: { + treeRoleMenu(guildInfo) { + const actionRow = functions.builders.actionRows.treeRoleMenu(); + let tempStrings = strings.embeds.treeRoleMenu; + let description = tempStrings[0] + tempStrings[1] + `<@&${guildInfo.waterRoleId}>` + tempStrings[2]; + if (guildInfo.fruitRoleId != undefined) { + description += tempStrings[3] + `<@&${guildInfo.fruitRoleId}>` + tempStrings[4]; + } + const embed = new EmbedBuilder() + .setColor(strings.embeds.color) + .setTitle(strings.embeds.roleMenuTitle) + .setDescription(description) + .setFooter({ text: strings.embeds.roleMenuFooter }); + return { embeds: [embed], components: [actionRow] }; } }, comparisonEmbed(content, guildInfo) { @@ -428,6 +471,42 @@ const functions = { } } }, + buttonHandlers: { + async fruitPing(interaction) { + if (interaction.client.guildInfos.has(interaction.guildId)) { + let guildInfo = interaction.client.guildInfos.get(interaction.guildId); + const role = await functions.roles.fetchRole(interaction.guild, guildInfo.fruitRoleId); + let status = "No Changes Made"; + if (interaction.member.roles.cache.some(role => role.id == guildInfo.fruitRoleId)) { + await functions.roles.takeRole(interaction.member, role); + status = "Removed the fruit role."; + } else { + await functions.roles.giveRole(interaction.member, role); + status = "Added the fruit role."; + } + return functions.builders.embed(status); + } else { + throw "Guild doesn't exist in database!"; + } + }, + async waterPing(interaction) { + if (interaction.client.guildInfos.has(interaction.guildId)) { + let guildInfo = interaction.client.guildInfos.get(interaction.guildId); + let status = "No Changes Made"; + const role = await functions.roles.fetchRole(interaction.guild, guildInfo.waterRoleId); + if (interaction.member.roles.cache.some(role => role.id == guildInfo.waterRoleId)) { + await functions.roles.takeRole(interaction.member, role); + status = "Removed the water role."; + } else { + await functions.roles.giveRole(interaction.member, role); + status = "Added the water role."; + } + return functions.builders.embed(status); + } else { + throw "Guild doesn't exist in database!"; + } + } + }, async refresh(interaction) { // const getGuildInfoResponse = await dbfn.getGuildInfo(interaction.guildId); // let guildInfo = getGuildInfoResponse.data; diff --git a/slash-commands/rolemenu.js b/slash-commands/rolemenu.js index 39806c8..13f39d5 100644 --- a/slash-commands/rolemenu.js +++ b/slash-commands/rolemenu.js @@ -15,6 +15,12 @@ module.exports = { .setRequired(false)), async execute(interaction) { await interaction.deferReply().catch(err => console.error(err)); - await interaction.editReply(fn.builders.embeds.treeRoleMenu()).catch(err => console.error(err)); + if (interaction.client.guildInfos.has(interaction.guildId)) { + let guildInfo = interaction.client.guildInfos.get(interaction.guildId); + guildInfo.setRoles(interaction.options.getRole('waterrole').id, interaction.options.getRole('fruitrole').id); + await interaction.editReply(fn.builders.embeds.treeRoleMenu(guildInfo)).catch(err => console.error(err)); + } else { + await interaction.editReply(fn.builders.errorEmbed("No information is known about your server yet, please run /setup or /compare")).catch(err => console.error(err)); + } }, }; \ No newline at end of file