Getting the executor from Audit Logs

Hello i have this code:
const fetchedLogs = await oldMember.guild.fetchAuditLogs({
limit: 1,
type: AuditLogEvent.MemberUpdate,
});

const userWhoDidIt = fetchedLogs.entries.first();
const executor = userWhoDidIt.executor
const fetchedLogs = await oldMember.guild.fetchAuditLogs({
limit: 1,
type: AuditLogEvent.MemberUpdate,
});

const userWhoDidIt = fetchedLogs.entries.first();
const executor = userWhoDidIt.executor
But the userWhoDidIt is undefined
26 Replies
d.js toolkit
d.js toolkit14mo 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!
duck
duck14mo ago
sounds like it was unable to find an audit log entry for that type
Frosty
FrostyOP14mo ago
Oh, i will try again it tomorrow! I was checking docs and only found MemberUpdate So i made the change, but now my code is:
const fetchedLogs = await oldMember.guild.fetchAuditLogs({
limit: 1,
type: AuditLogEvent.MemberRoleUpdate,
});

const offender = fetchedLogs.entries.first();
const executor = await newMember.guild.members.fetch(offender.id);
const fetchedLogs = await oldMember.guild.fetchAuditLogs({
limit: 1,
type: AuditLogEvent.MemberRoleUpdate,
});

const offender = fetchedLogs.entries.first();
const executor = await newMember.guild.members.fetch(offender.id);
But it cannot find the user using the ID
duck
duck14mo ago
offender appears to be the entry itself <GuildAuditLogsEntry>.id is the id of the entry, not the target's id <GuildAuditLogsEntry>.targetId exists
d.js docs
d.js docs14mo ago
property GuildAuditLogsEntry#targetId The id of the target of this entry
duck
duck14mo ago
unless you were looking to fetch the executor? your var names are a little confusing regardless <GuildAuditLogsEntry>.executorId also exists
Frosty
FrostyOP14mo ago
So it should be
const offender = fetchedLogs.entries.first();
const executor = await newMember.guild.members.fetch(offender.executorId);
const offender = fetchedLogs.entries.first();
const executor = await newMember.guild.members.fetch(offender.executorId);
duck
duck14mo ago
sure
Frosty
FrostyOP14mo ago
1 other question, the MemberRoleUpdate works only when i give a role, or does it also work when i remove a role? Coz right now it only works whenever i give someone a role, when removed, then it does nothing.,
duck
duck14mo ago
if it does nothing, then likely this code isn't executing it wouldn't be due to the audit log event type regardless, the answer is MemberRoleUpdate is also for removing roles
d.js docs
d.js docs14mo ago
If you aren't getting any errors, try to place console.log checkpoints throughout your code to find out where execution stops. - Once you do, log relevant values and if-conditions - More sophisticated debugging methods are breakpoints and runtime inspections: learn more
Frosty
FrostyOP14mo ago
Also, the MemebrRoleUpdate only executes when someone is chaging ROLEs on user, nothing else, right?
duck
duck14mo ago
I'm not exactly sure what else you'd expect an event type of that name to be for
Frosty
FrostyOP14mo ago
But if i have it in the guildMemberUpdate event, then if i only want the role updates i can do
if(!fetchedLogs) return;
if(!fetchedLogs) return;
?
duck
duck14mo ago
this doesn't really tell me a ton about the rest of your logic but assuming that's the only check, you may find a false positive when an older audit log entry exists for a member update unrelated to roles
Frosty
FrostyOP14mo ago
What i ment is that, guildMemberUpdate event also gets triggered when someone updates somoenes name right? I only want to go through when the evnt is triggered and a ROLE is given
duck
duck14mo ago
I understand that please read my message
Frosty
FrostyOP14mo ago
Oki 🙂 I got that yeah Thanks Also 1 more question hehe If i wanna remove all bot's roles expect their own bot role coz das impossible, how can i do that?
duck
duck14mo ago
you can access the client member for a given guild with <Guild>.members.me you can completely overwrite a member's role list with <GuildMember>.roles.set() since set accepts a Collection as well, you can simply pass a filtered <GuildMember>.roles.cache
d.js docs
d.js docs14mo ago
method GuildMemberRoleManager#set() Sets the roles applied to the member.
duck
duck14mo ago
members are also unable to remove their own highest role
Frosty
FrostyOP14mo ago
So how do i define the bot's own role in the guild?, with the .me?
duck
duck14mo ago
as mentioned previously, .me is the client member for the given guild it's a GuildMember <GuildMember>.roles.botRole exists, but in general you can also just check for <Role>.managed when filtering roles
Frosty
FrostyOP14mo ago
So when i'd like my bot, to remove another bots all roles expect the bot's own role, how shall i do it?
duck
duck14mo ago
<GuildMember>.roles.botRole exists, but in general you can also just check for <Role>.managed when filtering roles
Frosty
FrostyOP14mo ago
But could i do something like this:
let botRoleIs = newMember.roles.botRole
let botAllRoles = newMember.roles.cache
let botRoleIs = newMember.roles.botRole
let botAllRoles = newMember.roles.cache
So now i filter from botAllRoles and then i remove the rest?
Want results from more Discord servers?
Add your server