From 74ee1bfd185796e1b7bbc71d6896d5cc6d47c1b6 Mon Sep 17 00:00:00 2001 From: Jakemunro Date: Tue, 20 Aug 2024 10:20:23 -0400 Subject: [PATCH 1/2] Add channel selection for users in multiple programs --- slash/trade.js | 122 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 94 insertions(+), 28 deletions(-) diff --git a/slash/trade.js b/slash/trade.js index 67d2590..6d208de 100644 --- a/slash/trade.js +++ b/slash/trade.js @@ -1,4 +1,4 @@ -const Discord = require("discord.js"); +const { MessageActionRow, MessageSelectMenu } = require("discord.js"); const messages = require("../utils/message"); const ms = require("ms"); module.exports = { @@ -24,12 +24,12 @@ module.exports = { }; // Check if the member has one of the allowed roles - const userRole = interaction.member.roles.cache.find((r) => + const userRoles = interaction.member.roles.cache.filter((r) => Object.keys(allowedRoles).includes(r.name) ); // If the member doesn't have any of the allowed roles - if (!userRole) { + if (userRoles.size === 0) { return interaction.reply({ content: "❌ | You need to have the appropriate web, data or cyber role to start a shift swap.", @@ -37,30 +37,96 @@ module.exports = { }); } - const channelId = allowedRoles[userRole.name]; - const tradeChannel = client.channels.cache.get(channelId); - - const tradeDuration = ms(1000 * 60 * 15); - const tradeWinnerCount = 1; - const tradePrize = interaction.options.getString("shift"); - - await interaction.deferReply({ ephemeral: true }); - - // start giveaway - client.giveawaysManager.start(tradeChannel, { - // The giveaway duration - duration: ms(tradeDuration), - // The giveaway prize - prize: tradePrize, - // The giveaway Host - hostedBy: `<@${interaction.user.id}>`, - // The giveaway winner count - winnerCount: parseInt(tradeWinnerCount), - messages, - }); - interaction.editReply({ - content: `You dropped your shift in ${tradeChannel}!`, - ephemeral: true, - }); + if (userRoles.size === 1) { + // If the user has only one relevant role, use the corresponding channel + const userRoleName = userRoles.first().name; + const channelId = allowedRoles[userRoleName]; + const tradeChannel = client.channels.cache.get(channelId); + + // Use tradeChannel to start the giveaway + const tradeDuration = ms(1000 * 60 * 15); + const tradeWinnerCount = 1; + const tradePrize = interaction.options.getString("shift"); + + await interaction.deferReply({ ephemeral: true }); + + client.giveawaysManager.start(tradeChannel, { + duration: tradeDuration, + prize: tradePrize, + hostedBy: `<@${interaction.user.id}>`, + winnerCount: tradeWinnerCount, + messages, + }); + + interaction.editReply({ + content: `You dropped your shift in ${tradeChannel}!`, + ephemeral: true, + }); + } else { + // If the user has more than one relevant role, present a choice + const options = userRoles.map((role) => ({ + label: role.name, + value: allowedRoles[role.name], + })); + + const row = new MessageActionRow().addComponents( + new MessageSelectMenu() + .setCustomId("select-channel") + .setPlaceholder("Select a channel") + .addOptions(options) + ); + + await interaction.reply({ + content: "Please select the channel where you want to post:", + components: [row], + ephemeral: true, + }); + + // Await the user's choice + const filter = (i) => + i.customId === "select-channel" && i.user.id === interaction.user.id; + const collector = interaction.channel.createMessageComponentCollector({ + filter, + time: 60000, + }); + + collector.on("collect", async (i) => { + const channelId = i.values[0]; + const tradeChannel = client.channels.cache.get(channelId); + + if (!tradeChannel) { + return i.reply({ + content: "❌ | The selected channel could not be found.", + ephemeral: true, + }); + } + + await i.update({ + content: `You selected: ${tradeChannel.name}`, + components: [], + ephemeral: true, + }); + + const tradeDuration = ms(1000 * 60 * 15); + const tradeWinnerCount = 1; + const tradePrize = interaction.options.getString("shift"); + + await interaction.deferReply({ ephemeral: true }); + + // Start giveaway + client.giveawaysManager.start(tradeChannel, { + duration: tradeDuration, + prize: tradePrize, + hostedBy: `<@${interaction.user.id}>`, + winnerCount: tradeWinnerCount, + messages, + }); + + interaction.editReply({ + content: `You dropped your shift in ${tradeChannel}!`, + ephemeral: true, + }); + }); + } }, }; From fdf630b2a71e35a0d17e1f66f964fdcee63c0e8b Mon Sep 17 00:00:00 2001 From: Jakemunro Date: Tue, 20 Aug 2024 11:03:47 -0400 Subject: [PATCH 2/2] Fixed error of multiple defers --- slash/trade.js | 84 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 30 deletions(-) diff --git a/slash/trade.js b/slash/trade.js index 6d208de..adab395 100644 --- a/slash/trade.js +++ b/slash/trade.js @@ -1,6 +1,7 @@ const { MessageActionRow, MessageSelectMenu } = require("discord.js"); const messages = require("../utils/message"); const ms = require("ms"); + module.exports = { name: "trade", description: "🎉 Drop a shift!", @@ -28,7 +29,6 @@ module.exports = { Object.keys(allowedRoles).includes(r.name) ); - // If the member doesn't have any of the allowed roles if (userRoles.size === 0) { return interaction.reply({ content: @@ -37,33 +37,56 @@ module.exports = { }); } + const tradeDurationStr = "15m"; // This is the duration string + const tradeDuration = ms(tradeDurationStr); // Convert to milliseconds + + if (!tradeDuration || tradeDuration <= 0) { + return interaction.reply({ + content: `❌ | Invalid trade duration: "${tradeDurationStr}".`, + ephemeral: true, + }); + } + + const tradeWinnerCount = 1; + const tradePrize = interaction.options.getString("shift"); + if (userRoles.size === 1) { - // If the user has only one relevant role, use the corresponding channel const userRoleName = userRoles.first().name; const channelId = allowedRoles[userRoleName]; const tradeChannel = client.channels.cache.get(channelId); - // Use tradeChannel to start the giveaway - const tradeDuration = ms(1000 * 60 * 15); - const tradeWinnerCount = 1; - const tradePrize = interaction.options.getString("shift"); + if (!tradeChannel) { + return interaction.reply({ + content: "❌ | The corresponding channel could not be found.", + ephemeral: true, + }); + } await interaction.deferReply({ ephemeral: true }); - client.giveawaysManager.start(tradeChannel, { - duration: tradeDuration, - prize: tradePrize, - hostedBy: `<@${interaction.user.id}>`, - winnerCount: tradeWinnerCount, - messages, - }); + // Start giveaway in the channel + client.giveawaysManager + .start(tradeChannel, { + duration: tradeDuration, + prize: tradePrize, + hostedBy: `<@${interaction.user.id}>`, + winnerCount: tradeWinnerCount, + messages, + }) + .catch((error) => { + console.error("Error starting giveaway:", error); + interaction.editReply({ + content: + "❌ | Failed to start the giveaway. Please try again later.", + ephemeral: true, + }); + }); - interaction.editReply({ + return interaction.editReply({ content: `You dropped your shift in ${tradeChannel}!`, ephemeral: true, }); } else { - // If the user has more than one relevant role, present a choice const options = userRoles.map((role) => ({ label: role.name, value: allowedRoles[role.name], @@ -82,7 +105,6 @@ module.exports = { ephemeral: true, }); - // Await the user's choice const filter = (i) => i.customId === "select-channel" && i.user.id === interaction.user.id; const collector = interaction.channel.createMessageComponentCollector({ @@ -107,20 +129,22 @@ module.exports = { ephemeral: true, }); - const tradeDuration = ms(1000 * 60 * 15); - const tradeWinnerCount = 1; - const tradePrize = interaction.options.getString("shift"); - - await interaction.deferReply({ ephemeral: true }); - - // Start giveaway - client.giveawaysManager.start(tradeChannel, { - duration: tradeDuration, - prize: tradePrize, - hostedBy: `<@${interaction.user.id}>`, - winnerCount: tradeWinnerCount, - messages, - }); + client.giveawaysManager + .start(tradeChannel, { + duration: tradeDuration, + prize: tradePrize, + hostedBy: `<@${interaction.user.id}>`, + winnerCount: tradeWinnerCount, + messages, + }) + .catch((error) => { + console.error("Error starting giveaway:", error); + interaction.editReply({ + content: + "❌ | Failed to start the giveaway. Please try again later.", + ephemeral: true, + }); + }); interaction.editReply({ content: `You dropped your shift in ${tradeChannel}!`,