Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { StringSelectMenuBuilder, ActionRowBuilder, StringSelectMenuOptionBuilder, EmbedBuilder, inlineCode, bold, italic, ButtonBuilder, ButtonStyle, CommandInteraction } = require('discord.js');
- const uuid = require("../../../utils/generateUUID");
- const userGlobalData = require("../../../model/userGlobalData");
- const { forhire } = require("../../../model/postApplication");
- /**
- * Function to handle creating or updating a draft for a "for hire" post.
- * @param {Client} client - The Discord bot client.
- * @param {CommandInteraction} interaction - The command interaction object.
- * @param {Array} userRoleData - An array of user role data.
- * @param {String} postId - Optional post ID.
- */
- module.exports = async (client, interaction, userRoleData, postId) => {
- const id = postId || uuid();
- const userPostData = await forhire.findOne({ postId: postId || id });
- const userData = await userGlobalData.findOne({ userId: interaction.user.id });
- const draftTemplate = {
- draftId: id,
- draftType: "forhire",
- draftTitle: userPostData?.postTitle || "<Untitled Post>",
- }
- if (!userPostData) {
- await forhire.create({
- postId: id,
- userId: interaction.user.id
- });
- userData.postDrafts = [...userData.postDrafts, draftTemplate];
- await userData.save();
- } else {
- let draft = userData.postDrafts.find((x) => x.draftId === postId);
- if (draft) {
- draft = draftTemplate
- await userData.save();
- } else {
- userData.postDrafts = [...userData.postDrafts, draftTemplate];
- await userData.save();
- };
- };
- // Checking if the string is a Valid URL
- const isValidUrl = urlString=> {
- try {
- return Boolean(new URL(urlString));
- }
- catch(e){
- return false;
- }
- }
- const text = `Here's a preview of your post. Use the editor below to change how it looks.
- Your changes are saved automatically. If you'd like to continue where you left off, run ${inlineCode("/post")} and select "Edit an existing post"
- You cannot submit for approval until required fields (*) are filled out.
- If the bot does not allow you to post in a particular hireable channel, you do not have the relevant role for that channel.`
- const money = `${bold("Robux")}: R$${userPostData?.robuxValue || "null"}\n${bold("USD")}: $${userPostData?.usdValue || "null"}\n${bold("Percentage")}: ${userPostData?.percentageValue || "null"}%`
- const links = userPostData?.pastworkLinks || ""
- let separator = " ";
- if (links.includes(",")) {
- separator = ",";
- }
- // Split the links based on the separator
- const linkArray = links.split(separator).map(link => link.trim());
- // Format the links using Markdown syntax
- const formattedLinks = linkArray.map(link => {
- const [url] = link.split(" ");
- return `[link](${url})`;
- });
- // Join the formatted links back together with the same separator
- const markdownFormatted = formattedLinks.join(separator + ", ");
- // Draft embed
- const embed = new EmbedBuilder()
- .setTitle(`${userPostData?.postTitle || "<Untitled Post>"}`)
- .setAuthor({ name: interaction.user.username, iconURL: interaction.user.avatarURL() })
- .setDescription(`${userPostData?.postDescription || "<No description provided>"}`)
- .setFooter({ text: `Post ID: (${userPostData?.postId || id}) • Draft` })
- .addFields(
- { name: "Payment", value: money, inline: true },
- { name: "Payment Type", value: userPostData?.paymentType || italic("Nothing provided"), inline: true },
- { name: "Portfolio", value: userPostData?.protfolioLink || italic("Nothing provided") },
- { name: "Past Works", value: markdownFormatted || italic("Nothing provided") },
- { name: "Contact", value: `<@${interaction.user.id}>` },
- )
- .setColor("Green");
- if (userPostData?.postThumbnail !== "" && isValidUrl(userPostData?.postThumbnail)) {
- embed.setThumbnail(userPostData?.postThumbnail)
- };
- if (userPostData?.postImage !== "" && isValidUrl(userPostData?.postImage)) {
- embed.setImage(userPostData?.postImage)
- };
- await interaction.update({ content: text, embeds: [embed], components: await createPostButtons(interaction.user.id, id, userPostData) })
- .catch(error => console.log(error));
- }
- async function createPostButtons(userId, postId, userPostData) {
- const menuChannelHolder = [];
- const avaliableChannels = await getChannelsFromSkills(userId);
- for (const channel of avaliableChannels) {
- menuChannelHolder.push(
- new StringSelectMenuOptionBuilder()
- .setLabel(`${channel[0]}`)
- .setEmoji("📦")
- .setDefault(channel[0] === userPostData?.channel)
- .setDescription(channel[1])
- .setValue(channel[0])
- );
- };
- let editRequiredText = "*Edit post info (2 required)"
- let editRequiredColor = ButtonStyle.Danger
- let portfolioRequiredText = "*Edit portfolio (at least 1 required)"
- let portfolioRequiredColor = ButtonStyle.Danger
- let paymentRequiredText = "*Edit payment info (at least 1 required)"
- let paymentRequiredColor = ButtonStyle.Danger
- const addedValue =
- userPostData?.robuxValue !== "" ||
- userPostData?.usdValue !== "" ||
- userPostData?.percentageValue !== "";
- // Helper function to check if a value is defined and not empty
- function isDefinedAndNotEmpty(value) {
- return value !== undefined && value !== "";
- }
- // Check payment requirements
- if (addedValue && isDefinedAndNotEmpty(userPostData?.paymentType)) {
- paymentRequiredText = "Edit payment info";
- paymentRequiredColor = ButtonStyle.Secondary;
- }
- // Check edit requirements
- if (isDefinedAndNotEmpty(userPostData?.postTitle) || isDefinedAndNotEmpty(userPostData?.postDescription)) {
- editRequiredText = "*Edit post info (1 required)";
- editRequiredColor = ButtonStyle.Danger;
- }
- // Check full edit requirements
- if (isDefinedAndNotEmpty(userPostData?.postTitle) && isDefinedAndNotEmpty(userPostData?.postDescription)) {
- editRequiredText = "Edit post info";
- editRequiredColor = ButtonStyle.Secondary;
- }
- // Check portfolio requirements
- if (isDefinedAndNotEmpty(userPostData?.protfolioLink) && isDefinedAndNotEmpty(userPostData?.paymentType)) {
- portfolioRequiredText = "Edit portfolio";
- portfolioRequiredColor = ButtonStyle.Secondary;
- }
- const canSubmit =
- userPostData?.paymentType !== "" &&
- userPostData?.channel !== "" &&
- addedValue &&
- editRequiredColor !== ButtonStyle.Danger &&
- portfolioRequiredColor !== ButtonStyle.Danger &&
- paymentRequiredColor !== ButtonStyle.Danger;
- let enabled = undefined
- if (
- portfolioRequiredColor === 2 &&
- editRequiredColor === 2 &&
- isDefinedAndNotEmpty(userPostData?.paymentType)
- ) {
- enabled = true
- } else {
- enabled = false
- }
- const buttons = [
- edit = new ButtonBuilder()
- .setCustomId(`edit_forhire_post_info|${postId}`)
- .setLabel(editRequiredText)
- .setStyle(editRequiredColor),
- payment = new ButtonBuilder()
- .setCustomId(`edit_forhire_post_payment|${postId}`)
- .setLabel(paymentRequiredText)
- .setDisabled(!enabled)
- .setStyle(paymentRequiredColor),
- portfolio = new ButtonBuilder()
- .setCustomId(`edit_forhire_post_portfolio|${postId}`)
- .setLabel(portfolioRequiredText)
- .setStyle(portfolioRequiredColor)
- ];
- const lastButtons = [
- submit = new ButtonBuilder()
- .setCustomId(`submit_forhire_post|${postId}`)
- .setLabel('Submit for approval')
- .setDisabled(!canSubmit)
- .setStyle(ButtonStyle.Success),
- back = new ButtonBuilder()
- .setCustomId(`back_to_drafts|${postId}`)
- .setLabel("Go back")
- .setStyle(ButtonStyle.Secondary)
- ]
- const channel = new StringSelectMenuBuilder()
- .setCustomId(`edit_forhire_channel_type|${postId}`)
- .setPlaceholder('Select a channel to post in')
- .setMaxValues(1)
- .addOptions(menuChannelHolder);
- const paymentTypes = new StringSelectMenuBuilder()
- .setCustomId(`edit_forhire_post_payment_type|${postId}`)
- .setPlaceholder('Select a payment type')
- .setMaxValues(1)
- .addOptions([
- new StringSelectMenuOptionBuilder()
- .setLabel('Up-front')
- .setDescription("Up-front payment.")
- .setValue('Up-front'),
- new StringSelectMenuOptionBuilder()
- .setLabel('Partial up-front')
- .setDescription("Partial up-front payment.")
- .setValue('Partial up-front'),
- new StringSelectMenuOptionBuilder()
- .setLabel('Upon completion')
- .setDescription("One-time payment.")
- .setValue('Upon completion'),
- new StringSelectMenuOptionBuilder()
- .setLabel('Per-task')
- .setDescription("Per-task payment.")
- .setValue('Per-task'),
- ]);
- for (const option of paymentTypes.options) {
- const eOP = option.data
- if (eOP.value === userPostData?.paymentType) {
- eOP.default = true;
- } else {
- eOP.default = false;
- }
- };
- return [
- new ActionRowBuilder()
- .addComponents(buttons),
- new ActionRowBuilder()
- .addComponents(channel),
- new ActionRowBuilder()
- .addComponents(paymentTypes),
- new ActionRowBuilder()
- .addComponents(lastButtons),
- ];
- }
- const channels = {
- ["Luau Programmer"]: [
- cName = "scripter-hiring",
- desc = "For hiring Roblox-related scripting jobs"
- ],
- ["JavaScript Programmer"]: [
- cName = "programmer-hiring",
- desc = "For hiring Off-site programming jobs"
- ],
- }
- async function getChannelsFromSkills(userId) {
- const data = await userGlobalData.findOne({ userId });
- if (!data) return [];
- const skillChannels = []
- for (const skill of data.skillRoles) {
- if (channels[skill]) {
- skillChannels.push(channels[skill]);
- }
- }
- return skillChannels;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement