Log if user got disconnected and from who?

Something aint right. I am currently trying to make something like: Yeah user x disconnected user x. But when I try to do like newstate(voicestate).guild.fetchAuditLogs({type: discordjs.AuditLogEvent.MemberDisconnect}) then it simply just waits like 5 seconds and after that says that Cannot read properties of undefined (reading 'guild') at the newstate
6 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!
progamersischu
progamersischuOP14mo ago
voicestateupdate : async function(oldstate, newstate) {
console.log(newstate?.guild)
if(!newstate?.channel){
await newstate.guild.fetchAuditLogs({type: discordjs.AuditLogEvent.MemberDisconnect}).then(audit => {
let first = audit.entries.first()
if(!first) return;
console.log(first.createdTimestamp, Date.now() - 2000, Date.now() + 2000, Date.now())
if( first.createdTimestamp == Date.now() || first.createdTimestamp > Date.now() - 2000){
console.log(newstate.member.user.tag + " was disconnected from " + oldstate.channel.name + " by " + first.executor.tag + " (" + first.executor.id + ")")
}
})
}
}
voicestateupdate : async function(oldstate, newstate) {
console.log(newstate?.guild)
if(!newstate?.channel){
await newstate.guild.fetchAuditLogs({type: discordjs.AuditLogEvent.MemberDisconnect}).then(audit => {
let first = audit.entries.first()
if(!first) return;
console.log(first.createdTimestamp, Date.now() - 2000, Date.now() + 2000, Date.now())
if( first.createdTimestamp == Date.now() || first.createdTimestamp > Date.now() - 2000){
console.log(newstate.member.user.tag + " was disconnected from " + oldstate.channel.name + " by " + first.executor.tag + " (" + first.executor.id + ")")
}
})
}
}
And the guild obj:
<ref *2> Guild {
id: '583370607920545854',
name: 'ssssssssss2',
icon: '428e0939f68c4b38762bffcccedc9ccd',
features: [],
commands: <ref *1> GuildApplicationCommandManager {
permissions: ApplicationCommandPermissionsManager {
manager: [Circular *1],
guild: [Circular *2],
guildId: '583370607920545854',
commandId: null
},
guild: [Circular *2]
},
members: GuildMemberManager { guild: [Circular *2] },
channels: GuildChannelManager { guild: [Circular *2] },
bans: GuildBanManager { guild: [Circular *2] },
roles: RoleManager { guild: [Circular *2] },
presences: PresenceManager {},
voiceStates: VoiceStateManager { guild: [Circular *2] },
stageInstances: StageInstanceManager { guild: [Circular *2] },
invites: GuildInviteManager { guild: [Circular *2] },
scheduledEvents: GuildScheduledEventManager { guild: [Circular *2] },
autoModerationRules: AutoModerationRuleManager { guild: [Circular *2] },
available: true,
shardId: 0,
splash: null,
banner: null,
description: null,
verificationLevel: 0,
vanityURLCode: null,
nsfwLevel: 0,
premiumSubscriptionCount: 0,
discoverySplash: null,
memberCount: 8,
large: false,
premiumProgressBarEnabled: false,
applicationId: null,
afkTimeout: 300,
afkChannelId: null,
systemChannelId: null,
premiumTier: 0,
widgetEnabled: null,
widgetChannelId: null,
explicitContentFilter: 0,
mfaLevel: 0,
joinedTimestamp: 1677287925024,
defaultMessageNotifications: 0,
systemChannelFlags: SystemChannelFlagsBitField { bitfield: 0 },
maximumMembers: 500000,
maximumPresences: null,
maxVideoChannelUsers: 25,
approximateMemberCount: null,
approximatePresenceCount: null,
vanityURLUses: null,
rulesChannelId: null,
publicUpdatesChannelId: null,
preferredLocale: 'en-US',
ownerId: '539044928953516032',
emojis: GuildEmojiManager { guild: [Circular *2] },
stickers: GuildStickerManager { guild: [Circular *2] }
}
<ref *2> Guild {
id: '583370607920545854',
name: 'ssssssssss2',
icon: '428e0939f68c4b38762bffcccedc9ccd',
features: [],
commands: <ref *1> GuildApplicationCommandManager {
permissions: ApplicationCommandPermissionsManager {
manager: [Circular *1],
guild: [Circular *2],
guildId: '583370607920545854',
commandId: null
},
guild: [Circular *2]
},
members: GuildMemberManager { guild: [Circular *2] },
channels: GuildChannelManager { guild: [Circular *2] },
bans: GuildBanManager { guild: [Circular *2] },
roles: RoleManager { guild: [Circular *2] },
presences: PresenceManager {},
voiceStates: VoiceStateManager { guild: [Circular *2] },
stageInstances: StageInstanceManager { guild: [Circular *2] },
invites: GuildInviteManager { guild: [Circular *2] },
scheduledEvents: GuildScheduledEventManager { guild: [Circular *2] },
autoModerationRules: AutoModerationRuleManager { guild: [Circular *2] },
available: true,
shardId: 0,
splash: null,
banner: null,
description: null,
verificationLevel: 0,
vanityURLCode: null,
nsfwLevel: 0,
premiumSubscriptionCount: 0,
discoverySplash: null,
memberCount: 8,
large: false,
premiumProgressBarEnabled: false,
applicationId: null,
afkTimeout: 300,
afkChannelId: null,
systemChannelId: null,
premiumTier: 0,
widgetEnabled: null,
widgetChannelId: null,
explicitContentFilter: 0,
mfaLevel: 0,
joinedTimestamp: 1677287925024,
defaultMessageNotifications: 0,
systemChannelFlags: SystemChannelFlagsBitField { bitfield: 0 },
maximumMembers: 500000,
maximumPresences: null,
maxVideoChannelUsers: 25,
approximateMemberCount: null,
approximatePresenceCount: null,
vanityURLUses: null,
rulesChannelId: null,
publicUpdatesChannelId: null,
preferredLocale: 'en-US',
ownerId: '539044928953516032',
emojis: GuildEmojiManager { guild: [Circular *2] },
stickers: GuildStickerManager { guild: [Circular *2] }
}
Because somehow I get undefined sometimes with newstate.channel or smth else when it's defined when I log it. A bit bugged I guess client.on("voiceStateUpdate",async (oldstate,newstate) => { await betterlogs.handler({type: "voicestateupdate", oldstate: oldstate, newstate: newstate}) ....
progamersischu
progamersischuOP14mo ago
No description
progamersischu
progamersischuOP14mo ago
Want to do it in different files. Because otherwise my main file is going to be super big
d.js docs
d.js docs14mo ago
guide Creating Your Bot: Event handling - Individual event files read more
progamersischu
progamersischuOP14mo ago
Yeah. But still...doesn't make sense. I mean. I get a value for newstate and newstate.guild but after 5 seconds it tells me that its undefined I will do that thx Found the error. But like I do it, it isn't possible that way. I thought that every action is logged seperatly in the audit log but it just counts to 1,2,3,4,.. till like a certain time passed
Want results from more Discord servers?
Add your server