Problem with AuditLogs (MessageDelete)

I have some problem. If some other person deletes my message, and then I delete my messages, it will write that he deleted my messages. How to fix it?
let deleter = null
const auditLogs = await msg.guild.fetchAuditLogs({
type: 72,
limit: 1,
time: {after: msg.createdTimestamp},
target: msg.id
})
const auditLogEntry = auditLogs.entries.first()
if (
auditLogEntry?.target?.id === msg.author.id &&
auditLogEntry?.extra?.channel?.id === msg.channel.id
) {
deleter = auditLogEntry.executor;
}
let deleter = null
const auditLogs = await msg.guild.fetchAuditLogs({
type: 72,
limit: 1,
time: {after: msg.createdTimestamp},
target: msg.id
})
const auditLogEntry = auditLogs.entries.first()
if (
auditLogEntry?.target?.id === msg.author.id &&
auditLogEntry?.extra?.channel?.id === msg.channel.id
) {
deleter = auditLogEntry.executor;
}
10 Replies
d.js toolkit
d.js toolkit11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
emerald
emerald11mo ago
because you're fetching the latest log and deleting your own message doesn't create a log. you could create a work around by checking if the log was created in the past 5 seconds
vlad vollar
vlad vollar11mo ago
Can you please suggest how to do this? I don't understand
emerald
emerald11mo ago
you could check if your auditLogEntry createdTimestamp was in the last 5 seconds and if it isn't return the author of the message but this could have some flaws like for example if a bot deletes a message it could confuse that with the author deleting their own message
vlad vollar
vlad vollar11mo ago
It works the first few times, but then it doesn't detect up
Jhay
Jhay11mo ago
the code is fetching the latest audit log entry for any message deletions that have occurred after the creation of the message you're checking, regardless of who deleted it so if someone else deletes your message before you delete it yourself, the audit log entry for the other person's deletion will be returned instead of the entry for your own deletion you need to modify the code to filter out any audit log entries that do not correspond to your own deletion of the message you can do this is to add an additional check that verifies that the audit log entry was generated by your own deletion of the message
let deleter = null
const auditLogs = await msg.guild.fetchAuditLogs({
type: 72,
limit: 1,
time: {after: msg.createdTimestamp},
target: msg.id
})
const auditLogEntry = auditLogs.entries.first()
if (
auditLogEntry?.target?.id === msg.author.id &&
auditLogEntry?.extra?.channel?.id === msg.channel.id &&
auditLogEntry?.executor?.id === msg.author.id
) {
deleter = auditLogEntry.executor;
}
let deleter = null
const auditLogs = await msg.guild.fetchAuditLogs({
type: 72,
limit: 1,
time: {after: msg.createdTimestamp},
target: msg.id
})
const auditLogEntry = auditLogs.entries.first()
if (
auditLogEntry?.target?.id === msg.author.id &&
auditLogEntry?.extra?.channel?.id === msg.channel.id &&
auditLogEntry?.executor?.id === msg.author.id
) {
deleter = auditLogEntry.executor;
}
i added an additional check that verifies that the executor of the audit log entry (i.e., the user who performed the action that generated the entry) is the same as the author of the message this ensures that the audit log entry corresponds to your own deletion of the message, rather than someone else's deletion.
emerald
emerald11mo ago
? an entry isnt made in the audit log when a user deletes their own message
vlad vollar
vlad vollar11mo ago
vlad vollar
vlad vollar11mo ago
up
d.js docs
d.js docs11mo ago
property GuildAuditLogsEntry#createdAt The time this entry was created at