Skip to content

Instantly share code, notes, and snippets.

@Kaisarion
Created March 18, 2022 15:34
Show Gist options
  • Save Kaisarion/4309bef3452480edba36cab4e1ce341b to your computer and use it in GitHub Desktop.
Save Kaisarion/4309bef3452480edba36cab4e1ce341b to your computer and use it in GitHub Desktop.
THIS IS BEA BEFORE SHE DISCOVERED DISCORD.JS CHOICES IN OPTIONS
const { SlashCommandBuilder } = require("@discordjs/builders");
const { MessageEmbed, Permissions } = require("discord.js");
const DatabaseManager = require("../../handlers/DatabaseManager.js");
module.exports = {
data: new SlashCommandBuilder()
.setName("timeout")
.setDescription("Timeout a mentioned user in the server.")
.addUserOption((option) =>
option
.setName("timeoutuser")
.setDescription("The user to kick from the server.")
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("time")
.setDescription(
"The time the user should be timed out for. e.g 50m, 1d, 2h, 30s, off"
)
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("timeoutreason")
.setDescription("The reason for timing out the mentioned user.")
),
async execute(interaction) {
const embedcolor = await DatabaseManager.getHexColor(interaction.guild.id);
const usr = interaction.options.getUser("timeoutuser");
const mem = await interaction.guild.members.fetch(usr);
let rs = interaction.options.getString("timeoutreason");
if (!rs) rs = "Not specified.";
// modlogs
const logChannel = await DatabaseManager.getGuildModLogsID(
interaction.guild.id
);
const ModLogsCh = await interaction.guild.channels.cache.get(logChannel);
if (!interaction.member.permissions.has(Permissions.FLAGS.KICK_MEMBERS))
return interaction.reply(
"❌ **ERROR !** \n" +
"you do not have permissions to use this command. if you believe this is in error, contact the server staff team."
);
if (!interaction.guild.me.permissions.has(Permissions.FLAGS.KICK_MEMBERS))
return interaction.reply(
"❌ **ERROR !** \n" +
"I don't have the right permissions! if you believe this is in error, please check my role permissions."
);
// Time statement
let time = interaction.options.getString("time");
let op;
// Set up a statement to convert time.
switch (true) {
case time.includes("s"):
time.replace("s", "");
op = "seconds";
break;
case time.includes("m"):
time.replace("m", "");
op = "minutes";
break;
case time.includes("h"):
time.replace("h", "");
op = "hours";
break;
case time.includes("d"):
time.replace("d", "");
op = "days";
break;
case time.includes("off"):
time.replace("off", null);
op = null;
break;
default:
time = null;
op = null;
}
// Test the time regex string and return an error, if any
let timeregex = /^\b(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\b/i;
const eregex = new MessageEmbed()
.setTitle("Error!")
.setDescription(
`Your input for time, ${time}, is not a valid input.\nValid inputs include:\n5m for 5 minutes\n20s for 20 seconds\n50h for 50 hours\n2d for 2 days\noff to remove the user's timeout.`
)
.setColor(embedcolor)
.setFooter({
text: `If this error persists, use /amourhelp to contact the bot support team.`,
});
if (!timeregex.test(time) && time !== null) {
return interaction.reply({ embeds: [eregex], ephemeral: true });
}
// Handle the time conversion for .timeout
switch (true) {
case op === "seconds":
time = time * 1000;
break;
case op === "minutes":
time = time * 60 * 1000;
break;
case op === "hours":
time = time * 3600000;
break;
case op === "days":
time = time * 86400000;
break;
case op === null:
time = null;
break;
}
const timeoutembed = new MessageEmbed()
.setColor(embedcolor)
.setAuthor({
name: interaction.user.username,
iconURL: interaction.user.displayAvatarURL({ dynamic: true }),
})
.setTitle("🔇 You have been timed out in " + interaction.guild.name)
.setDescription(`**Reason:** ${rs}`);
const owner = await interaction.guild.fetchOwner();
if (owner.id !== interaction.user.id) {
if (
member.roles.highest.rawPosition >=
interaction.member.roles.highest.rawPosition
) {
return message.channel.send(
"This user can't be timed out because they have the same or higher role to you."
);
}
}
const done = new MessageEmbed()
.setTitle("Member Timed Out")
.setColor(embedcolor)
.setThumbnail(usr.displayAvatarURL())
.addField("User Timed Out", `${usr} (${usr.tag})`)
.addField("User ID", usr.id)
.addField("Timed out by", `${interaction.user} (${interaction.user.id})`)
.setDescription(`Timed out for ${rs}`)
.setTimestamp();
mem
.timeout(time, `${interaction.user.tag} ;; ${rs} `)
.then(async () => {
await usr
.send({ embeds: [timeoutembed] })
.catch((err) =>
interaction.channel.send(`I could not DM the user! Reason: ${err}`)
);
interaction.reply({ embeds: [done] });
if (logChannel === "" || !logChannel) {
return interaction.reply({ embeds: [done] });
} else {
if (ModLogsCh)
ModLogsCh.send({ embeds: [done] }).catch((err) =>
message.channel.send(err)
);
return;
}
})
.catch((err) => {
if (err)
return interaction.reply(
"Something went wrong. If this issue persists, please use /amourhelp"
);
});
},
};
@Kaisarion
Copy link
Author

I AM SO FUCKING DUMB

@Kaisarion
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment