feat(GuildMember): Support edit guild Avatar, Banner, Bio

This commit is contained in:
March 7th 2022-08-04 19:26:16 +07:00
parent 710cb59e48
commit 54e1dbb6b0
6 changed files with 85 additions and 12 deletions

View File

@ -197,6 +197,10 @@ const Messages = {
INVALID_REMOTE_AUTH_URL: 'Invalid remote auth URL (https://discord.com/ra/{hash})',
INVALID_URL: url =>
`Invalid URL: ${url}.\nMake sure you are using a valid URL (https://discord.com/oauth2/authorize?...)`,
NITRO_REQUIRED: 'This feature is only available for Nitro users.',
NITRO_BOOST_REQUIRED: feature => `This feature (${feature}) is only available for Nitro Boost users.`,
ONLY_ME: 'This feature is only available for self.',
};
for (const [name, message] of Object.entries(Messages)) register(name, message);

View File

@ -9,6 +9,7 @@ const BaseGuildVoiceChannel = require('../structures/BaseGuildVoiceChannel');
const { GuildMember } = require('../structures/GuildMember');
const { Role } = require('../structures/Role');
const { Events, Opcodes } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const SnowflakeUtil = require('../util/SnowflakeUtil');
/**
@ -236,6 +237,9 @@ class GuildMemberManager extends CachedManager {
* (if they are connected to voice), or `null` if you want to disconnect them from voice
* @property {DateResolvable|null} [communicationDisabledUntil] The date or timestamp
* for the member's communication to be disabled until. Provide `null` to enable communication again.
* @property {?(BufferResolvable|Base64Resolvable)} [avatar] The new guild avatar
* @property {?(BufferResolvable|Base64Resolvable)} [banner] The new guild banner
* @property {?string} [bio] The new guild about me
*/
/**
@ -268,11 +272,22 @@ class GuildMemberManager extends CachedManager {
_data.communication_disabled_until =
_data.communicationDisabledUntil && new Date(_data.communicationDisabledUntil).toISOString();
// Avatar, banner, bio
if (typeof _data.avatar !== 'undefined') {
_data.avatar = await DataResolver.resolveImage(_data.avatar);
}
if (typeof _data.banner !== 'undefined') {
_data.banner = await DataResolver.resolveImage(_data.banner);
}
let endpoint = this.client.api.guilds(this.guild.id);
if (id === this.client.user.id) {
const keys = Object.keys(data);
if (keys.length === 1 && keys[0] === 'nick') endpoint = endpoint.members('@me');
else endpoint = endpoint.members(id);
if (keys.length === 1 && ['nick', 'avatar', 'banner', 'bio'].includes(keys[0])) {
endpoint = endpoint.members('@me');
} else {
endpoint = endpoint.members(id);
}
} else {
endpoint = endpoint.members(id);
}

View File

@ -104,6 +104,8 @@ class ClientUser extends User {
* @typedef {Object} ClientUserEditData
* @property {string} [username] The new username
* @property {?(BufferResolvable|Base64Resolvable)} [avatar] The new avatar
* @property {?(BufferResolvable|Base64Resolvable)} [banner] The new banner
* @property {?string} [bio] The new bio
*/
/**
@ -209,7 +211,7 @@ class ClientUser extends User {
/**
* Set Accent color
* @param {ColorResolvable} color Color to set
* @returns {Promise}
* @returns {Promise<ClientUser>}
*/
setAccentColor(color = null) {
return this.edit({ accent_color: color ? Util.resolveColor(color) : null });
@ -219,7 +221,7 @@ class ClientUser extends User {
* Set discriminator
* @param {User.discriminator} discriminator It is #1234
* @param {string} password The password of the account
* @returns {Promise}
* @returns {Promise<ClientUser>}
*/
setDiscriminator(discriminator, password) {
if (this.nitroType == 'NONE') throw new Error('You must be a Nitro User to change your discriminator.');
@ -234,8 +236,8 @@ class ClientUser extends User {
/**
* Set About me
* @param {string} bio Bio to set
* @returns {Promise}
* @param {string | null} bio Bio to set
* @returns {Promise<ClientUser>}
*/
setAboutMe(bio = null) {
return this.edit({
@ -247,7 +249,7 @@ class ClientUser extends User {
* Change the email
* @param {Email<string>} email Email to change
* @param {string} password Password of the account
* @returns {Promise}
* @returns {Promise<ClientUser>}
*/
setEmail(email, password) {
if (!password && !this.client.password) {
@ -263,7 +265,7 @@ class ClientUser extends User {
* Set new password
* @param {string} oldPassword Old password
* @param {string} newPassword New password to set
* @returns {Promise}
* @returns {Promise<ClientUser>}
*/
setPassword(oldPassword, newPassword) {
if (!oldPassword && !this.client.password) {
@ -279,7 +281,7 @@ class ClientUser extends User {
/**
* Disable account
* @param {string} password Password of the account
* @returns {Promise}
* @returns {Promise<ClientUser>}
*/
async disableAccount(password) {
if (!password && !this.client.password) {
@ -324,7 +326,7 @@ class ClientUser extends User {
/**
* Delete account. Warning: Cannot be changed once used!
* @param {string} password Password of the account
* @returns {Promise}
* @returns {Promise<ClientUser>}
*/
async deleteAccount(password) {
if (!password && !this.client.password) {

View File

@ -347,6 +347,51 @@ class GuildMember extends Base {
return this.edit({ nick }, reason);
}
/**
* Sets the guild avatar of the logged in client.
* @param {?(BufferResolvable|Base64Resolvable)} avatar The new avatar
* @returns {Promise<GuildMember>}
*/
setAvatar(avatar) {
if (this.user.id !== this.client.user.id) {
throw new Error('ONLY_ME');
}
if (this.client.user.nitroType !== 'NITRO_BOOST') {
throw new Error('NITRO_BOOST_REQUIRED', 'avatar');
}
return this.edit({ avatar });
}
/**
* Sets the guild banner of the logged in client.
* @param {?(BufferResolvable|Base64Resolvable)} banner The new banner
* @returns {Promise<GuildMember>}
*/
setBanner(banner) {
if (this.user.id !== this.client.user.id) {
throw new Error('ONLY_ME');
}
if (this.client.user.nitroType !== 'NITRO_BOOST') {
throw new Error('NITRO_BOOST_REQUIRED', 'banner');
}
return this.edit({ banner });
}
/**
* Set Guild About me
* @param {string | null} bio Bio to set
* @returns {Promise<GuildMember>}
*/
setAboutMe(bio = null) {
if (this.user.id !== this.client.user.id) {
throw new Error('ONLY_ME');
}
if (this.client.user.nitroType !== 'NITRO_BOOST') {
throw new Error('NITRO_BOOST_REQUIRED', 'bio');
}
return this.edit({ bio });
}
/**
* Creates a DM channel between the client and this member.
* @param {boolean} [force=false] Whether to skip the cache check and request the API

View File

@ -342,7 +342,6 @@ class Invite extends Base {
captcha_key: captcha,
}
: {},
// Goodjob discord :) Bypass Phone Verification (not captcha .-.)
headers: {
'X-Context-Properties': Buffer.from(JSON.stringify(dataHeader), 'utf8').toString('base64'),
},

10
typings/index.d.ts vendored
View File

@ -900,7 +900,7 @@ export class ClientUser extends User {
public setHypeSquad(type: HypeSquadType): Promise<void>;
public setAccentColor(color: ColorResolvable): Promise<this>;
public setDiscriminator(discriminator: string, password: string): Promise<this>;
public setAboutMe(bio: string): Promise<this>;
public setAboutMe(bio: string | null): Promise<this>;
public setEmail(email: string, password: string): Promise<this>;
public setPassword(oldPassword: string, newPassword: string): Promise<this>;
public disableAccount(password: string): Promise<this>;
@ -1451,6 +1451,9 @@ export class GuildMember extends PartialTextBasedChannel(Base) {
public kick(reason?: string): Promise<GuildMember>;
public permissionsIn(channel: GuildChannelResolvable): Readonly<Permissions>;
public setNickname(nickname: string | null, reason?: string): Promise<GuildMember>;
public setAvatar(avatar: BufferResolvable | Base64Resolvable | null): Promise<GuildMember>;
public setBanner(banner: BufferResolvable | Base64Resolvable | null): Promise<GuildMember>;
public setAboutMe(bio: string | null): Promise<GuildMember>;
public toJSON(): unknown;
public toString(): MemberMention;
public valueOf(): string;
@ -4938,6 +4941,8 @@ export interface ClientPresenceStatusData {
export interface ClientUserEditData {
username?: string;
avatar?: BufferResolvable | Base64Resolvable | null;
banner?: BufferResolvable | Base64Resolvable | null;
bio?: string | null;
}
export interface CloseEvent {
@ -5582,6 +5587,9 @@ export interface GuildMemberEditData {
deaf?: boolean;
channel?: GuildVoiceChannelResolvable | null;
communicationDisabledUntil?: DateResolvable | null;
avatar?: BufferResolvable | Base64Resolvable | null;
banner?: BufferResolvable | Base64Resolvable | null;
bio?: string | null;
}
export type GuildMemberResolvable = GuildMember | UserResolvable;