From fdfc2fc88ab096de8505e9c4aa97f1e6a5d9e891 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sun, 19 Feb 2023 19:20:35 -0500 Subject: [PATCH] Move role menu setup and allow everyone to send it --- data/strings.json | 3 +- modules/CustomClasses.js | 15 ++++--- modules/functions.js | 2 +- slash-commands/rolemenu.js | 24 +++-------- slash-commands/setup.js | 88 +++++++++++++++++++++++++++----------- 5 files changed, 82 insertions(+), 50 deletions(-) diff --git a/data/strings.json b/data/strings.json index 84b339d..a595ff7 100755 --- a/data/strings.json +++ b/data/strings.json @@ -59,7 +59,8 @@ "missingTreeMessage": "There was a problem finding the Tree message. Please make sure the ``/tree`` and ``/top trees`` messages are this channel, or run to set the ``/tree`` and ``/top trees`` channels.", "missingTreeChannel": "There was a problem finding the Tree channel, was it deleted? Please make sure the ``/tree`` and ``/top trees`` messages are in this channel, or run to set the ``/tree`` and ``/top trees`` channels.", "reset": "All guild configuration information has been removed from the database.", - "resetError": "There was a problem deleting your guild information, contact @voidf1sh#0420 for help." + "resetError": "There was a problem deleting your guild information, contact @voidf1sh#0420 for help.", + "noRoleMenu": "A role menu has not been created for this guild yet. Run to create a role menu." }, "notifications": { "water": "is ready to be watered again!", diff --git a/modules/CustomClasses.js b/modules/CustomClasses.js index 9f1eac3..7c72c7c 100755 --- a/modules/CustomClasses.js +++ b/modules/CustomClasses.js @@ -54,9 +54,9 @@ module.exports = { this.watchChannelId = watchChannelId; return this; } - setRoles(waterRole, fruitRole) { - this.waterRoleId = waterRole.id; - if (fruitRole) this.fruitRoleId = fruitRole.id; + setRoles(waterRoleId, fruitRoleId) { + this.waterRoleId = waterRoleId; + if (fruitRoleId) this.fruitRoleId = fruitRoleId; return this; } queryBuilder(query) { @@ -134,9 +134,12 @@ module.exports = { case "setRoles": if (this.fruitRoleId != "") { queryParts = [ - `UPDATE guild_info SET water_role_id = ${db.escape(this.waterRoleId)}, `, - `fruit_role_id = ${db.escape(this.fruitRoleId)} `, - `WHERE guild_id = ${db.escape(this.guildId)}` + `INSERT INTO guild_info (`, + `guild_id, water_role_id, fruit_role_id`, + `) VALUES (`, + `${db.escape(this.guildId)}, ${db.escape(this.waterRoleId)}, ${db.escape(this.fruitRoleId)}`, + `) ON DUPLICATE KEY UPDATE water_role_id = ${db.escape(this.waterRoleId)}, `, + `fruit_role_id = ${db.escape(this.fruitRoleId)}` ]; } else { queryParts = [ diff --git a/modules/functions.js b/modules/functions.js index 4e6ba57..9bbb07f 100755 --- a/modules/functions.js +++ b/modules/functions.js @@ -543,7 +543,7 @@ const functions = { const comparedRankings = await this.rankings.compare(interaction, guildInfo); const embed = this.builders.comparisonEmbed(comparedRankings, guildInfo); - await interaction.update(embed).catch(err => console.error(err)); + await interaction.update(embed).catch(e => console.error(e)); } else { await interaction.update(this.builders.errorEmbed(findMessagesResponse.status)); } diff --git a/slash-commands/rolemenu.js b/slash-commands/rolemenu.js index d10f7c3..f2ee244 100755 --- a/slash-commands/rolemenu.js +++ b/slash-commands/rolemenu.js @@ -1,30 +1,18 @@ -const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js'); -const dbfn = require('../modules/dbfn.js'); +const { SlashCommandBuilder } = require('discord.js'); const fn = require('../modules/functions.js'); +const strings = require('../data/strings.json'); module.exports = { data: new SlashCommandBuilder() .setName('rolemenu') - .setDescription('Send a self-assignable role selection menu') - .addRoleOption(o => - o.setName('waterrole') - .setDescription('The role for water reminder pings') - .setRequired(true)) - .addRoleOption(o => - o.setName('fruitrole') - .setDescription('The role for fruit alert pings') - .setRequired(false)) - .setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles), + .setDescription('Send a self-assignable role selection menu'), async execute(interaction) { - await interaction.deferReply().catch(err => console.error(err)); + await interaction.deferReply().catch(e => console.error(e)); if (interaction.client.guildInfos.has(interaction.guildId)) { let guildInfo = interaction.client.guildInfos.get(interaction.guildId); - guildInfo.setRoles(interaction.options.getRole('waterrole'), interaction.options.getRole('fruitrole')); - await dbfn.setGuildInfo(guildInfo.queryBuilder("setRoles")); - await fn.collectionBuilders.guildInfos(interaction.client); - await interaction.editReply(fn.builders.embeds.treeRoleMenu(guildInfo)).catch(err => console.error(err)); + await interaction.editReply(fn.builders.embeds.treeRoleMenu(guildInfo)).catch(e => console.error(e)); } 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)); + await interaction.editReply(fn.builders.errorEmbed(strings.status.noRoleMenu)).catch(e => console.error(e)); } }, }; \ No newline at end of file diff --git a/slash-commands/setup.js b/slash-commands/setup.js index 16ee532..3ac0069 100755 --- a/slash-commands/setup.js +++ b/slash-commands/setup.js @@ -2,37 +2,77 @@ const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js'); const fn = require('../modules/functions.js'); const strings = require('../data/strings.json'); const dbfn = require('../modules/dbfn.js'); +const { GuildInfo } = require('../modules/CustomClasses.js'); module.exports = { data: new SlashCommandBuilder() .setName('setup') .setDescription('Attempt automatic configuration of the bot.') - .addChannelOption(o => - o.setName('treechannel') - .setDescription('What channel is your tree in?') - .setRequired(true)) - .addChannelOption(o => - o.setName('leaderboardchannel') - .setDescription('If your leaderboard isn\'t in the same channel, where is it?') - .setRequired(false)) + .addSubcommand(sc => + sc.setName('compare') + .setDescription('Set up the channels to be used with /compare') + .addChannelOption(o => + o.setName('treechannel') + .setDescription('What channel is your tree in?') + .setRequired(true) + ) + .addChannelOption(o => + o.setName('leaderboardchannel') + .setDescription('If your leaderboard isn\'t in the same channel, where is it?') + .setRequired(false) + ) + ) + .addSubcommand(sc => + sc.setName('rolemenu') + .setDescription('Setup the roles to be used with /rolemenu') + .addRoleOption(o => + o.setName('waterrole') + .setDescription('The role for water reminder pings') + .setRequired(true) + ) + .addRoleOption(o => + o.setName('fruitrole') + .setDescription('The role for fruit alert pings') + .setRequired(false) + ) + ) .setDefaultMemberPermissions(PermissionFlagsBits.Administrator), async execute(interaction) { await interaction.deferReply({ ephemeral: true }); - /**/ - let guildInfo = { - "guildId": interaction.guildId, - "treeName": "", - "treeHeight": 0, - "treeMessageId": "", - "treeChannelId": `${interaction.options.getChannel('treechannel').id }`, - "leaderboardMessageId": "", - "leaderboardChannelId": `${interaction.options.getChannel('leaderboardchannel').id || interaction.options.getChannel('treechannel').id }`, - "reminderMessage": "", - "reminderChannelId": "", - "remindedStatus": 0, - "reminderOptIn": 0, - }; - const findMessagesResponse = await fn.messages.find(interaction, guildInfo); - interaction.editReply(findMessagesResponse.status); + const subcommand = interaction.options.getSubcommand(); + switch (subcommand) { + case "compare": + if (interaction.client.guildInfos.has(interaction.guildId)) { + let guildInfo = interaction.client.guildInfos.get(interaction.guildId); + const findMessagesResponse = await fn.messages.find(interaction, guildInfo); + await interaction.editReply(findMessagesResponse.status).catch(e => console.error(e)); + } else { + let guildInfo = new GuildInfo() + .setId(interaction.guildId); + const findMessagesResponse = await fn.messages.find(interaction, guildInfo); + await interaction.editReply(findMessagesResponse.status).catch(e => console.error(e)); + } + break; + case "rolemenu": + let waterRoleId = interaction.options.getRole('waterrole').id; + let fruitRoleId = interaction.options.getRole('fruitrole') ? interaction.options.getRole('fruitrole').id : undefined; + if (interaction.client.guildInfos.has(interaction.guildId)) { + let guildInfo = interaction.client.guildInfos.get(interaction.guildId); + guildInfo.setRoles(waterRoleId, fruitRoleId); + await dbfn.setGuildInfo(guildInfo.queryBuilder("setRoles")); + await fn.collectionBuilders.guildInfos(interaction.client); + await interaction.editReply(fn.builders.embeds.treeRoleMenu(guildInfo)).catch(e => console.error(e)); + } else { + let guildInfo = new GuildInfo() + .setId(interaction.guildId); + guildInfo.setRoles(waterRoleId, fruitRoleId); + await dbfn.setGuildInfo(guildInfo.queryBuilder("setRoles")); + await fn.collectionBuilders.guildInfos(interaction.client); + await interaction.editReply(fn.builders.embeds.treeRoleMenu(guildInfo)).catch(e => console.error(e)); + } + break; + default: + break; + } }, }; \ No newline at end of file