fix: avatarURL doesn't correctly return the default avatar (#119)

* fix(User|Member): avatarURL with default avatar

* Member#avatarURL fallback
This commit is contained in:
Drylozu 2022-10-12 11:23:30 -05:00 committed by GitHub
parent 388540e401
commit 6c6f982100
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 39 deletions

View File

@ -10,11 +10,7 @@ import type { AvatarOptions } from './user';
import { User } from './user'; import { User } from './user';
import { Guild } from './guilds'; import { Guild } from './guilds';
import { Util } from '../utils/util'; import { Util } from '../utils/util';
import { import { USER_AVATAR, THREAD_USER } from '@biscuitland/api-types';
USER_AVATAR,
USER_DEFAULT_AVATAR,
THREAD_USER,
} from '@biscuitland/api-types';
import { Permissions } from './special/permissions'; import { Permissions } from './special/permissions';
/** /**
@ -39,8 +35,8 @@ export class Member implements Model {
? Number.parseInt(data.premium_since) ? Number.parseInt(data.premium_since)
: undefined; : undefined;
this.channelPermissions = data.permissions ? new Permissions(BigInt(data.permissions)) : undefined; this.channelPermissions = data.permissions ? new Permissions(BigInt(data.permissions)) : undefined;
this.joinedTimestamp = Number.parseInt(data.joined_at); this.joinedTimestamp = Number.parseInt(data.joined_at);
this.roles = data.roles; this.roles = data.roles;
this.deaf = !!data.deaf; this.deaf = !!data.deaf;
this.mute = !!data.mute; this.mute = !!data.mute;
@ -69,8 +65,8 @@ export class Member implements Model {
/** when the user started boosting the guild */ /** when the user started boosting the guild */
premiumSince?: number; premiumSince?: number;
/** total permissions of the member in the channel, including overwrites, returned when in the interaction object */ /** total permissions of the member in the channel, including overwrites, returned when in the interaction object */
channelPermissions?: Permissions; channelPermissions?: Permissions;
/** when the user joined the guild */ /** when the user joined the guild */
joinedTimestamp: number; joinedTimestamp: number;
@ -146,10 +142,10 @@ export class Member implements Model {
return member; return member;
} }
/** calls {@link Member#edit} which calls {@link Guild#editMember} under the hood */ /** calls {@link Member#edit} which calls {@link Guild#editMember} under the hood */
async timeout(time: number | null) { async timeout(time: number | null) {
await this.edit({ communicationDisabledUntil: time }); await this.edit({ communicationDisabledUntil: time });
} }
/** adds a role to this member */ /** adds a role to this member */
async addRole(roleId: Snowflake, reason?: string): Promise<void> { async addRole(roleId: Snowflake, reason?: string): Promise<void> {
@ -174,30 +170,22 @@ export class Member implements Model {
); );
} }
async fetch(): Promise<Member> { async fetch(): Promise<Member> {
const member = await Guild.prototype.fetchMember.call({ session: this.session, id: this.guildId }, this.id); const member = await Guild.prototype.fetchMember.call({ session: this.session, id: this.guildId }, this.id);
return member; return member;
} }
/** gets the members's guild avatar, not to be confused with Member.user.avatarURL() */ /** gets the members's guild avatar if the user has one, gets the user's avatar instead */
avatarURL(options: AvatarOptions): string { avatarURL(options: AvatarOptions): string {
let url: string; if (!this.avatarHash) {
if (this.user.bot) {
return this.user.avatarURL(options); return this.user.avatarURL(options);
} }
if (!this.avatarHash) { return Util.formatImageURL(USER_AVATAR(
url = USER_DEFAULT_AVATAR(Number(this.user.discriminator) % 5); this.user.id,
} else { this.avatarHash
url = USER_AVATAR( ), options.size ?? 128, options.format);
this.user.id,
this.avatarHash
);
}
return Util.formatImageURL(url, options.size ?? 128, options.format);
} }
/** /**

View File

@ -121,7 +121,7 @@ export class Permissions implements BitField<bigint> {
} }
} }
static sum(permissions: Array<bigint | number>) { static sum(permissions: (bigint | number)[]) {
return permissions.reduce((y, x) => BigInt(y) | BigInt(x), Permissions.None); return permissions.reduce((y, x) => BigInt(y) | BigInt(x), Permissions.None);
} }

View File

@ -27,7 +27,7 @@ export class User implements Model {
? data.avatar ? data.avatar
: undefined; : undefined;
this.accentColor = data.accent_color; this.accentColor = data.accent_color;
this.bot = !!data.bot; this.bot = !!data.bot;
this.system = !!data.system; this.system = !!data.system;
this.banner = data.banner this.banner = data.banner
@ -103,15 +103,14 @@ export class User implements Model {
/** gets the user's avatar */ /** gets the user's avatar */
avatarURL(options: AvatarOptions): string { avatarURL(options: AvatarOptions): string {
let url: string;
if (!this.avatarHash) { if (!this.avatarHash) {
url = USER_DEFAULT_AVATAR(Number(this.discriminator) % 5); return USER_DEFAULT_AVATAR(Number(this.discriminator) % 5);
} else {
url = USER_AVATAR(this.id, this.avatarHash);
} }
return Util.formatImageURL(url, options.size ?? 128, options.format); return Util.formatImageURL(USER_AVATAR(
this.id,
this.avatarHash
), options.size ?? 128, options.format);
} }
openDM(): Promise<DMChannel> { openDM(): Promise<DMChannel> {