From 97d3068d2839a6bd8413261d73c9d031e0932360 Mon Sep 17 00:00:00 2001 From: Taken Date: Fri, 24 Nov 2023 23:51:40 +0100 Subject: [PATCH] Added timeout command --- package-lock.json | 3 +- package.json | 3 +- src/commands/check.js | 1 - src/commands/timeout.js | 139 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 src/commands/timeout.js diff --git a/package-lock.json b/package-lock.json index 6520265..857c839 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,8 @@ "discord.js": "^14.8.0", "dotenv": "^16.0.3", "log-beautify": "^1.2.0", - "mongoose": "^7.0.1" + "mongoose": "^7.0.1", + "ms": "^2.1.3" } }, "node_modules/@discordjs/builders": { diff --git a/package.json b/package.json index a33152f..0d9b4e4 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "discord.js": "^14.8.0", "dotenv": "^16.0.3", "log-beautify": "^1.2.0", - "mongoose": "^7.0.1" + "mongoose": "^7.0.1", + "ms": "^2.1.3" } } diff --git a/src/commands/check.js b/src/commands/check.js index cdcf266..25034b0 100644 --- a/src/commands/check.js +++ b/src/commands/check.js @@ -7,7 +7,6 @@ module.exports = { name: "check", description: "Check a player's stats.", type: "slash", - dev: true, data: new SlashCommandBuilder() .setName("check") diff --git a/src/commands/timeout.js b/src/commands/timeout.js new file mode 100644 index 0000000..03fb0e8 --- /dev/null +++ b/src/commands/timeout.js @@ -0,0 +1,139 @@ +const { SlashCommandBuilder, PermissionFlagsBits, userMention } = require("discord.js") +const { color } = require("../../config/options.json") +const ms = require("ms") + +module.exports = { + name: "timeout", + description: "Times out a memeber", + type: "slash", + dev: true, + + data: new SlashCommandBuilder() + .setName("timeout") + .setDescription("Times out a memeber") + .addUserOption(option => + option + .setName("user") + .setDescription("The user to timeout") + .setRequired(true)) + .addStringOption(option => + option + .setName("time") + .setDescription("The time to timeout the user for") + .setRequired(true)) + .addStringOption(option => + option + .setName("reason") + .setDescription("The reason for the timeout")) + .setDefaultMemberPermissions(PermissionFlagsBits.Administrator) + .setDMPermission(false), + + /** @param { import('discord.js').ChatInputCommandInteraction } interaction */ + + async execute(interaction) { + + await interaction.deferReply() + + const target1 = interaction.options.getUser("user") + const target = interaction.guild.members.cache.get(target1.id) + const timeString = interaction.options.getString("time") + const reason = interaction.options.getString("reason") || "No reason provided" + const time = ms(timeString) + const embedColor = Number(color.replace("#", "0x")) + + if (target.bot) { + interaction.deferReply({ + embeds: [{ + description: "You cannot timeout a bot.", + color: embedColor, + }] + }) + return + } + + if (target.id == interaction.guild.ownerId) { + await interaction.deferReply({ + embeds: [{ + description: "You cannot timeout the server owner.", + color: embedColor, + }] + }) + return + } + + if (interaction.guild.members.me.roles.highest.position <= target.roles.highest.position) { + interaction.deferReply({ + embeds: [{ + description: "I cannot timeout this user because their role is higher than mine.", + color: embedColor, + }] + }) + return + } + + if (interaction.member.roles.highest.position <= target.roles.highest.position) { + await interaction.deferReply({ + embeds: [{ + description: "You cannot timeout this user because their role is higher than yours.", + color: embedColor, + }] + }) + return + } + + if (target.id == interaction.user.id) { + interaction.deferReply({ + embeds: [{ + description: "You cannot timeout yourself.", + color: embedColor, + }] + }) + return + } + + if (target.isCommunicationDisabled()) { + if (time === 0) { + await target.timeout(null, reason) + await interaction.editReply({ + embeds: [{ + description: "Removed timeout of " + userMention(target.id) + " for " + reason, + color: embedColor, + footer: { + text: "ID: " + target.id, + iconURL: target.avatarURL() + }, + timestamp: new Date() + }] + }) + return + } + + await target.timeout(time, reason) + await interaction.editReply({ + embeds: [{ + description: "Updated timeout of " + userMention(target.id) + " to " + timeString + " for " + reason, + color: embedColor, + footer: { + text: "ID: " + target.id, + iconURL: target.avatarURL() + }, + timestamp: new Date() + }] + }) + return + } + + await target.timeout(time, reason) + await interaction.editReply({ + embeds: [{ + description: "Timed out " + userMention(target.id) + " for " + timeString + " for " + reason, + color: embedColor, + footer: { + text: "ID: " + target.id, + iconURL: target.avatarURL() + }, + timestamp: new Date() + }] + }) + } +}