From 5227fce48da9473147c9dc8a262f1eae4da34f6b Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Mon, 21 Feb 2022 02:28:37 +0000 Subject: [PATCH] Restructure Modals to work --- c2.js | 60 ++++++++++++ package.json | 2 + slash/createTask.js | 195 +++++++++++++++++++++++++++++++++++++++ slash/create_task.js | 41 -------- slash/getBoardDetails.js | 77 +++++++++++++--- slash/ping.js | 9 +- working | 193 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 521 insertions(+), 56 deletions(-) create mode 100644 c2.js create mode 100644 slash/createTask.js delete mode 100644 slash/create_task.js create mode 100644 working diff --git a/c2.js b/c2.js new file mode 100644 index 0000000..33e8233 --- /dev/null +++ b/c2.js @@ -0,0 +1,60 @@ +const { Permissions } = require("discord.js"); +const discordModals = require('discord-modals') // Define the discord-modals package! +const { MessageActionRow, MessageSelectMenu } = require('discord.js'); +var http = require('unirest'); +var http = require('unirest'); + +exports.run = async (client, interaction) => { // eslint-disable-line no-unused-vars + discordModals(client); // discord-modals needs your client in order to interact with modals+ + //await interaction.deferReply(); + const { Modal, TextInputComponent, showModal } = require('discord-modals') // Now we extract the showModal method + + let titleCompontent = new TextInputComponent() // We create an Text Input Component + .setCustomId('title') // We set the customId to title + .setLabel('Title Name Here') + .setStyle('SHORT') //IMPORTANT: Text Input Component Style can be 'SHORT' or 'LONG' + .setMinLength(4) + .setMaxLength(15) + .setPlaceholder('Write a text here') + .setRequired(true) // If it's required or not + .setValue('value') + + let descCompontent = new TextInputComponent() // We create an Text Input Component + .setCustomId('desc') // We set the customId to title + .setLabel('Description') + .setStyle('LONG') //IMPORTANT: Text Input Component Style can be 'SHORT' or 'LONG' + .setMinLength(4) + .setMaxLength(250) + .setPlaceholder('Write a text here') + .setRequired(true) // If it's required or not + .setValue('value') + + let components = [titleCompontent, descCompontent] + console.log(components) + + const modal = new Modal() // We create a Modal + .setCustomId('title') + .setTitle('Task Information') + .addComponents(components); + // Let's say that the interaction will be an Slash Command called 'ping'. + + showModal(modal, { + client: client, // The showModal() method needs the client to send the modal through the API. + interaction: interaction // The showModal() method needs the interaction to send the modal with the Interaction ID & Token. + }) + +}; + +exports.commandData = { + name: "createtask2", + description: "Test Command", + options: [], + defaultPermission: true, +}; + +// Set guildOnly to true if you want it to be available on guilds only. +// Otherwise false is global. +exports.conf = { + permLevel: "User", + guildOnly: true +}; \ No newline at end of file diff --git a/package.json b/package.json index 54a02ae..bfebda3 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,9 @@ "axios": "^0.25.0", "colorette": "^1.3.0", "date-and-time": "^2.1.0", + "discord-modals": "^1.1.8", "discord.js": "^13.0.1", + "discordjs-modal": "^1.0.0-beta.2", "dotenv": "^10.0.0", "enmap": "^5.8.5", "jsonfile": "^6.1.0", diff --git a/slash/createTask.js b/slash/createTask.js new file mode 100644 index 0000000..5d55c4c --- /dev/null +++ b/slash/createTask.js @@ -0,0 +1,195 @@ +// For HTTP Requests +var http = require('unirest'); +let count = 0 + +const jsonfile = require('jsonfile') + +const { MessageActionRow, MessageSelectMenu } = require('discord.js'); + +require("dotenv").config(); + +exports.run = async (client, interaction) => { + let interactionT = interaction; + let boardList = [] + let embed = [] + let pages = [] + let projectID + let title + let userID + let inter = interaction + var rand = Math.floor(Math.random() * 999999999); + // eslint-disable-line no-unused-vars + await interaction.deferReply(); + console.log("DeferReply called") + + const jsonfile = require('jsonfile') + const usercache = 'cache/' + interaction.user.id + '.user' + jsonfile.readFile(usercache) + .then(obj => { + + userID = obj.userid + + + // Setting up our Request, using getAllProjects method + var Request = http.get('https://' + process.env.ROOT_DOMAIN + '/jsonrpc.php').headers({ Accept: 'application/json', 'Content-Type': 'application/json' }).send({ "jsonrpc": "2.0", "method": "getAllProjects", "id": 0 }); + + // Begin the request and send authenication using the jsonrpc2.0 protocol. + Request.auth({ + user: 'jsonrpc', + pass: process.env.KANBOARD_API_KEY, + sendImmediately: false + }).then(function (response) { + // console.log(response) + // We have a response, lets set up a var + let data = response.body.result + + // Setting up the correct formatting for our paginator + const pusherFunc = board => boardList.push({ label: board.name, description: board.name, value: board.id }); + data.forEach(pusherFunc); + + console.log(boardList) + const row = new MessageActionRow() + .addComponents( + new MessageSelectMenu() + .setCustomId('select') + .setPlaceholder('Select a Project') + .addOptions(boardList), + ); + + (async function () { + await interaction.editReply({ content: 'Use the menu below to choose a Project', components: [row] }); + })() + + // await interaction.reply({ content: 'Pong!', components: [row] }); + client.on('interactionCreate', interaction => { + if (!interaction.isSelectMenu()) return; + console.log(interaction) + }) + + client.on('modalSubmit', (modal) => { + + if (interaction.customId === 'select') { + console.log(interaction.values[0]); + projectID = interaction.values[0] + + let titleCompontent = new TextInputComponent() // We create an Text Input Component + .setCustomId('title') // We set the customId to title + .setLabel('Title Name Here') + .setStyle('SHORT') //IMPORTANT: Text Input Component Style can be 'SHORT' or 'LONG' + .setMinLength(4) + .setMaxLength(15) + .setPlaceholder('Write a text here') + .setRequired(true) // If it's required or not + .setValue('value') + + let descCompontent = new TextInputComponent() // We create an Text Input Component + .setCustomId('desc') // We set the customId to title + .setLabel('Description') + .setStyle('LONG') //IMPORTANT: Text Input Component Style can be 'SHORT' or 'LONG' + .setMinLength(4) + .setMaxLength(250) + .setPlaceholder('Write a text here') + .setRequired(true) // If it's required or not + .setValue('value') + + let components = [titleCompontent, descCompontent] + console.log(components) + + const modal = new Modal() // We create a Modal + .setCustomId('title') + .setTitle('Task Information') + .addComponents(components); + + + + + if (modal.customId === 'title') { + showModal(modal, { + client: client, // The showModal() method needs the client to send the modal through the API. + interaction: interaction // The showModal() method needs the interaction to send the modal with the Interaction ID & Token. + }) + + + } + } + + (async () => { + + title = modal.getTextInputValue('title') + desc = modal.getTextInputValue('desc') + var Request = http.post('https://' + process.env.ROOT_DOMAIN + '/jsonrpc.php').headers({ Accept: 'application/json', 'Content-Type': 'application/json' }).send({ + "jsonrpc": "2.0", + "method": "createTask", + "id": 1176509098, + "params": { + "owner_id": userID, + "creator_id": userID, + "date_due": "", + "description": desc, + "category_id": 0, + "score": 0, + "title": title, + "project_id": projectID, + "color_id": "yellow", + "column_id": 0, + "recurrence_status": 0, + "recurrence_trigger": 0, + "recurrence_factor": 0, + "recurrence_timeframe": 0, + "recurrence_basedate": 0 + } + }); + + // Begin the request and send authenication using the jsonrpc2.0 protocol. + Request.auth({ + user: 'jsonrpc', + pass: process.env.KANBOARD_API_KEY, + sendImmediately: true + }).then(function (response) { + (async () => { + + console.log(response.body) + + + await interaction.editReply({ content: 'Task Created!', components: [] }); + + })() + boardList = [] + embed = [] + pages = [] + projectID = null + userID = null + title = null + desc = null + count = 0 + titleCompontent = null + descCompontent = null + components = null + modal = null + rand = Math.floor(Math.random() * 999999999); + + } + ) + })() + + + }) + + }) + + }) +} + +exports.commandData = { + name: "createtask", + description: "generate a task", + // options: [{ "name": "name", "description": "The name of your new task", "type": 3, "name": "projectname", "description": "The name of the project to create in", "type": 3 }], + defaultPermission: true, +}; + +// Set guildOnly to true if you want it to be available on guilds only. +// Otherwise false is global. +exports.conf = { + permLevel: "User", + guildOnly: true +}; \ No newline at end of file diff --git a/slash/create_task.js b/slash/create_task.js deleted file mode 100644 index c740c79..0000000 --- a/slash/create_task.js +++ /dev/null @@ -1,41 +0,0 @@ -// For HTTP Requests -var http = require('unirest'); -let boardList=[] -let embed = [] -let pages =[] - - -require("dotenv").config(); - -exports.run = async (client, interaction) => { - - // eslint-disable-line no-unused-vars - await interaction.deferReply(); - // const reply = await interaction.editReply("Ping?"); - // await interaction.editReply(`Pong! Latency is ${reply.createdTimestamp - interaction.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms.`); - var Request = http.post('https://' + process.env.ROOT_DOMAIN + '/jsonrpc.php').headers({ Accept: 'application/json', 'Content-Type': 'application/json' }).send({ "jsonrpc": "2.0", "method": "getAllProjects", "id": 0 }); - - Request.auth({ - user: 'jsonrpc', - pass: process.env.KANBOARD_API_KEY, - sendImmediately: false - }).then(function (response) { - let data = response.body.result - console.log(data) - - }) -}; - -exports.commandData = { - name: "test", - description: "create a task", - options: [], - defaultPermission: true, -}; - -// Set guildOnly to true if you want it to be available on guilds only. -// Otherwise false is global. -exports.conf = { - permLevel: "User", - guildOnly: false -}; \ No newline at end of file diff --git a/slash/getBoardDetails.js b/slash/getBoardDetails.js index d677043..7371c35 100644 --- a/slash/getBoardDetails.js +++ b/slash/getBoardDetails.js @@ -86,26 +86,28 @@ exports.run = async (client, interaction) => { data.columns.forEach(pusherFunc); let dataTasks = response.body.result[0].columns - dataTasks.forEach(function(column) { - if (column.tasks != []){ - column.tasks.forEach(function(task) { - // console.log(task) + dataTasks.forEach(function (column) { + if (column.tasks != []) { + column.tasks.forEach(function (task) { + // console.log(task) //console.log (task.title + " in " + task.column_name + " for " + task.project_name) if (task.is_active == 0) return tasksInfo.push(task.title + " in " + task.column_name + "\n") - - })} - }); - console.log(tasksInfo) + + }) + } + }); + console.log(tasksInfo) const mainEmbed = new MessageEmbed() + // { name: 'link', value: "https://" + process.env.ROOT_DOMAIN + "/board/" + data.project_id, inline: true }) + .setColor('#0099ff') .addFields( { name: 'Project ID', value: data.project_id, inline: true }, { name: 'Swimlane Description', value: desc }, { name: 'Task Info', value: columns.join(" "), inline: true }, - { name: 'Open Task Breakdown', value: tasksInfo.join(" "), inline: false }, - { name: 'link', value: "https://" + process.env.ROOT_DOMAIN + "/board/" + data.project_id, inline: true }) + { name: 'Open Task Breakdown', value: tasksInfo.join(" "), inline: false }) .setTitle('Requested Details') .setDescription("Swimlane: " + data.name) .setFooter(date.format(now, 'MM/DD/YYYY hh:mm:ss')); @@ -114,7 +116,7 @@ exports.run = async (client, interaction) => { await interaction.editReply({ embeds: [mainEmbed] }); // Clear the list boardList = []; - columns = []; + columns = []; desc = ""; tasksInfo = []; })(); @@ -123,6 +125,59 @@ exports.run = async (client, interaction) => { } + if (!interaction.isSelectMenu()) return; + + console.log(interaction) + + + if (interaction.customId === 'select') { + console.log(interaction.values[0]); + projectID = interaction.values[0] + + + + const discordModals = require('discord-modals') // Define the discord-modals package! + const { Modal, TextInputComponent, showModal } = require('discord-modals') // Now we extract the showModal method + discordModals(client); // Provide the client to the discord-modals package + + + let titleCompontent = new TextInputComponent() // We create an Text Input Component + .setCustomId('title') // We set the customId to title + .setLabel('Title Name Here') + .setStyle('SHORT') //IMPORTANT: Text Input Component Style can be 'SHORT' or 'LONG' + .setMinLength(4) + .setMaxLength(15) + .setPlaceholder('Write a text here') + .setRequired(true) // If it's required or not + .setValue('value') + + let descCompontent = new TextInputComponent() // We create an Text Input Component + .setCustomId('desc') // We set the customId to title + .setLabel('Description') + .setStyle('LONG') //IMPORTANT: Text Input Component Style can be 'SHORT' or 'LONG' + .setMinLength(4) + .setMaxLength(250) + .setPlaceholder('Write a text here') + .setRequired(true) // If it's required or not + .setValue('value') + + let components = [titleCompontent, descCompontent] + console.log(components) + + const modal = new Modal() // We create a Modal + .setCustomId('title') + .setTitle('Task Information') + .addComponents(components); + + showModal(modal, { + client: client, // The showModal() method needs the client to send the modal through the API. + interaction: interaction // The showModal() method needs the interaction to send the modal with the Interaction ID & Token. + }) + + } + + + }) }) diff --git a/slash/ping.js b/slash/ping.js index 62ff1ed..db8adc0 100644 --- a/slash/ping.js +++ b/slash/ping.js @@ -1,8 +1,9 @@ exports.run = async (client, interaction) => { // eslint-disable-line no-unused-vars - await interaction.deferReply(); - const reply = await interaction.editReply("Ping?"); - await interaction.editReply(`Pong! Latency is ${reply.createdTimestamp - interaction.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms.`); -}; + // await interaction.deferReply(); + // const reply = await interaction.editReply("Ping?"); + // await interaction.editReply(`Pong! Latency is ${reply.createdTimestamp - interaction.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms.`); + +} exports.commandData = { name: "ping", diff --git a/working b/working new file mode 100644 index 0000000..c4b99b6 --- /dev/null +++ b/working @@ -0,0 +1,193 @@ +// For HTTP Requests +var http = require('unirest'); + + +const jsonfile = require('jsonfile') + +const { MessageActionRow, MessageSelectMenu } = require('discord.js'); + +require("dotenv").config(); + +exports.run = async (client, interaction) => { + let boardList = [] + let embed = [] + let pages = [] + let projectID + let title + let userID + // eslint-disable-line no-unused-vars + //await interaction.deferReply(); + + + const jsonfile = require('jsonfile') + const usercache = 'cache/' + interaction.user.id + '.user' + jsonfile.readFile(usercache) + .then(obj => { + + userID = obj.userid + + + // Setting up our Request, using getAllProjects method + var Request = http.get('https://' + process.env.ROOT_DOMAIN + '/jsonrpc.php').headers({ Accept: 'application/json', 'Content-Type': 'application/json' }).send({ "jsonrpc": "2.0", "method": "getAllProjects", "id": 0 }); + + // Begin the request and send authenication using the jsonrpc2.0 protocol. + Request.auth({ + user: 'jsonrpc', + pass: process.env.KANBOARD_API_KEY, + sendImmediately: false + }).then(function (response) { + // console.log(response) + // We have a response, lets set up a var + let data = response.body.result + + // Setting up the correct formatting for our paginator + const pusherFunc = board => boardList.push({ label: board.name, description: board.name, value: board.id }); + data.forEach(pusherFunc); + + console.log(boardList) + const row = new MessageActionRow() + .addComponents( + new MessageSelectMenu() + .setCustomId('select') + .setPlaceholder('Select a Project') + .addOptions(boardList), + ); + + (async function () { + await interaction.editReply({ content: 'Use the menu below to choose a Project', components: [row] }); + })() + + // await interaction.reply({ content: 'Pong!', components: [row] }); + client.on('interactionCreate', interaction => { + if (!interaction.isSelectMenu()) return; + + console.log(interaction) + + + if (interaction.customId === 'select') { + console.log(interaction.values[0]); + projectID = interaction.values[0] + + + + const discordModals = require('discord-modals') // Define the discord-modals package! + const { Modal, TextInputComponent, showModal } = require('discord-modals') // Now we extract the showModal method + discordModals(client); // Provide the client to the discord-modals package + + + let titleCompontent = new TextInputComponent() // We create an Text Input Component + .setCustomId('title') // We set the customId to title + .setLabel('Title Name Here') + .setStyle('SHORT') //IMPORTANT: Text Input Component Style can be 'SHORT' or 'LONG' + .setMinLength(4) + .setMaxLength(15) + .setPlaceholder('Write a text here') + .setRequired(true) // If it's required or not + .setValue('value') + + let descCompontent = new TextInputComponent() // We create an Text Input Component + .setCustomId('desc') // We set the customId to title + .setLabel('Description') + .setStyle('LONG') //IMPORTANT: Text Input Component Style can be 'SHORT' or 'LONG' + .setMinLength(4) + .setMaxLength(250) + .setPlaceholder('Write a text here') + .setRequired(true) // If it's required or not + .setValue('value') + + let components = [titleCompontent, descCompontent] + console.log(components) + + const modal = new Modal() // We create a Modal + .setCustomId('title') + .setTitle('Task Information') + .addComponents(components); + + showModal(modal, { + client: client, // The showModal() method needs the client to send the modal through the API. + interaction: interaction // The showModal() method needs the interaction to send the modal with the Interaction ID & Token. + }) + + + client.on('modalSubmit', (modal) => { + if (modal.customId === 'title') { + (async () => { + + title = modal.getTextInputValue('title') + desc = modal.getTextInputValue('desc') + var Request = http.post('https://' + process.env.ROOT_DOMAIN + '/jsonrpc.php').headers({ Accept: 'application/json', 'Content-Type': 'application/json' }).send({ + "jsonrpc": "2.0", + "method": "createTask", + "id": 1176509098, + "params": { + "owner_id": userID, + "creator_id": userID, + "date_due": "", + "description": desc, + "category_id": 0, + "score": 0, + "title": title, + "project_id": projectID, + "color_id": "yellow", + "column_id": 0, + "recurrence_status": 0, + "recurrence_trigger": 0, + "recurrence_factor": 0, + "recurrence_timeframe": 0, + "recurrence_basedate": 0 + } + }); + + // Begin the request and send authenication using the jsonrpc2.0 protocol. + Request.auth({ + user: 'jsonrpc', + pass: process.env.KANBOARD_API_KEY, + sendImmediately: false + }).then(function (response) { + console.log(response.body) + }) + + await modal.reply({ content: 'Task Created!', components: [] }); + // boardList = [] + // embed = [] + // pages = [] + projectID = null + userID = null + title = null + desc = null + titleCompontent = null + descCompontent = null + components = null + + interaction = null + modal = null + })() + + } + + }) + + } + + + }) + }) + + }) + +}; + + +exports.commandData = { + name: "createtask", + description: "generate a task", + // options: [{ "name": "name", "description": "The name of your new task", "type": 3, "name": "projectname", "description": "The name of the project to create in", "type": 3 }], + defaultPermission: true, +}; + +// Set guildOnly to true if you want it to be available on guilds only. +// Otherwise false is global. +exports.conf = { + permLevel: "User", + guildOnly: true +}; \ No newline at end of file