avatarURL function is broken?

Hey. I've been making a website with an integration through a Discord bot to get a staff list and its information. I've came accross an issue with the avatarURL function always returning null (I use an alternative for now, displayAvatarURL(). Here the code of the staff route:
const staffList = this.app.config.discordData.staffList;

let staffData: StaffData[] | undefined = this.app._cache.get("staffData");
if (!staffData) {
staffData = [];
const guild = this.app._client.guilds.cache.get(this.app.config.discordData.guildId) || await this.app._client.guilds.fetch(this.app.config.discordData.guildId);
for (const staff of staffList) {
const user = guild.members.cache.get(staff) || await guild.members.fetch(staff);
console.log(user.displayAvatarURL())
staffData.push({
staff: user,
highestRole: user.roles.highest,
avatar: user.avatarURL() as string
});
}

this.app._cache.set("staffData", staffData);
}

res.render("staff", { title: "Staff", data: staffData });
const staffList = this.app.config.discordData.staffList;

let staffData: StaffData[] | undefined = this.app._cache.get("staffData");
if (!staffData) {
staffData = [];
const guild = this.app._client.guilds.cache.get(this.app.config.discordData.guildId) || await this.app._client.guilds.fetch(this.app.config.discordData.guildId);
for (const staff of staffList) {
const user = guild.members.cache.get(staff) || await guild.members.fetch(staff);
console.log(user.displayAvatarURL())
staffData.push({
staff: user,
highestRole: user.roles.highest,
avatar: user.avatarURL() as string
});
}

this.app._cache.set("staffData", staffData);
}

res.render("staff", { title: "Staff", data: staffData });
And here is where I login the bot in the structures/app.ts file:
private initializeClient() {
this._client.on("ready", () => {
this.logger.info(`Connected to Discord through the bot ${this._client.user?.tag}`, "Discord");
});

this._client.login(this.config.discordData.botToken).catch(err => {
this.logger.error(`Discord connection failed; This may cause an outage.`);
});
}
private initializeClient() {
this._client.on("ready", () => {
this.logger.info(`Connected to Discord through the bot ${this._client.user?.tag}`, "Discord");
});

this._client.login(this.config.discordData.botToken).catch(err => {
this.logger.error(`Discord connection failed; This may cause an outage.`);
});
}
4 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!
monbrey
monbrey11mo ago
Thats not broken. It can return null. For when they dont have an avatar
Adam
AdamOP11mo ago
So avatarURL() function returns the custom avatar on a server or what? As every member has an avatar, but I can only retrieve it using the displayAvatarURL()
monbrey
monbrey11mo ago
Depends if you're calling it on a GuildMember or User, but yes, that's the point of the methods avatarURL() will return null if they don't have one displayAvatarURL() will default to a higher level avatar
Want results from more Discord servers?
Add your server