forked from snxraven/DiscordJS-v14-Template
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
|
|
||||||
let modalInputDataString = interaction.fields.getTextInputValue('modalInput');
|
|
||||||
|
|
||||||
console.log(modalInputDataString)
|
|
||||||
|
|
||||||
|
// Create an embed to display the user input
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
// Set color to blue
|
|
||||||
.setColor("#FF0000")
|
.setColor("#FF0000")
|
||||||
.setTitle("Your input!")
|
.setTitle("Your input!")
|
||||||
.setDescription(`You said: ${modalInputDataString}`)
|
.setDescription(`You said: ${modalInputDataString}`)
|
||||||
.setTimestamp()
|
.setTimestamp()
|
||||||
.setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` });
|
.setFooter({ text: `Requested by ${modalInteraction.user.tag}`, iconURL: modalInteraction.user.displayAvatarURL() });
|
||||||
interaction.reply({ embeds: [embed] });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
// Reply to the modal submission with the embed
|
||||||
|
await modalInteraction.reply({ embeds: [embed] });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
@ -2,7 +2,7 @@ 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) => {
|
||||||
|
@ -5,7 +5,6 @@ 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 = [];
|
||||||
@ -18,61 +17,50 @@ client.on("interactionCreate", async (interaction) => {
|
|||||||
|
|
||||||
if (!file?.name) return;
|
if (!file?.name) return;
|
||||||
|
|
||||||
const properties = {
|
const properties = { directory, ...file };
|
||||||
directory,
|
|
||||||
...file
|
|
||||||
};
|
|
||||||
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;
|
||||||
|
|
||||||
// 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 = []
|
|
||||||
|
|
||||||
// We use ForEach here to filter our array into the single commands info.
|
|
||||||
await arrayOfSlashCommands.forEach(command => {
|
await arrayOfSlashCommands.forEach(command => {
|
||||||
if (command.name == interaction.commandName) {
|
if (command.name === interaction.commandName) {
|
||||||
commandData.push(command)
|
commandData.push(command);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Process and Parse Data
|
// Process and parse the command data
|
||||||
let dataToProcess = JSON.stringify(commandData[0])
|
const parsedData = commandData[0];
|
||||||
let parsedData = JSON.parse(dataToProcess)
|
|
||||||
|
|
||||||
|
// Defer reply based on privacy settings
|
||||||
if (interaction.commandName == "modal-example"){
|
if (interaction.commandName === "modal-example") {
|
||||||
console.log("Modal - Skipping defer")
|
console.log("Modal - Skipping defer");
|
||||||
} else {
|
} else {
|
||||||
// If the command is private, set ephemeral true else, set false
|
const isPrivate = parsedData?.private;
|
||||||
console.log(parsedData)
|
|
||||||
if (parsedData.private == true) {
|
|
||||||
await interaction.deferReply({
|
await interaction.deferReply({
|
||||||
ephemeral: true
|
ephemeral: !!isPrivate,
|
||||||
}).catch(() => {});
|
|
||||||
|
|
||||||
} else {
|
|
||||||
await interaction.deferReply({
|
|
||||||
ephemeral: false
|
|
||||||
}).catch(() => {});
|
}).catch(() => {});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
const cmd = client.slashCommands.get(interaction.commandName);
|
const cmd = client.slashCommands.get(interaction.commandName);
|
||||||
if (!cmd)
|
if (!cmd) {
|
||||||
return interaction.followUp({
|
return interaction.followUp({ content: "An error has occurred" });
|
||||||
content: "An error has occurred "
|
}
|
||||||
});
|
|
||||||
|
|
||||||
const args = [];
|
const args = [];
|
||||||
|
|
||||||
for (let option of interaction.options.data) {
|
for (let option of interaction.options.data) {
|
||||||
if (option.type === "SUB_COMMAND") {
|
if (option.type === "SUB_COMMAND") {
|
||||||
if (option.name) args.push(option.name);
|
if (option.name) args.push(option.name);
|
||||||
@ -81,16 +69,19 @@ client.on("interactionCreate", async (interaction) => {
|
|||||||
});
|
});
|
||||||
} else if (option.value) args.push(option.value);
|
} else if (option.value) args.push(option.value);
|
||||||
}
|
}
|
||||||
interaction.member = interaction.guild.members.cache.get(interaction.user.id);
|
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run the command
|
||||||
cmd.run(client, interaction, args);
|
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);
|
const command = client.slashCommands.get(interaction.commandName);
|
||||||
if (command) command.run(client, interaction);
|
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
|
// Register for all the guilds the bot is in
|
||||||
await client.application.commands.set(arrayOfSlashCommands);
|
await rest.put(
|
||||||
|
Discord.Routes.applicationCommands(client.user.id),
|
||||||
|
{ 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