fixed issue with kicks/bans being logged twice

This commit is contained in:
JandereDev 2021-12-06 22:59:44 +01:00
parent 2a10d081a0
commit 2931468cee
Signed by: Lea
GPG key ID: 5D5E18ACB990F57A
3 changed files with 38 additions and 30 deletions

View file

@ -60,11 +60,6 @@ export default {
let reason = args.join(' ') || 'No reason provided'; let reason = args.join(' ') || 'No reason provided';
if (banDuration == 0) { 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 infId = ulid();
let { userWarnCount } = await storeInfraction({ let { userWarnCount } = await storeInfraction({
_id: infId, _id: infId,
@ -77,14 +72,14 @@ export default {
actionType: 'ban', actionType: 'ban',
} as Infraction); } as Infraction);
message.reply(`### @${targetUser.username} has been banned.\n`
+ `Infraction ID: \`${infId}\` (**#${userWarnCount}** for this user)`);
} else {
message.serverContext.banUser(targetUser._id, { 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}\``)); .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 banUntil = Date.now() + banDuration;
let infId = ulid(); let infId = ulid();
let { userWarnCount } = await storeInfraction({ let { userWarnCount } = await storeInfraction({
@ -98,6 +93,11 @@ export default {
actionType: 'ban', actionType: 'ban',
} as Infraction); } 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({ await storeTempBan({
id: infId, id: infId,
bannedUser: targetUser._id, bannedUser: targetUser._id,

View file

@ -40,12 +40,6 @@ export default {
return message.reply(`Failed to fetch member: \`${e}\``); 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 infId = ulid();
let { userWarnCount } = await storeInfraction({ let { userWarnCount } = await storeInfraction({
_id: infId, _id: infId,
@ -58,6 +52,12 @@ export default {
actionType: 'kick', actionType: 'kick',
} as Infraction); } 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` message.reply(`### @${targetUser.username} has been ${Math.random() > 0.8 ? 'ejected' : 'kicked'}.\n`
+ `Infraction ID: \`${infId}\` (**#${userWarnCount}** for this user)`); + `Infraction ID: \`${infId}\` (**#${userWarnCount}** for this user)`);
} }

View file

@ -5,7 +5,7 @@ import InfractionType from "../../struct/antispam/InfractionType";
import { storeInfraction } from "../util"; import { storeInfraction } from "../util";
// Listen to system messages // Listen to system messages
client.on('message', message => { client.on('message', async message => {
if (typeof message.content != 'object') return; if (typeof message.content != 'object') return;
let sysMsg = message.asSystemMessage; let sysMsg = message.asSystemMessage;
@ -13,21 +13,29 @@ client.on('message', message => {
switch(sysMsg.type) { switch(sysMsg.type) {
case 'user_kicked': case 'user_kicked':
case 'user_banned': case 'user_banned':
if (message.channel && try {
sysMsg.user && let recentEvents = await client.db.get('infractions').findOne({
sysMsg.by && date: { $gt: Date.now() - 30000 },
sysMsg.by._id != client.user?._id) return; user: sysMsg.user?._id,
server: message.channel?.server_id,
actionType: sysMsg.type == 'user_kicked' ? 'kick' : 'ban',
});
storeInfraction({ if (!message.channel ||
_id: ulid(), !sysMsg.user ||
createdBy: sysMsg.by?._id, recentEvents) return;
reason: 'Unknown reason (caught system message)',
date: message.createdAt, storeInfraction({
server: message.channel!.server_id, _id: ulid(),
type: InfractionType.Manual, createdBy: sysMsg.by?._id,
user: sysMsg.user!._id, reason: 'Unknown reason (caught system message)',
actionType: sysMsg.type == 'user_kicked' ? 'kick' : 'ban', date: message.createdAt,
} as Infraction).catch(console.warn); 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; break;
case 'user_joined': break; case 'user_joined': break;
case 'user_left' : break; case 'user_left' : break;