Files
BluBot/commands/misc/pinguser.js
2025-06-21 15:13:58 -05:00

93 lines
3.0 KiB
JavaScript

const { Client, Interaction, SlashCommandBuilder } = require("discord.js");
const cooldowns = new Map();
var isDisabled = false;
/**
* Handles the ping user command.
* @param {Interaction} interaction - The interaction object.
*/
async function handlePingCommand(interaction) {
const user = interaction.options.getUser("user"); // Get the user from the interaction
const times = interaction.options.getInteger("times") || 1; // Default to 1 if not provided
try {
// Ensure the user exists in the guild
const userPing = await interaction.guild.members.fetch(user.id);
if (!userPing) {
await interaction.reply("That user doesn't exist in this server.");
return;
}
// Prevent the bot from pinging itself
if (userPing.id === interaction.guild.members.me.id) {
await interaction.reply("I can't ping myself.");
return;
}
// Cooldown logic
const coolDown = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
const now = Date.now();
const lastExec = cooldowns.get(interaction.user.id);
if (lastExec && now - lastExec < coolDown) {
const timeLeft = coolDown - (now - lastExec);
const days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
const hours = Math.floor((timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((timeLeft % (1000 * 60)) / 1000);
await interaction.reply(
`You need to wait ${days} days, ${hours} hours, ${minutes} minutes, and ${seconds} seconds before using this command again.`,
);
return;
}
// Send the pings
await interaction.reply({ content: "Ok, sending pings.", ephemeral: false });
for (let i = 0; i < times; i++) {
await interaction.channel.send(`Pinging ${userPing}`);
await new Promise((resolve) => setTimeout(resolve, 300)); // Wait 300ms between pings
}
// Update the cooldown
cooldowns.set(interaction.user.id, now);
} catch (error) {
console.error("There was an error in pinguser: ", error);
await interaction.reply({
content: "An error occurred while executing the command.",
ephemeral: true,
});
}
}
module.exports = {
data: new SlashCommandBuilder()
.setName("pinguser")
.setDescription("Ping the mentioned user")
.addUserOption((option) =>
option.setName("user").setDescription("The user to ping").setRequired(true),
)
.addIntegerOption((option) =>
option
.setName("times")
.setDescription("Number of times to ping the user")
.setRequired(false),
),
async execute(interaction) {
try {
if (isDisabled) {
return interaction.reply({
content: "The command is disabled... Try again later.",
ephemeral: true,
});
}
await handlePingCommand(interaction); // Ensure we await this function
} catch (error) {
console.error("There was an error executing the command: ", error);
}
},
};