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) => {
|
||||
|
||||
// Declare a random var for the main modal - Each Session
|
||||
let rand = Math.floor(Math.random() * 99999999999999).toString();
|
||||
// Declare a random ID for the modal to ensure unique interactions
|
||||
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;
|
||||
|
||||
// await interaction.deferReply();
|
||||
// Create the modal
|
||||
const modal = new ModalBuilder()
|
||||
.setCustomId(rand)
|
||||
.setCustomId(modalId)
|
||||
.setTitle('This is an example modal');
|
||||
|
||||
// TODO: Add components to modal...
|
||||
// Create a text input component for the modal
|
||||
const modalInputData = new TextInputBuilder()
|
||||
.setCustomId('modalInput')
|
||||
// The label is the prompt the user sees for this input
|
||||
.setLabel("What text do you want to send?")
|
||||
// Short means only a single line of text
|
||||
.setStyle(TextInputStyle.Paragraph);
|
||||
.setStyle(TextInputStyle.Paragraph); // Allows for multi-line text
|
||||
|
||||
// An action row only holds one text input,
|
||||
// so you need one action row per text input.
|
||||
const modalInputRow = new ActionRowBuilder().addComponents([modalInputData]);
|
||||
// Wrap the text input in an action row
|
||||
const modalInputRow = new ActionRowBuilder().addComponents(modalInputData);
|
||||
|
||||
// Add inputs to the modal
|
||||
modal.addComponents([modalInputRow]);
|
||||
// Add the input row to the modal
|
||||
modal.addComponents(modalInputRow);
|
||||
|
||||
// Show the modal to the user
|
||||
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
|
||||
if (interaction.type == "modal") return
|
||||
// Interaction type is 5 == Modal
|
||||
if (interaction.type === 5) {
|
||||
// Ensure we are handling the correct modal submission
|
||||
if (!modalInteraction.isModalSubmit() || modalInteraction.customId !== modalId) return;
|
||||
|
||||
// Make sure we are working with our users modal only
|
||||
if (interaction.customId === rand) {
|
||||
|
||||
// Get the data entered by the user
|
||||
let modalInputDataString = interaction.fields.getTextInputValue('modalInput');
|
||||
|
||||
console.log(modalInputDataString)
|
||||
// Retrieve the data entered by the user
|
||||
const modalInputDataString = modalInteraction.fields.getTextInputValue('modalInput');
|
||||
|
||||
// Create an embed to display the user input
|
||||
const embed = new EmbedBuilder()
|
||||
// Set color to blue
|
||||
.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] });
|
||||
}
|
||||
}
|
||||
})
|
||||
.setFooter({ text: `Requested by ${modalInteraction.user.tag}`, iconURL: modalInteraction.user.displayAvatarURL() });
|
||||
|
||||
// Reply to the modal submission with the embed
|
||||
await modalInteraction.reply({ embeds: [embed] });
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -2,14 +2,14 @@ const { EmbedBuilder } = require('discord.js');
|
||||
|
||||
module.exports = {
|
||||
name: "ping",
|
||||
private: true,
|
||||
private: true, // Mark this command as private for ephemeral replies
|
||||
description: "Returns websocket latency",
|
||||
|
||||
run: async (client, interaction) => {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor("#FF0000")
|
||||
.setTitle("🏓 Pong!")
|
||||
.setDescription(`Latency : ${client.ws.ping}ms`)
|
||||
.setDescription(`Latency: ${client.ws.ping}ms`)
|
||||
.setTimestamp()
|
||||
.setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` });
|
||||
interaction.followUp({ embeds: [embed] });
|
||||
|
@ -5,7 +5,6 @@ const { promisify } = require("util");
|
||||
const globPromise = promisify(glob);
|
||||
|
||||
client.on("interactionCreate", async (interaction) => {
|
||||
|
||||
// Slash Commands
|
||||
const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`);
|
||||
const arrayOfSlashCommands = [];
|
||||
@ -18,61 +17,50 @@ client.on("interactionCreate", async (interaction) => {
|
||||
|
||||
if (!file?.name) return;
|
||||
|
||||
const properties = {
|
||||
directory,
|
||||
...file
|
||||
};
|
||||
const properties = { directory, ...file };
|
||||
client.slashCommands.set(file.name, properties);
|
||||
|
||||
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
|
||||
|
||||
// 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
|
||||
if (interaction.isChatInputCommand()) {
|
||||
let commandData = [];
|
||||
|
||||
// Grabbing Command Data for this interaction
|
||||
let commandData = []
|
||||
|
||||
// We use ForEach here to filter our array into the single commands info.
|
||||
// Filter to find the command for this interaction
|
||||
await arrayOfSlashCommands.forEach(command => {
|
||||
if (command.name == interaction.commandName) {
|
||||
commandData.push(command)
|
||||
if (command.name === interaction.commandName) {
|
||||
commandData.push(command);
|
||||
}
|
||||
});
|
||||
|
||||
// Process and Parse Data
|
||||
let dataToProcess = JSON.stringify(commandData[0])
|
||||
let parsedData = JSON.parse(dataToProcess)
|
||||
// Process and parse the command data
|
||||
const parsedData = commandData[0];
|
||||
|
||||
|
||||
if (interaction.commandName == "modal-example"){
|
||||
console.log("Modal - Skipping defer")
|
||||
// Defer reply based on privacy settings
|
||||
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) {
|
||||
const isPrivate = parsedData?.private;
|
||||
await interaction.deferReply({
|
||||
ephemeral: true
|
||||
}).catch(() => {});
|
||||
|
||||
} else {
|
||||
await interaction.deferReply({
|
||||
ephemeral: false
|
||||
ephemeral: !!isPrivate,
|
||||
}).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
const cmd = client.slashCommands.get(interaction.commandName);
|
||||
if (!cmd)
|
||||
return interaction.followUp({
|
||||
content: "An error has occurred "
|
||||
});
|
||||
if (!cmd) {
|
||||
return interaction.followUp({ content: "An error has occurred" });
|
||||
}
|
||||
|
||||
const args = [];
|
||||
|
||||
for (let option of interaction.options.data) {
|
||||
if (option.type === "SUB_COMMAND") {
|
||||
if (option.name) args.push(option.name);
|
||||
@ -81,16 +69,19 @@ client.on("interactionCreate", async (interaction) => {
|
||||
});
|
||||
} 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);
|
||||
}
|
||||
|
||||
// Context Menu Handling
|
||||
if (interaction.isContextMenuCommand()) {
|
||||
await interaction.deferReply({
|
||||
ephemeral: false
|
||||
});
|
||||
await interaction.deferReply({ ephemeral: false });
|
||||
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 { promisify } = require("util");
|
||||
const globPromise = promisify(glob);
|
||||
const { REST } = require('@discordjs/rest');
|
||||
const Discord = require('discord.js');
|
||||
|
||||
module.exports = async (client) => {
|
||||
const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);
|
||||
|
||||
// Slash Commands
|
||||
const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`);
|
||||
const arrayOfSlashCommands = [];
|
||||
|
||||
slashCommands.map((value) => {
|
||||
const file = require(value);
|
||||
const splitted = value.split("/");
|
||||
@ -18,7 +23,15 @@ module.exports = async (client) => {
|
||||
client.slashCommands.set(file.name, properties);
|
||||
|
||||
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
|
||||
@ -27,11 +40,15 @@ module.exports = async (client) => {
|
||||
|
||||
// Slash Commands Register
|
||||
client.on("ready", async () => {
|
||||
// // Register for a single guild
|
||||
// await client.guilds.cache.get("GUIDIDHERE").commands.set(arrayOfSlashCommands);
|
||||
|
||||
try {
|
||||
// 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