vylbot-app/src/commands/mute.ts
Vylpes cd666d24fd
Feature/vba 77 (#178)
* Add say command (#174)

Co-authored-by: Ethan Lane <ethan@vylpes.com>
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/vylbot-app/pulls/174

* Add repo and funding link to about message (#176)

Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/vylbot-app/pulls/176

* Add other subreddits to bunny command (#177)

Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/vylbot-app/pulls/177

* Add database table

* Save moderation actions to database

* Add audit command to see a user's audits

* Add audit view subcommand

* Add audit clear subcommand

* Create add audit subcommand

* Fix bot crashing when viewing an audit with no reason

* Fix changes requested
2022-09-05 18:10:04 +01:00

104 lines
3.3 KiB
TypeScript

import { AuditType } from "../constants/AuditType";
import ErrorMessages from "../constants/ErrorMessages";
import { ICommandContext } from "../contracts/ICommandContext";
import ICommandReturnContext from "../contracts/ICommandReturnContext";
import Audit from "../entity/Audit";
import ErrorEmbed from "../helpers/embeds/ErrorEmbed";
import LogEmbed from "../helpers/embeds/LogEmbed";
import PublicEmbed from "../helpers/embeds/PublicEmbed";
import { Command } from "../type/command";
export default class Mute extends Command {
constructor() {
super();
super.Category = "Moderation";
super.Roles = [
"moderator"
];
}
public override async execute(context: ICommandContext): Promise<ICommandReturnContext> {
const targetUser = context.message.mentions.users.first();
if (!targetUser) {
const embed = new ErrorEmbed(context, "User does not exist");
await embed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [embed]
};
}
const targetMember = context.message.guild?.members.cache.find(x => x.user.id == targetUser.id);
if (!targetMember) {
const embed = new ErrorEmbed(context, "User is not in this server");
await embed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [embed]
};
}
const reasonArgs = context.args;
reasonArgs.splice(0, 1);
const reason = reasonArgs.join(" ");
if (!context.message.guild?.available) {
return {
commandContext: context,
embeds: []
};
}
if (!targetMember.manageable) {
const embed = new ErrorEmbed(context, ErrorMessages.InsufficientBotPermissions);
await embed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [embed]
};
}
const logEmbed = new LogEmbed(context, "Member Muted");
logEmbed.AddUser("User", targetUser, true)
logEmbed.AddUser("Moderator", context.message.author);
logEmbed.AddReason(reason);
const publicEmbed = new PublicEmbed(context, "", `${targetUser} has been muted`);
publicEmbed.AddReason(reason);
const mutedRole = context.message.guild.roles.cache.find(role => role.name == process.env.ROLES_MUTED);
if (!mutedRole) {
const embed = new ErrorEmbed(context, ErrorMessages.RoleNotFound);
await embed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [embed]
};
}
await targetMember.roles.add(mutedRole, `Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}`);
await logEmbed.SendToModLogsChannel();
await publicEmbed.SendToCurrentChannel();
if (context.message.guild) {
const audit = new Audit(targetUser.id, AuditType.Mute, reason, context.message.author.id, context.message.guild.id);
await audit.Save(Audit, audit);
}
return {
commandContext: context,
embeds: [logEmbed, publicEmbed]
};
}
}