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';
/** /**
@ -180,24 +176,16 @@ export class Member implements Model {
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);
} else {
url = USER_AVATAR(
this.user.id, this.user.id,
this.avatarHash this.avatarHash
); ), options.size ?? 128, options.format);
}
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

@ -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> {