feat(bots/discord/commands): add mute and unmute commands

This commit is contained in:
PalmDevs
2024-06-24 01:15:12 +07:00
parent 467acff57a
commit c0fa2fe1c3
4 changed files with 144 additions and 1 deletions

View File

@@ -0,0 +1,65 @@
import { SlashCommandBuilder } from 'discord.js'
import CommandError, { CommandErrorType } from '$/classes/CommandError'
import { applyRolePreset } from '$/utils/discord/rolePresets'
import type { Command } from '..'
import { applyReferenceToModerationActionEmbed, createModerationActionEmbed } from '$/utils/discord/embeds'
import { parse } from 'simple-duration'
export default {
data: new SlashCommandBuilder()
.setName('mute')
.setDescription('Mute a member')
.addUserOption(option => option.setName('member').setRequired(true).setDescription('The member to mute'))
.addStringOption(option => option.setName('reason').setDescription('The reason for muting the member'))
.addStringOption(option => option.setName('duration').setDescription('The duration of the mute'))
.toJSON(),
memberRequirements: {
permissions: 8n,
},
global: false,
async execute({ config, logger }, interaction) {
const user = interaction.options.getUser('member', true)
const reason = interaction.options.getString('reason')
const duration = interaction.options.getString('duration')
const durationMs = duration ? parse(duration) : null
if (Number.isInteger(durationMs) && durationMs! < 1)
throw new CommandError(
CommandErrorType.InvalidDuration,
'The duration must be at least 1 millisecond long.',
)
const member = await interaction.guild!.members.fetch(user.id)
if (!member)
throw new CommandError(
CommandErrorType.InvalidUser,
'The provided member is not in the server or does not exist.',
)
await applyRolePreset(member, 'mute', durationMs ? Date.now() + durationMs : null)
const embed = createModerationActionEmbed(
'Muted',
user,
interaction.user,
reason ?? 'No reason provided',
durationMs,
)
const reply = await interaction.reply({ embeds: [embed] }).then(it => it.fetch())
const logConfig = config.moderation?.log
if (logConfig) {
const channel = await interaction.guild!.channels.fetch(logConfig.thread ?? logConfig.channel)
if (!channel || !channel.isTextBased())
return void logger.warn('The moderation log channel does not exist, skipping logging')
await channel.send({ embeds: [applyReferenceToModerationActionEmbed(embed, reply.url)] })
}
},
} satisfies Command

View File

@@ -0,0 +1,44 @@
import { SlashCommandBuilder } from 'discord.js'
import CommandError, { CommandErrorType } from '$/classes/CommandError'
import { applyReferenceToModerationActionEmbed, createModerationActionEmbed } from '$/utils/discord/embeds'
import { removeRolePreset } from '$/utils/discord/rolePresets'
import type { Command } from '..'
export default {
data: new SlashCommandBuilder()
.setName('unmute')
.setDescription('Unmute a member')
.addUserOption(option => option.setName('member').setRequired(true).setDescription('The member to mute'))
.toJSON(),
memberRequirements: {
permissions: 8n,
},
global: false,
async execute({ config, logger }, interaction) {
const user = interaction.options.getUser('member', true)
const member = await interaction.guild!.members.fetch(user.id)
if (!member)
throw new CommandError(
CommandErrorType.InvalidUser,
'The provided member is not in the server or does not exist.',
)
await removeRolePreset(member, 'mute')
const embed = createModerationActionEmbed('Muted', user, interaction.user)
const reply = await interaction.reply({ embeds: [embed] }).then(it => it.fetch())
const logConfig = config.moderation?.log
if (logConfig) {
const channel = await interaction.guild!.channels.fetch(logConfig.thread ?? logConfig.channel)
if (!channel || !channel.isTextBased())
return void logger.warn('The moderation log channel does not exist, skipping logging')
await channel.send({ embeds: [applyReferenceToModerationActionEmbed(embed, reply.url)] })
}
},
} satisfies Command