Discord_API_Bot/slash-commands/create-question.js
2024-04-08 03:45:23 -06:00

112 lines
No EOL
3.2 KiB
JavaScript

const { ticket_settings } = require('../config');
const { queryMulti } = require("../helpers/helper");
const {
SlashCommandBuilder,
PermissionFlagsBits,
EmbedBuilder
} = require("discord.js");
const answerTypeChoices = [
{
name: "Short",
value: "Short"
},
{
name: "Paragraph",
value: "Paragraph"
}
];
const answerRequiredChoices = [
{
name: "Yes",
value: "Yes"
},
{
name: "No",
value: "No"
}
];
module.exports = {
data: new SlashCommandBuilder()
.setName("create-question")
.setDescription("Create new question form ticket form")
.addStringOption(option =>
option
.setName("option")
.setRequired(true)
.setAutocomplete(true)
.setDescription("Select the ticket option.")
)
.addStringOption(option =>
option
.setMaxLength(45)
.setRequired(true)
.setName('question')
.setDescription("Enter a question.")
)
.addStringOption(option =>
option
.setRequired(true)
.setName("place-holder")
.setDescription("Enter a place holder for answer area.")
)
.addStringOption(option =>
option
.setRequired(true)
.setName("answer-type")
.addChoices(...answerTypeChoices)
.setDescription("Select type of the answer.")
)
.addStringOption(option =>
option
.setRequired(true)
.setName("answer-required")
.addChoices(...answerRequiredChoices)
.setDescription("Select whether the answer is required or not.")
)
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator),
async run(client, interaction) {
const { options } = interaction;
const ephemeral = true;
await interaction.deferReply({ ephemeral });
const ticketOption = options.getString("option");
const question = options.getString('question');
const placeHolder = options.getString("place-holder");
const answerType = options.getString("answer-type");
const answerRequired = options.getString("answer-required") === "Yes";
const authorName = {
name: interaction.guild.name
};
let embed = new EmbedBuilder()
.setColor(ticket_settings.embed_color)
.setAuthor(authorName)
.setThumbnail(ticket_settings.ticket_thumbnail)
.setDescription("You can't have more than 5 questions at the ticket form!");
const existingQuestions = await queryMulti(
db,
"SELECT * FROM questions WHERE guildID=? AND optionID=?",
[interaction.guildId, ticketOption]
);
if (existingQuestions.length > 5) {
return interaction.editReply({ embeds: [embed], ephemeral });
}
await db.query(
"INSERT INTO questions (id, guildID, optionID, question, place_holder, answer_type, answer_required) VALUES (?, ?, ?, ?, ?, ?, ?)",
[interaction.id, interaction.guildId, ticketOption, question, placeHolder, answerType, answerRequired]
);
embed.setDescription("You successfully added new question to ticket form!");
return interaction.editReply({ embeds: [embed], ephemeral });
}
};