-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(classes): make classes inherit more from base
- Loading branch information
Showing
6 changed files
with
143 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type { Imperial } from "../client"; | ||
import type { User as IUser } from "../types/users"; | ||
import { Base } from "./Base"; | ||
|
||
export class User extends Base<IUser> { | ||
constructor(client: Imperial, user: IUser) { | ||
super(client); | ||
|
||
if (!user) throw new Error("User: No data provided"); | ||
|
||
this._patch(user); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public _patch(user: IUser) { | ||
if ("username" in user) { | ||
this.username = user.username; | ||
} else if (typeof user.username !== "string") { | ||
// @ts-ignore | ||
this.username = null; | ||
} | ||
|
||
if ("memberPlus" in user) { | ||
this.memberPlus = user.memberPlus; | ||
} else if (typeof user.memberPlus !== "boolean") { | ||
this.memberPlus = false; | ||
} | ||
|
||
if ("icon" in user) { | ||
this.icon = user.icon; | ||
} else if (typeof user.icon !== "string") { | ||
this.icon = "/assets/img/pfp.png"; | ||
} | ||
|
||
if ("banned" in user) { | ||
this.banned = user.banned; | ||
} else if (typeof user.banned !== "boolean") { | ||
this.banned = false; | ||
} | ||
|
||
return user; | ||
} | ||
|
||
/** | ||
* Revalidate the user | ||
* @example user.revalidate().then(console.log); | ||
* | ||
*/ | ||
public async revalidate(): Promise<User> { | ||
const user = this._clone(); | ||
const data = await this.client.users.get(user.username); | ||
user._patch(data); | ||
return user; | ||
} | ||
|
||
/** | ||
* Concatonate the username insted of the object | ||
*/ | ||
public toString() { | ||
return this.username; | ||
} | ||
|
||
/** | ||
* Convert the user to a JSON object | ||
*/ | ||
public toJSON() { | ||
return super.toJSON(); | ||
} | ||
} | ||
|
||
export interface User { | ||
username: string; | ||
icon: string; | ||
memberPlus: boolean; | ||
banned: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export { Document } from "./Document"; | ||
export { User } from "./User"; | ||
export { Settings } from "./Document/Settings"; | ||
export { Timestamps } from "./Document/Timestamps"; |