From 51291998b3a1b865caecce2b2c4832dba61af00c Mon Sep 17 00:00:00 2001 From: janderedev Date: Mon, 3 Jan 2022 15:43:59 +0100 Subject: [PATCH] Allow warning unknown users --- src/bot/commands/warn.ts | 12 ++++++------ src/bot/commands/warns.ts | 15 ++++++--------- src/bot/modules/mod_logs.ts | 5 ++--- src/bot/util.ts | 12 ++++++++++++ 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/bot/commands/warn.ts b/src/bot/commands/warn.ts index c5a2a15..b45fa9c 100644 --- a/src/bot/commands/warn.ts +++ b/src/bot/commands/warn.ts @@ -1,9 +1,9 @@ import Command from "../../struct/Command"; -import { isModerator, NO_MANAGER_MSG, parseUser, storeInfraction } from "../util"; +import { isModerator, NO_MANAGER_MSG, parseUserOrId, storeInfraction } from "../util"; import Infraction from "../../struct/antispam/Infraction"; import { ulid } from "ulid"; import InfractionType from "../../struct/antispam/InfractionType"; -import { logModAction } from "../modules/mod_logs"; +import { fetchUsername, logModAction } from "../modules/mod_logs"; import MessageCommandContext from "../../struct/MessageCommandContext"; export default { @@ -13,9 +13,9 @@ export default { description: 'add an infraction to an user\'s record', run: async (message: MessageCommandContext, args: string[]) => { if (!await isModerator(message.member!, message.serverContext)) return message.reply(NO_MANAGER_MSG); - let user = await parseUser(args.shift() ?? ''); + let user = await parseUserOrId(args.shift() ?? ''); if (!user) return message.reply('I can\'t find that user.'); - if (user.bot != null) return message.reply('You cannot warn bots.'); + if ((user as any)?.bot != null) return message.reply('You cannot warn bots.'); let reason: string = args.join(' ') ?.replace(new RegExp('`', 'g'), '\'') @@ -39,10 +39,10 @@ export default { message.reply(`### User warned` + `${message.serverContext._id != message.channel?.server_id ? ` in **${message.serverContext.name}**` : ''}.\n` + `This is ${userWarnCount == 1 ? '**the first warn**' : `warn number **${userWarnCount}**`}` - + ` for ${user.username ?? 'this user'}.\n` + + ` for ${await fetchUsername(user._id)}.\n` + `**Infraction ID:** \`${infraction._id}\`\n` + `**Reason:** \`${infraction.reason}\``), - logModAction('warn', message.serverContext, message.member!, user, reason, `This is warn number **${userWarnCount}** for this user.`), + logModAction('warn', message.serverContext, message.member!, user._id, reason, `This is warn number **${userWarnCount}** for this user.`), ]); } } as Command; diff --git a/src/bot/commands/warns.ts b/src/bot/commands/warns.ts index 5cc8e49..c9e5e6f 100644 --- a/src/bot/commands/warns.ts +++ b/src/bot/commands/warns.ts @@ -1,14 +1,11 @@ import Command from "../../struct/Command"; -import { Message } from "@janderedev/revolt.js/dist/maps/Messages"; import { client } from "../.."; import Infraction from "../../struct/antispam/Infraction"; import InfractionType from "../../struct/antispam/InfractionType"; -import { isModerator, NO_MANAGER_MSG, parseUser, uploadFile } from "../util"; +import { isModerator, NO_MANAGER_MSG, parseUserOrId, uploadFile } from "../util"; import Day from 'dayjs'; import RelativeTime from 'dayjs/plugin/relativeTime'; import Xlsx from 'xlsx'; -import FormData from 'form-data'; -import axios from "axios"; import { fetchUsername } from "../modules/mod_logs"; import MessageCommandContext from "../../struct/MessageCommandContext"; @@ -65,13 +62,13 @@ export default { + `Created ${Day(inf.date).fromNow()}`); break; default: - let user = await parseUser(args[0]); - if (!user) return message.reply('Unknown user'); + let user = await parseUserOrId(args[0]); + if (!user?._id) return message.reply('I can\'t find this user.'); let infs = userInfractions.get(user._id); - if (!infs) return message.reply(`There are no infractions stored for \`@${user.username}\`.`); + if (!infs) return message.reply(`There are no infractions stored for \`${await fetchUsername(user._id)}\`.`); else { - let msg = `## ${infs.length} infractions stored for @${user.username}\n\u200b\n`; + let msg = `## ${infs.length} infractions stored for ${await fetchUsername(user._id)}\n\u200b\n`; let attachSpreadsheet = false; for (const i in infs) { let inf = infs[i]; @@ -94,7 +91,7 @@ export default { if (attachSpreadsheet) { try { let csv_data = [ - [`Warns for @${user.username} (${user._id}) - ${Day().toString()}`], + [`Warns for ${await fetchUsername(user._id)} (${user._id}) - ${Day().toString()}`], [], ['Date', 'Reason', 'Created By', 'Type', 'Action Type', 'ID'], ]; diff --git a/src/bot/modules/mod_logs.ts b/src/bot/modules/mod_logs.ts index a31163c..12bf9c7 100644 --- a/src/bot/modules/mod_logs.ts +++ b/src/bot/modules/mod_logs.ts @@ -1,6 +1,5 @@ import { Member } from "@janderedev/revolt.js/dist/maps/Members"; import { Server } from "@janderedev/revolt.js/dist/maps/Servers"; -import { User } from "@janderedev/revolt.js/dist/maps/Users"; import { client } from "../.."; import ServerConfig from "../../struct/ServerConfig"; import logger from "../logger"; @@ -115,7 +114,7 @@ client.on('packet', async (packet) => { } }); -async function logModAction(type: 'warn'|'kick'|'ban', server: Server, mod: Member, target: User, reason: string|null, extraText?: string|null): Promise { +async function logModAction(type: 'warn'|'kick'|'ban', server: Server, mod: Member, target: string, reason: string|null, extraText?: string|null): Promise { try { let config: ServerConfig = await client.db.get('servers').findOne({ id: server._id }) ?? {}; let logChannelID = config.logs?.modAction; @@ -125,7 +124,7 @@ async function logModAction(type: 'warn'|'kick'|'ban', server: Server, mod: Memb let aType = type == 'ban' ? 'banned' : type + 'ed'; let msg = `User ${aType}\n` + `\`@${mod.user?.username}\` **${aType}** \`@` - + `${target.username}\`${type == 'warn' ? '.' : ` from ${server.name}.`}\n` + + `${await fetchUsername(target)}\`${type == 'warn' ? '.' : ` from ${server.name}.`}\n` + `**Reason**: \`${reason ? reason : 'No reason provided.'}\`\n` + (extraText ?? ''); diff --git a/src/bot/util.ts b/src/bot/util.ts index f54863a..247b536 100644 --- a/src/bot/util.ts +++ b/src/bot/util.ts @@ -67,6 +67,17 @@ async function parseUser(text: string): Promise { } catch(e) { return null; } } +/** + * Does the exact same as `parseUser`, but returns only `_id` instead + * of null if the user was not found and the input is also an ID + */ +async function parseUserOrId(text: string): Promise { + let parsed = await parseUser(text); + if (parsed) return parsed; + if (ULID_REGEX.test(text)) return { _id: text.toUpperCase() }; + return null; +} + async function isModerator(member: Member, server: Server) { return hasPerm(member, 'KickMembers') || await isBotManager(member, server) @@ -160,6 +171,7 @@ export { isModerator, isBotManager, parseUser, + parseUserOrId, storeInfraction, uploadFile, sanitizeMessageContent,