|
@@ -0,0 +1,90 @@
|
|
|
+import { Plugin, ICommandData, Command } from "src/model/plugin";
|
|
|
+import { parseArgs, tryDo, parseDuration, UNIT_MEASURES } from "src/util";
|
|
|
+import { GuildMember, Guild, MessageEmbed } from "discord.js";
|
|
|
+import { logger } from "src/logging";
|
|
|
+import { client } from "src/client";
|
|
|
+import humanizeDuration from "humanize-duration";
|
|
|
+
|
|
|
+const MENTION_PATTERN = /<@!?(\d+)>/;
|
|
|
+
|
|
|
+@Plugin
|
|
|
+export class MutePlugin {
|
|
|
+ @Command({
|
|
|
+ type: "prefix",
|
|
|
+ pattern: "mute",
|
|
|
+ auth: true
|
|
|
+ })
|
|
|
+ async muteUser({ message }: ICommandData): Promise<void> {
|
|
|
+ if (!message.guild) {
|
|
|
+ await message.reply("cannot do in DMs!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const [, userId, duration, ...rest] = parseArgs(message.content);
|
|
|
+
|
|
|
+ if (!userId) {
|
|
|
+ await message.reply("no user specified!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const user = await this.resolveUser(message.guild, userId);
|
|
|
+
|
|
|
+ if (!user) {
|
|
|
+ await message.reply("couldn't find the given user!");
|
|
|
+ logger.error("Tried to mute user %s but couldn't find them by id!", userId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ let durationMs = parseDuration(duration);
|
|
|
+ let reasonArray = rest;
|
|
|
+
|
|
|
+ if (!durationMs) {
|
|
|
+ durationMs = UNIT_MEASURES.d;
|
|
|
+ reasonArray = [duration, ...reasonArray];
|
|
|
+ }
|
|
|
+
|
|
|
+ let reason = reasonArray.join(" ");
|
|
|
+
|
|
|
+ if (!reason)
|
|
|
+ reason = "None given";
|
|
|
+
|
|
|
+ message.channel.send(new MessageEmbed({
|
|
|
+ title: "User has been muted for server violation",
|
|
|
+ color: 4944347,
|
|
|
+ timestamp: new Date(),
|
|
|
+ footer: {
|
|
|
+ text: client.botUser.username
|
|
|
+ },
|
|
|
+ author: {
|
|
|
+ name: client.botUser.username,
|
|
|
+ iconURL: client.botUser.avatarURL() ?? undefined
|
|
|
+ },
|
|
|
+ fields: [
|
|
|
+ {
|
|
|
+ name: "Username",
|
|
|
+ value: user.toString()
|
|
|
+ },
|
|
|
+ {
|
|
|
+ name: "Duration",
|
|
|
+ value: humanizeDuration(durationMs, { unitMeasures: UNIT_MEASURES })
|
|
|
+ },
|
|
|
+ {
|
|
|
+ name: "Reason",
|
|
|
+ value: reason
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+ private async resolveUser(guild: Guild, id: string): Promise<GuildMember | undefined> {
|
|
|
+ const result = MENTION_PATTERN.exec(id);
|
|
|
+ if (result) {
|
|
|
+ const userId = result[1];
|
|
|
+ const fetchResult = await tryDo(guild.members.fetch(userId));
|
|
|
+ if (fetchResult.ok)
|
|
|
+ return fetchResult.result;
|
|
|
+ }
|
|
|
+
|
|
|
+ const fetchResult = await tryDo(guild.members.fetch(id));
|
|
|
+ return fetchResult.result;
|
|
|
+ }
|
|
|
+}
|