Skip to content

Commit

Permalink
feat(logger): improvements
Browse files Browse the repository at this point in the history
* Add click-to-navigate buttons on message edit/delete logs
* Fix ban log showing wrong reason
  • Loading branch information
virtual-designer committed Nov 27, 2022
1 parent a4d4121 commit 78073dd
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 60 deletions.
110 changes: 64 additions & 46 deletions src/automod/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { roleMention } from '@discordjs/builders';
import { formatDuration, intervalToDuration } from 'date-fns';
import { BanOptions, CommandInteraction, FileOptions, Guild, GuildBan, GuildMember, Message, MessageEmbed, MessageOptions, MessagePayload, TextChannel, User } from 'discord.js';
import { BanOptions, CommandInteraction, FileOptions, Guild, GuildBan, GuildMember, Message, MessageActionRow, MessageButton, MessageEmbed, MessageOptions, MessagePayload, TextChannel, User } from 'discord.js';
import ms from 'ms';
import DiscordClient from '../client/Client';
import { IPunishment } from '../models/Punishment';
Expand Down Expand Up @@ -88,6 +88,10 @@ class Logger {
{
name: 'User ID',
value: newMsg.author.id
},
{
name: 'Channel',
value: `${newMsg.channel} (${newMsg.channel.id})`
})
.setAuthor({
name: newMsg.author.tag,
Expand All @@ -97,6 +101,15 @@ class Logger {
text: "Edited",
})
.setTimestamp()
],
components: [
new MessageActionRow()
.addComponents(
new MessageButton()
.setLabel('Go to context')
.setStyle('LINK')
.setURL(newMsg.url)
)
]
});
}, newMsg);
Expand All @@ -106,7 +119,7 @@ class Logger {
this.channel(async (channel) => {
const embed = new MessageEmbed()
.setColor('#f14a60')
.setTitle('Message Deleted in #' + (msg.channel as TextChannel).name + " (" + msg.channel.id + ")")
.setTitle('Message Deleted')
.setDescription(msg.content)
.setAuthor({
name: msg.author.tag,
Expand All @@ -119,6 +132,10 @@ class Logger {
{
name: 'User ID',
value: msg.author.id
},
{
name: 'Channel',
value: `${msg.channel} (${msg.channel.id})`
})
.setFooter({
text: "Deleted",
Expand All @@ -145,58 +162,59 @@ class Logger {
embeds: [
embed
],
files
files,
components: [
new MessageActionRow()
.addComponents(
new MessageButton()
.setLabel('Go to context')
.setStyle('LINK')
.setURL(msg.url)
)
]
});
}, msg);
}

logBanned(ban: GuildBan) {
this.channel(async (channel) => {
let r = '*No reason provided*';
async loggingChannel(guild: Guild) {
return <TextChannel> guild.channels.cache.get(DiscordClient.client.config.props[guild.id].logging_channel);
}

const auditLog = (await ban.guild.fetchAuditLogs({
limit: 1,
type: 'MEMBER_BAN_ADD',
})).entries.first();

if (ban.reason) {
r = ban.reason;
}
else if (auditLog) {
console.log(auditLog);
const { target, reason } = await auditLog;
async logBanned(ban: GuildBan) {
const auditLog = (await ban.guild.fetchAuditLogs({
limit: 1,
type: 'MEMBER_BAN_ADD',
})).entries.first();

if (target!.id === ban.user.id && reason) {
r = reason.replace(/^\[BAN\]/, '');
}
if (auditLog?.executor?.id === this.client.user!.id) {
console.log("Action taken by bot");
return;
}

if (auditLog.executor?.id === this.client.user!.id || (r.startsWith("[TEMPBAN]") || r.startsWith("[SOFTBAN]"))) {
return;
}
}
const guildBan = await ban.guild.bans.fetch(ban.user.id);
const reason = guildBan.reason ?? '*No reason provided*';

await channel.send({
embeds: [
new MessageEmbed()
.setColor('#f14a60')
.setTitle("A user was banned")
.setAuthor({
name: ban.user.tag,
iconURL: ban.user.displayAvatarURL(),
})
.addField('Reason', r)
.addField('User ID', ban.user.id)
.addFields({
name: 'Banned by',
value: auditLog?.executor ? `${auditLog.executor.tag} (${auditLog.executor.id})` : 'Unknown'
})
.setFooter({
text: "Banned",
})
.setTimestamp()
]
});
}, ban);
await (await this.loggingChannel(ban.guild)).send({
embeds: [
new MessageEmbed()
.setColor('#f14a60')
.setTitle("A user was banned")
.setAuthor({
name: guildBan.user.tag,
iconURL: guildBan.user.displayAvatarURL(),
})
.addField('Reason', reason)
.addField('User ID', guildBan.user.id)
.addFields({
name: 'Banned by',
value: auditLog?.executor ? `${auditLog.executor.tag} (${auditLog.executor.id})` : 'Unknown'
})
.setFooter({
text: "Banned",
})
.setTimestamp()
]
});
}

logSoftBan(banOptions: BanOptions, guild: Guild, user: User, model: IPunishment) {
Expand Down
30 changes: 16 additions & 14 deletions src/events/guildBan/GuildBanAddEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@ export default class GuildBanAddEvent extends BaseEvent {
}

async run(client: DiscordClient, ban: GuildBan) {
await client.logger.logBanned(ban);
setTimeout(async () => {
await client.logger.logBanned(ban);

const logs = (await ban.guild.fetchAuditLogs({
limit: 1,
type: 'MEMBER_BAN_ADD',
})).entries.first();
const logs = (await ban.guild.fetchAuditLogs({
limit: 1,
type: 'MEMBER_BAN_ADD',
})).entries.first();

console.log(logs?.executor);
console.log(logs?.executor);

await Punishment.create({
type: PunishmentType.BAN,
user_id: ban.user.id,
guild_id: ban.guild!.id,
mod_id: logs?.executor?.id ?? client.user!.id,
mod_tag: logs?.executor?.tag ?? 'Unknown',
reason: ban.reason ?? undefined
});
await Punishment.create({
type: PunishmentType.BAN,
user_id: ban.user.id,
guild_id: ban.guild!.id,
mod_id: logs?.executor?.id ?? client.user!.id,
mod_tag: logs?.executor?.tag ?? 'Unknown',
reason: ban.reason ?? undefined
});
}, 3500);
}
}

1 comment on commit 78073dd

@vercel
Copy link

@vercel vercel bot commented on 78073dd Nov 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.