From 2931468ceed74552e1c8effaa6625d8981066b9b Mon Sep 17 00:00:00 2001 From: JandereDev Date: Mon, 6 Dec 2021 22:59:44 +0100 Subject: [PATCH] fixed issue with kicks/bans being logged twice --- src/bot/commands/ban.ts | 18 +++++++-------- src/bot/commands/kick.ts | 12 +++++----- src/bot/modules/event_handler.ts | 38 +++++++++++++++++++------------- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/bot/commands/ban.ts b/src/bot/commands/ban.ts index c28a35b..1a1a3fc 100644 --- a/src/bot/commands/ban.ts +++ b/src/bot/commands/ban.ts @@ -60,11 +60,6 @@ export default { let reason = args.join(' ') || 'No reason provided'; if (banDuration == 0) { - message.serverContext.banUser(targetUser._id, { - reason: reason + ` (by ${await fetchUsername(message.author_id)} ${message.author_id})` - }) - .catch(e => message.reply(`Failed to ban user: \`${e}\``)); - let infId = ulid(); let { userWarnCount } = await storeInfraction({ _id: infId, @@ -77,14 +72,14 @@ export default { actionType: 'ban', } as Infraction); - message.reply(`### @${targetUser.username} has been banned.\n` - + `Infraction ID: \`${infId}\` (**#${userWarnCount}** for this user)`); - } else { message.serverContext.banUser(targetUser._id, { - reason: reason + ` (by ${await fetchUsername(message.author_id)} ${message.author_id}) (${durationStr})` + reason: reason + ` (by ${await fetchUsername(message.author_id)} ${message.author_id})` }) .catch(e => message.reply(`Failed to ban user: \`${e}\``)); + message.reply(`### @${targetUser.username} has been banned.\n` + + `Infraction ID: \`${infId}\` (**#${userWarnCount}** for this user)`); + } else { let banUntil = Date.now() + banDuration; let infId = ulid(); let { userWarnCount } = await storeInfraction({ @@ -98,6 +93,11 @@ export default { actionType: 'ban', } as Infraction); + message.serverContext.banUser(targetUser._id, { + reason: reason + ` (by ${await fetchUsername(message.author_id)} ${message.author_id}) (${durationStr})` + }) + .catch(e => message.reply(`Failed to ban user: \`${e}\``)); + await storeTempBan({ id: infId, bannedUser: targetUser._id, diff --git a/src/bot/commands/kick.ts b/src/bot/commands/kick.ts index 231f52d..93af771 100644 --- a/src/bot/commands/kick.ts +++ b/src/bot/commands/kick.ts @@ -40,12 +40,6 @@ export default { return message.reply(`Failed to fetch member: \`${e}\``); } - try { - await targetMember.kick(); - } catch(e) { - return message.reply(`Failed to kick user: \`${e}\``); - } - let infId = ulid(); let { userWarnCount } = await storeInfraction({ _id: infId, @@ -58,6 +52,12 @@ export default { actionType: 'kick', } as Infraction); + try { + await targetMember.kick(); + } catch(e) { + return message.reply(`Failed to kick user: \`${e}\``); + } + message.reply(`### @${targetUser.username} has been ${Math.random() > 0.8 ? 'ejected' : 'kicked'}.\n` + `Infraction ID: \`${infId}\` (**#${userWarnCount}** for this user)`); } diff --git a/src/bot/modules/event_handler.ts b/src/bot/modules/event_handler.ts index c113d01..7b9ad72 100644 --- a/src/bot/modules/event_handler.ts +++ b/src/bot/modules/event_handler.ts @@ -5,7 +5,7 @@ import InfractionType from "../../struct/antispam/InfractionType"; import { storeInfraction } from "../util"; // Listen to system messages -client.on('message', message => { +client.on('message', async message => { if (typeof message.content != 'object') return; let sysMsg = message.asSystemMessage; @@ -13,21 +13,29 @@ client.on('message', message => { switch(sysMsg.type) { case 'user_kicked': case 'user_banned': - if (message.channel && - sysMsg.user && - sysMsg.by && - sysMsg.by._id != client.user?._id) return; + try { + let recentEvents = await client.db.get('infractions').findOne({ + date: { $gt: Date.now() - 30000 }, + user: sysMsg.user?._id, + server: message.channel?.server_id, + actionType: sysMsg.type == 'user_kicked' ? 'kick' : 'ban', + }); - storeInfraction({ - _id: ulid(), - createdBy: sysMsg.by?._id, - reason: 'Unknown reason (caught system message)', - date: message.createdAt, - server: message.channel!.server_id, - type: InfractionType.Manual, - user: sysMsg.user!._id, - actionType: sysMsg.type == 'user_kicked' ? 'kick' : 'ban', - } as Infraction).catch(console.warn); + if (!message.channel || + !sysMsg.user || + recentEvents) return; + + storeInfraction({ + _id: ulid(), + createdBy: sysMsg.by?._id, + reason: 'Unknown reason (caught system message)', + date: message.createdAt, + server: message.channel!.server_id, + type: InfractionType.Manual, + user: sysMsg.user!._id, + actionType: sysMsg.type == 'user_kicked' ? 'kick' : 'ban', + } as Infraction).catch(console.warn); + } catch(e) { console.error(e) } break; case 'user_joined': break; case 'user_left' : break;