Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel selection #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 94 additions & 28 deletions slash/trade.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -24,43 +24,109 @@ 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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be .length instead of .size

return interaction.reply({
content:
"❌ | You need to have the appropriate web, data or cyber role to start a shift swap.",
ephemeral: true,
});
}

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,
});
});
}
},
};