Adding User App Support
This commit is contained in:
parent
26621c9866
commit
7e7ddcb45f
@ -7,60 +7,51 @@ module.exports = {
|
|||||||
|
|
||||||
run: async (client, interaction) => {
|
run: async (client, interaction) => {
|
||||||
|
|
||||||
// Declare a random var for the main modal - Each Session
|
// Declare a random ID for the modal to ensure unique interactions
|
||||||
let rand = Math.floor(Math.random() * 99999999999999).toString();
|
const modalId = `modal-${Math.floor(Math.random() * 99999999999999).toString()}`;
|
||||||
|
|
||||||
// Check if this is a chatInput
|
// Check if this is a chatInput command interaction
|
||||||
if (!interaction.isChatInputCommand()) return;
|
if (!interaction.isChatInputCommand()) return;
|
||||||
|
|
||||||
// await interaction.deferReply();
|
// Create the modal
|
||||||
const modal = new ModalBuilder()
|
const modal = new ModalBuilder()
|
||||||
.setCustomId(rand)
|
.setCustomId(modalId)
|
||||||
.setTitle('This is an example modal');
|
.setTitle('This is an example modal');
|
||||||
|
|
||||||
// TODO: Add components to modal...
|
// Create a text input component for the modal
|
||||||
const modalInputData = new TextInputBuilder()
|
const modalInputData = new TextInputBuilder()
|
||||||
.setCustomId('modalInput')
|
.setCustomId('modalInput')
|
||||||
// The label is the prompt the user sees for this input
|
|
||||||
.setLabel("What text do you want to send?")
|
.setLabel("What text do you want to send?")
|
||||||
// Short means only a single line of text
|
.setStyle(TextInputStyle.Paragraph); // Allows for multi-line text
|
||||||
.setStyle(TextInputStyle.Paragraph);
|
|
||||||
|
|
||||||
// An action row only holds one text input,
|
// Wrap the text input in an action row
|
||||||
// so you need one action row per text input.
|
const modalInputRow = new ActionRowBuilder().addComponents(modalInputData);
|
||||||
const modalInputRow = new ActionRowBuilder().addComponents([modalInputData]);
|
|
||||||
|
|
||||||
// Add inputs to the modal
|
// Add the input row to the modal
|
||||||
modal.addComponents([modalInputRow]);
|
modal.addComponents(modalInputRow);
|
||||||
|
|
||||||
|
// Show the modal to the user
|
||||||
await interaction.showModal(modal);
|
await interaction.showModal(modal);
|
||||||
|
|
||||||
client.on('interactionCreate', interaction => {
|
// Handle the modal submission within the same interaction listener
|
||||||
|
client.once('interactionCreate', async (modalInteraction) => {
|
||||||
|
|
||||||
// Do not continue if its a modal
|
// Ensure we are handling the correct modal submission
|
||||||
if (interaction.type == "modal") return
|
if (!modalInteraction.isModalSubmit() || modalInteraction.customId !== modalId) return;
|
||||||
// Interaction type is 5 == Modal
|
|
||||||
if (interaction.type === 5) {
|
|
||||||
|
|
||||||
// Make sure we are working with our users modal only
|
// Retrieve the data entered by the user
|
||||||
if (interaction.customId === rand) {
|
const modalInputDataString = modalInteraction.fields.getTextInputValue('modalInput');
|
||||||
|
|
||||||
// Get the data entered by the user
|
// Create an embed to display the user input
|
||||||
let modalInputDataString = interaction.fields.getTextInputValue('modalInput');
|
const embed = new EmbedBuilder()
|
||||||
|
.setColor("#FF0000")
|
||||||
console.log(modalInputDataString)
|
.setTitle("Your input!")
|
||||||
|
.setDescription(`You said: ${modalInputDataString}`)
|
||||||
const embed = new EmbedBuilder()
|
.setTimestamp()
|
||||||
// Set color to blue
|
.setFooter({ text: `Requested by ${modalInteraction.user.tag}`, iconURL: modalInteraction.user.displayAvatarURL() });
|
||||||
.setColor("#FF0000")
|
|
||||||
.setTitle("Your input!")
|
|
||||||
.setDescription(`You said: ${modalInputDataString}`)
|
|
||||||
.setTimestamp()
|
|
||||||
.setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` });
|
|
||||||
interaction.reply({ embeds: [embed] });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
// Reply to the modal submission with the embed
|
||||||
|
await modalInteraction.reply({ embeds: [embed] });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
@ -2,14 +2,14 @@ const { EmbedBuilder } = require('discord.js');
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: "ping",
|
name: "ping",
|
||||||
private: true,
|
private: true, // Mark this command as private for ephemeral replies
|
||||||
description: "Returns websocket latency",
|
description: "Returns websocket latency",
|
||||||
|
|
||||||
run: async (client, interaction) => {
|
run: async (client, interaction) => {
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
.setColor("#FF0000")
|
.setColor("#FF0000")
|
||||||
.setTitle("🏓 Pong!")
|
.setTitle("🏓 Pong!")
|
||||||
.setDescription(`Latency : ${client.ws.ping}ms`)
|
.setDescription(`Latency: ${client.ws.ping}ms`)
|
||||||
.setTimestamp()
|
.setTimestamp()
|
||||||
.setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` });
|
.setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` });
|
||||||
interaction.followUp({ embeds: [embed] });
|
interaction.followUp({ embeds: [embed] });
|
||||||
|
@ -5,93 +5,84 @@ const { promisify } = require("util");
|
|||||||
const globPromise = promisify(glob);
|
const globPromise = promisify(glob);
|
||||||
|
|
||||||
client.on("interactionCreate", async (interaction) => {
|
client.on("interactionCreate", async (interaction) => {
|
||||||
|
|
||||||
// Slash Commands
|
// Slash Commands
|
||||||
const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`);
|
const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`);
|
||||||
const arrayOfSlashCommands = [];
|
const arrayOfSlashCommands = [];
|
||||||
|
|
||||||
// Map the slash commands into data to be processed
|
// Map the slash commands into data to be processed
|
||||||
slashCommands.map((value) => {
|
slashCommands.map((value) => {
|
||||||
const file = require(value);
|
const file = require(value);
|
||||||
const splitted = value.split("/");
|
const splitted = value.split("/");
|
||||||
const directory = splitted[splitted.length - 2];
|
const directory = splitted[splitted.length - 2];
|
||||||
|
|
||||||
if (!file?.name) return;
|
if (!file?.name) return;
|
||||||
|
|
||||||
const properties = {
|
const properties = { directory, ...file };
|
||||||
directory,
|
client.slashCommands.set(file.name, properties);
|
||||||
...file
|
|
||||||
};
|
|
||||||
client.slashCommands.set(file.name, properties);
|
|
||||||
|
|
||||||
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
|
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
|
||||||
|
|
||||||
// Push the data
|
// Push the data
|
||||||
arrayOfSlashCommands.push(file);
|
const JSONCommand = {
|
||||||
|
...file,
|
||||||
|
integration_types: [0, 1], // 0 for guild, 1 for user
|
||||||
|
contexts: [0, 1, 2], // 0 for guild, 1 for app DMs, 2 for GDMs and other DMs
|
||||||
|
};
|
||||||
|
arrayOfSlashCommands.push(JSONCommand);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Slash Command Handling
|
// Slash Command Handling
|
||||||
if (interaction.isChatInputCommand()) {
|
if (interaction.isChatInputCommand()) {
|
||||||
|
let commandData = [];
|
||||||
|
|
||||||
// Grabbing Command Data for this interaction
|
// Filter to find the command for this interaction
|
||||||
let commandData = []
|
await arrayOfSlashCommands.forEach(command => {
|
||||||
|
if (command.name === interaction.commandName) {
|
||||||
// We use ForEach here to filter our array into the single commands info.
|
commandData.push(command);
|
||||||
await arrayOfSlashCommands.forEach(command => {
|
|
||||||
if (command.name == interaction.commandName) {
|
|
||||||
commandData.push(command)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Process and Parse Data
|
|
||||||
let dataToProcess = JSON.stringify(commandData[0])
|
|
||||||
let parsedData = JSON.parse(dataToProcess)
|
|
||||||
|
|
||||||
|
|
||||||
if (interaction.commandName == "modal-example"){
|
|
||||||
console.log("Modal - Skipping defer")
|
|
||||||
} else {
|
|
||||||
// If the command is private, set ephemeral true else, set false
|
|
||||||
console.log(parsedData)
|
|
||||||
if (parsedData.private == true) {
|
|
||||||
await interaction.deferReply({
|
|
||||||
ephemeral: true
|
|
||||||
}).catch(() => {});
|
|
||||||
|
|
||||||
} else {
|
|
||||||
await interaction.deferReply({
|
|
||||||
ephemeral: false
|
|
||||||
}).catch(() => {});
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Process and parse the command data
|
||||||
|
const parsedData = commandData[0];
|
||||||
|
|
||||||
|
// Defer reply based on privacy settings
|
||||||
|
if (interaction.commandName === "modal-example") {
|
||||||
|
console.log("Modal - Skipping defer");
|
||||||
|
} else {
|
||||||
|
const isPrivate = parsedData?.private;
|
||||||
|
await interaction.deferReply({
|
||||||
|
ephemeral: !!isPrivate,
|
||||||
|
}).catch(() => {});
|
||||||
}
|
}
|
||||||
const cmd = client.slashCommands.get(interaction.commandName);
|
|
||||||
if (!cmd)
|
|
||||||
return interaction.followUp({
|
|
||||||
content: "An error has occurred "
|
|
||||||
});
|
|
||||||
|
|
||||||
const args = [];
|
const cmd = client.slashCommands.get(interaction.commandName);
|
||||||
|
if (!cmd) {
|
||||||
|
return interaction.followUp({ content: "An error has occurred" });
|
||||||
|
}
|
||||||
|
|
||||||
for (let option of interaction.options.data) {
|
const args = [];
|
||||||
if (option.type === "SUB_COMMAND") {
|
for (let option of interaction.options.data) {
|
||||||
if (option.name) args.push(option.name);
|
if (option.type === "SUB_COMMAND") {
|
||||||
option.options?.forEach((x) => {
|
if (option.name) args.push(option.name);
|
||||||
if (x.value) args.push(x.value);
|
option.options?.forEach((x) => {
|
||||||
});
|
if (x.value) args.push(x.value);
|
||||||
} else if (option.value) args.push(option.value);
|
});
|
||||||
}
|
} else if (option.value) args.push(option.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the interaction is in a guild and assign member accordingly
|
||||||
|
if (interaction.inGuild()) {
|
||||||
interaction.member = interaction.guild.members.cache.get(interaction.user.id);
|
interaction.member = interaction.guild.members.cache.get(interaction.user.id);
|
||||||
|
}
|
||||||
|
|
||||||
cmd.run(client, interaction, args);
|
// Run the command
|
||||||
|
cmd.run(client, interaction, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Context Menu Handling
|
// Context Menu Handling
|
||||||
if (interaction.isContextMenuCommand()) {
|
if (interaction.isContextMenuCommand()) {
|
||||||
await interaction.deferReply({
|
await interaction.deferReply({ ephemeral: false });
|
||||||
ephemeral: false
|
const command = client.slashCommands.get(interaction.commandName);
|
||||||
});
|
if (command) command.run(client, interaction);
|
||||||
const command = client.slashCommands.get(interaction.commandName);
|
|
||||||
if (command) command.run(client, interaction);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
@ -2,11 +2,16 @@ require("dotenv").config();
|
|||||||
const { glob } = require("glob");
|
const { glob } = require("glob");
|
||||||
const { promisify } = require("util");
|
const { promisify } = require("util");
|
||||||
const globPromise = promisify(glob);
|
const globPromise = promisify(glob);
|
||||||
|
const { REST } = require('@discordjs/rest');
|
||||||
|
const Discord = require('discord.js');
|
||||||
|
|
||||||
module.exports = async (client) => {
|
module.exports = async (client) => {
|
||||||
|
const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);
|
||||||
|
|
||||||
// Slash Commands
|
// Slash Commands
|
||||||
const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`);
|
const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`);
|
||||||
const arrayOfSlashCommands = [];
|
const arrayOfSlashCommands = [];
|
||||||
|
|
||||||
slashCommands.map((value) => {
|
slashCommands.map((value) => {
|
||||||
const file = require(value);
|
const file = require(value);
|
||||||
const splitted = value.split("/");
|
const splitted = value.split("/");
|
||||||
@ -18,7 +23,15 @@ module.exports = async (client) => {
|
|||||||
client.slashCommands.set(file.name, properties);
|
client.slashCommands.set(file.name, properties);
|
||||||
|
|
||||||
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
|
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
|
||||||
arrayOfSlashCommands.push(file);
|
|
||||||
|
// Add integration types and contexts to support user apps
|
||||||
|
const JSONCommand = {
|
||||||
|
...file,
|
||||||
|
integration_types: [0, 1], // 0 for guild, 1 for user
|
||||||
|
contexts: [0, 1, 2], // 0 for guild, 1 for DMs, 2 for GDMs and other DMs
|
||||||
|
};
|
||||||
|
|
||||||
|
arrayOfSlashCommands.push(JSONCommand);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Events
|
// Events
|
||||||
@ -27,11 +40,15 @@ module.exports = async (client) => {
|
|||||||
|
|
||||||
// Slash Commands Register
|
// Slash Commands Register
|
||||||
client.on("ready", async () => {
|
client.on("ready", async () => {
|
||||||
// // Register for a single guild
|
try {
|
||||||
// await client.guilds.cache.get("GUIDIDHERE").commands.set(arrayOfSlashCommands);
|
// Register for all the guilds the bot is in
|
||||||
|
await rest.put(
|
||||||
// Register for all the guilds the bot is in
|
Discord.Routes.applicationCommands(client.user.id),
|
||||||
await client.application.commands.set(arrayOfSlashCommands);
|
{ body: arrayOfSlashCommands }
|
||||||
|
);
|
||||||
|
console.log("Successfully registered application commands.");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error while registering application commands:", error);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user