Can't get 'uses' property on fetched invites

Trying to get all the invites from the guild
import { Guild } from 'discord.js';
import { client } from '../client';
import fs from 'node:fs';

export const fetchInvites = async (guild: Guild): Promise<void> => {
const inv = await guild.invites.fetch();
fs.writeFile('invites.json', JSON.stringify(inv), (err) => {
if (err) console.log(err);
});
console.log('Invites cached');
};
import { Guild } from 'discord.js';
import { client } from '../client';
import fs from 'node:fs';

export const fetchInvites = async (guild: Guild): Promise<void> => {
const inv = await guild.invites.fetch();
fs.writeFile('invites.json', JSON.stringify(inv), (err) => {
if (err) console.log(err);
});
console.log('Invites cached');
};
Invite objects with the 'uses' counter are expected to be received, but none of them have it at all
No description
18 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! - Marked as resolved by staff
Harnes
Harnes14mo ago
Can you log inv? (Also thats not how cache works)
waseeeen
waseeeenOP14mo ago
It is already on the screenshot, since it writed to the file for debugging purposes
Harnes
Harnes14mo ago
Show your Client constructor
waseeeen
waseeeenOP14mo ago
import { Client, Options, Partials, Collection } from 'discord.js';
import { readdirSync } from 'fs';
import { join } from 'path';
import { Button, SlashCommand } from '../types';
const { Reaction, Message } = Partials;

const allIntents =
(1 << 0) + // GUILDS
(1 << 1) + // GUILD_MEMBERS
(1 << 2) + // GUILD_MODERATION
(1 << 3) + // GUILD_EMOJIS_AND_STICKERS
(1 << 4) + // GUILD_INTEGRATIONS
(1 << 5) + // GUILD_WEBHOOKS
(1 << 6) + // GUILD_INVITES
(1 << 7) + // GUILD_VOICE_STATES
(1 << 8) + // GUILD_PRESENCES
(1 << 9) + // GUILD_MESSAGES
(1 << 10) + // GUILD_MESSAGE_REACTIONS
(1 << 11) + // GUILD_MESSAGE_TYPING
(1 << 12) + // DIRECT_MESSAGES
(1 << 13) + // DIRECT_MESSAGE_REACTIONS
(1 << 14) + // DIRECT_MESSAGE_TYPING
(1 << 15) + // MESSAGE_CONTENT
(1 << 16) + // GUILD_SCHEDULED_EVENTS
(1 << 20) + // AUTO_MODERATION_CONFIGURATION
(1 << 21); // AUTO_MODERATION_EXECUTION

export const client = new Client({
makeCache: Options.cacheWithLimits({
GuildMemberManager: 1000,
}),

intents: allIntents,
partials: [Reaction, Message],
});
import { Client, Options, Partials, Collection } from 'discord.js';
import { readdirSync } from 'fs';
import { join } from 'path';
import { Button, SlashCommand } from '../types';
const { Reaction, Message } = Partials;

const allIntents =
(1 << 0) + // GUILDS
(1 << 1) + // GUILD_MEMBERS
(1 << 2) + // GUILD_MODERATION
(1 << 3) + // GUILD_EMOJIS_AND_STICKERS
(1 << 4) + // GUILD_INTEGRATIONS
(1 << 5) + // GUILD_WEBHOOKS
(1 << 6) + // GUILD_INVITES
(1 << 7) + // GUILD_VOICE_STATES
(1 << 8) + // GUILD_PRESENCES
(1 << 9) + // GUILD_MESSAGES
(1 << 10) + // GUILD_MESSAGE_REACTIONS
(1 << 11) + // GUILD_MESSAGE_TYPING
(1 << 12) + // DIRECT_MESSAGES
(1 << 13) + // DIRECT_MESSAGE_REACTIONS
(1 << 14) + // DIRECT_MESSAGE_TYPING
(1 << 15) + // MESSAGE_CONTENT
(1 << 16) + // GUILD_SCHEDULED_EVENTS
(1 << 20) + // AUTO_MODERATION_CONFIGURATION
(1 << 21); // AUTO_MODERATION_EXECUTION

export const client = new Client({
makeCache: Options.cacheWithLimits({
GuildMemberManager: 1000,
}),

intents: allIntents,
partials: [Reaction, Message],
});
Harnes
Harnes14mo ago
Its better to use GatewayIntentBits enum
waseeeen
waseeeenOP14mo ago
planned just copied from one old project, so already all intents on the hand but this will not affect the topic of the question in any way as temporary solution now I use
await rest.get(Routes.guildInvites(guild.id))
await rest.get(Routes.guildInvites(guild.id))
d.js docs
d.js docs14mo ago
method GuildInviteManager#fetch() Fetches invite(s) from Discord.
waseeeen
waseeeenOP14mo ago
no I cant. there are no uses property in retrieved objects they are in Invite class, but they are not in Invite objects, that I get from guild.invites.fetch() check attachment so only rest method is the solution? but invites, that I get from rest request have both uses and maxUses properties even if maxUses is 0 sorry for answering so late
import { Guild, Invite, Routes } from 'discord.js';
import { client, rest } from '../client';
import fs from 'fs';

export const fetchInvites = async (guild: Guild): Promise<void> => {
const invFetch = await guild.invites.fetch();
const invRest = await rest.get(Routes.guildInvites(guild.id));
const inv = (await rest.get(Routes.guildInvites(guild.id))) as Invite[];
client.invites = inv;
fs.appendFile('invitesDjs.json', JSON.stringify(invFetch), function (err) {
if (err) throw err;
console.log('Saved!');
});

fs.appendFile('invitesRest.json', JSON.stringify(invRest), function (err) {
if (err) throw err;
console.log('Saved!');
});

console.log('Invites cached');
};
import { Guild, Invite, Routes } from 'discord.js';
import { client, rest } from '../client';
import fs from 'fs';

export const fetchInvites = async (guild: Guild): Promise<void> => {
const invFetch = await guild.invites.fetch();
const invRest = await rest.get(Routes.guildInvites(guild.id));
const inv = (await rest.get(Routes.guildInvites(guild.id))) as Invite[];
client.invites = inv;
fs.appendFile('invitesDjs.json', JSON.stringify(invFetch), function (err) {
if (err) throw err;
console.log('Saved!');
});

fs.appendFile('invitesRest.json', JSON.stringify(invRest), function (err) {
if (err) throw err;
console.log('Saved!');
});

console.log('Invites cached');
};
Kinect3000
Kinect300014mo ago
Is rest defined as client.rest?
waseeeen
waseeeenOP14mo ago
rest defined in client/index.ts as
export const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);
export const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);
Kinect3000
Kinect300014mo ago
Can you try using client.rest instead of rest?
waseeeen
waseeeenOP14mo ago
same zero difference between client.rest and REST() if I log it without stringifying it appears in console as [object Object]
waseeeen
waseeeenOP14mo ago
import { Guild, Invite, Routes } from 'discord.js';
import { client, rest } from '../client';

export const fetchInvites = async (guild: Guild): Promise<void> => {
const inv = (await rest.get(Routes.guildInvites(guild.id))) as Invite[];
const invRest = await rest.get(Routes.guildInvites(guild.id));
const invFetch = await guild.invites.fetch();
console.log('==invites REST==\n' + invRest + '\n==end invites REST==');
console.log('==invites Fetch==\n' + invFetch + '\n==end invites Fetch==');
client.invites = inv;

console.log('Invites cached');
};
import { Guild, Invite, Routes } from 'discord.js';
import { client, rest } from '../client';

export const fetchInvites = async (guild: Guild): Promise<void> => {
const inv = (await rest.get(Routes.guildInvites(guild.id))) as Invite[];
const invRest = await rest.get(Routes.guildInvites(guild.id));
const invFetch = await guild.invites.fetch();
console.log('==invites REST==\n' + invRest + '\n==end invites REST==');
console.log('==invites Fetch==\n' + invFetch + '\n==end invites Fetch==');
client.invites = inv;

console.log('Invites cached');
};
waseeeen
waseeeenOP14mo ago
got it
import { Guild, Invite, Routes } from 'discord.js';
import { client, rest } from '../client';

export const fetchInvites = async (guild: Guild): Promise<void> => {
const inv = (await rest.get(Routes.guildInvites(guild.id))) as Invite[];
const invRest = await rest.get(Routes.guildInvites(guild.id));
const invFetch = await guild.invites.fetch();
console.log('==invites REST==');
console.log(invRest);
console.log('==end invites REST==');
console.log('==invites Fetch==');
console.log(invFetch);
console.log('==end invites Fetch==');
client.invites = inv;

console.log('Invites cached');
};
import { Guild, Invite, Routes } from 'discord.js';
import { client, rest } from '../client';

export const fetchInvites = async (guild: Guild): Promise<void> => {
const inv = (await rest.get(Routes.guildInvites(guild.id))) as Invite[];
const invRest = await rest.get(Routes.guildInvites(guild.id));
const invFetch = await guild.invites.fetch();
console.log('==invites REST==');
console.log(invRest);
console.log('==end invites REST==');
console.log('==invites Fetch==');
console.log(invFetch);
console.log('==end invites Fetch==');
client.invites = inv;

console.log('Invites cached');
};
oh, there it is
waseeeen
waseeeenOP14mo ago
waseeeen
waseeeenOP14mo ago
the thing is, I couldn't access uses previously, so I decided to console.log(JSON.stringify(invFetch)) and as uses wasn't there I thought that they are not exist in fetched invites I've already forgotten and not commited the way I tried @Qjuh tyvm, looks like I need to learn more about js objects 🤓
Want results from more Discord servers?
Add your server