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';
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,

View file

@ -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)`);
}

View file

@ -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;