User leave issue

Hi, I'm making an invite tracking bot for myself, however my leave logic isn't recognising when a user leaves? Originally I thought it may be an SQLite issue, however when adding a debug print statement it doesn't print anything, which is weird because my join logic work and sucesfully prints, could somebody help?
client.on("guildMemberRemove", async member => {
const db = await dbPromise;

const guildInvites = await member.guild.invites.fetch().catch(() => null);
if (!guildInvites) return;

const cachedInvites = invitesCache.get(member.guild.id);
const inviteUsed = guildInvites.find(inv => cachedInvites && cachedInvites.get(inv.code) < inv.uses);

if (!inviteUsed || inviteUsed.code === member.guild.vanityURLCode) return;

const inviterId = inviteUsed.inviter.id;

console.log(`User left after invited from ${inviteUsed.inviter.username} using link ${inviteUsed.code}`);


await db.run("UPDATE invites SET invites = invites - 1 WHERE userId = ?", inviterId);
});

client.on("guildMemberRemove", async member => {
const db = await dbPromise;

const guildInvites = await member.guild.invites.fetch().catch(() => null);
if (!guildInvites) return;

const cachedInvites = invitesCache.get(member.guild.id);
const inviteUsed = guildInvites.find(inv => cachedInvites && cachedInvites.get(inv.code) < inv.uses);

if (!inviteUsed || inviteUsed.code === member.guild.vanityURLCode) return;

const inviterId = inviteUsed.inviter.id;

console.log(`User left after invited from ${inviteUsed.inviter.username} using link ${inviteUsed.code}`);


await db.run("UPDATE invites SET invites = invites - 1 WHERE userId = ?", inviterId);
});

12 Replies
d.js toolkit
d.js toolkit5d 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!
br1t
br1tOP5d ago
treble/luna
treble/luna5d ago
The event only emits for cached members if you dont have the partial
d.js docs
d.js docs5d ago
:guide: Popular Topics: Partial Structures read more
treble/luna
treble/luna5d ago
With the partial you will only receive the id
br1t
br1tOP5d ago
partials: [
'GUILD_MEMBER'

],
partials: [
'GUILD_MEMBER'

],
I assume it would be this? oops
treble/luna
treble/luna5d ago
No Read the guide i linked
br1t
br1tOP5d ago
I did and I am still just as confused?
treble/luna
treble/luna5d ago
The guide shows the correct way of defining partials
br1t
br1tOP5d ago
Still doesn't work for me, nothing is outputted not even an error
client.on("guildMemberRemove", async member => {

if (member.partial) {
try {

member = await member.fetch();
} catch (error) {
console.error('Error fetching member:', error);
return;
}
}

const guild = member.guild;


const guildInvites = await guild.invites.fetch().catch(() => null);
if (!guildInvites) return;


const cachedInvites = invitesCache.get(guild.id);
const inviteUsed = guildInvites.find(inv => cachedInvites && cachedInvites.get(inv.code) < inv.uses);


if (!inviteUsed || inviteUsed.code === guild.vanityURLCode) return;

const inviterId = inviteUsed.inviter.id;


console.log(`User left after invited from ${inviteUsed.inviter.username} using link ${inviteUsed.code}`);

const db = await dbPromise;


await db.run("UPDATE invites SET invites = invites - 1 WHERE userId = ?", inviterId);


invitesCache.set(guild.id, new Map(guildInvites.map(inv => [inv.code, inv.uses])));
invitedUsers.delete(member.id);
});

client.on("guildMemberRemove", async member => {

if (member.partial) {
try {

member = await member.fetch();
} catch (error) {
console.error('Error fetching member:', error);
return;
}
}

const guild = member.guild;


const guildInvites = await guild.invites.fetch().catch(() => null);
if (!guildInvites) return;


const cachedInvites = invitesCache.get(guild.id);
const inviteUsed = guildInvites.find(inv => cachedInvites && cachedInvites.get(inv.code) < inv.uses);


if (!inviteUsed || inviteUsed.code === guild.vanityURLCode) return;

const inviterId = inviteUsed.inviter.id;


console.log(`User left after invited from ${inviteUsed.inviter.username} using link ${inviteUsed.code}`);

const db = await dbPromise;


await db.run("UPDATE invites SET invites = invites - 1 WHERE userId = ?", inviterId);


invitesCache.set(guild.id, new Map(guildInvites.map(inv => [inv.code, inv.uses])));
invitedUsers.delete(member.id);
});

treble/luna
treble/luna5d ago
First of all that fetch wont ever work, there is no member anymore. Second, show how you added the partial
br1t
br1tOP5d ago
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildInvites
],
partials: [Partials.GuildMember],
});
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildInvites
],
partials: [Partials.GuildMember],
});
probs not how your meant to do it but I need those intents, and documents didn;t go over that

Did you find this page helpful?