Skip to content

Commit

Permalink
backport: Guild#{fetchEmbed,setEmbed} (#2778)
Browse files Browse the repository at this point in the history
*  backport: Guild Embeds

* fix: Added missing return

* docs: Updated typings
  • Loading branch information
kyranet authored and Lewdcario committed Aug 28, 2018
1 parent 3345c77 commit 091b4fc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/client/rest/RESTMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class RESTMethods {
});
}

fetchEmbed(guildID) {
return this.rest.makeRequest('get', Endpoints.Guild(guildID).embed, true).then(data => ({
enabled: data.enabled,
channel: data.channel_id ? this.client.channels.get(data.channel_id) : null,
}));
}

sendMessage(channel, content, { tts, nonce, embed, disableEveryone, split, code, reply } = {}, files = null) {
return new Promise((resolve, reject) => { // eslint-disable-line complexity
if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content);
Expand Down Expand Up @@ -853,6 +860,13 @@ class RESTMethods {
);
}

updateEmbed(guildID, embed, reason) {
return this.rest.makeRequest('patch', Endpoints.Guild(guildID).embed, true, {
enabled: embed.enabled,
channel_id: this.client.resolver.resolveChannelID(embed.channel),
}, undefined, reason);
}

setRolePositions(guildID, roles) {
return this.rest.makeRequest('patch', Endpoints.Guild(guildID).roles, true, roles).then(() =>
this.client.actions.GuildRolesPositionUpdate.handle({
Expand Down
31 changes: 31 additions & 0 deletions src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,26 @@ class Guild {
return this.client.rest.methods.fetchVoiceRegions(this.id);
}

/**
* The Guild Embed object
* @typedef {Object} GuildEmbedData
* @property {boolean} enabled Whether the embed is enabled
* @property {?ChannelResolvable} channel The embed channel
*/

/**
* Fetches the guild embed.
* @returns {Promise<GuildEmbedData>}
* @example
* // Fetches the guild embed
* guild.fetchEmbed()
* .then(embed => console.log(`The embed is ${embed.enabled ? 'enabled' : 'disabled'}`))
* .catch(console.error);
*/
fetchEmbed() {
return this.client.rest.methods.fetchEmbed(this.id);
}

/**
* Fetch audit logs for this guild.
* @param {Object} [options={}] Options for fetching audit logs
Expand Down Expand Up @@ -1025,6 +1045,17 @@ class Guild {
return this.client.rest.methods.updateChannelPositions(this.id, channelPositions);
}

/**
* Edits the guild's embed.
* @param {GuildEmbedData} embed The embed for the guild
* @param {string} [reason] Reason for changing the guild's embed
* @returns {Promise<Guild>}
*/
setEmbed(embed, reason) {
return this.client.rest.methods.updateEmbed(this.id, embed, reason)
.then(() => this);
}

/**
* Creates a new role in the guild with given information.
* @param {RoleData} [data] The data to update the role with
Expand Down
7 changes: 7 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ declare module 'discord.js' {
public equals(guild: Guild): boolean;
public fetchAuditLogs(options?: GuildAuditLogsFetchOptions): Promise<GuildAuditLogs>;
public fetchBans(): Promise<Collection<Snowflake, User>>;
public fetchEmbed(): Promise<GuildEmbedData>;
public fetchInvites(): Promise<Collection<Snowflake, Invite>>;
public fetchMember(user: UserResolvable, cache?: boolean): Promise<GuildMember>;
public fetchMembers(query?: string, limit?: number): Promise<Guild>;
Expand All @@ -547,6 +548,7 @@ declare module 'discord.js' {
public setChannelPosition(channel: string | GuildChannel, position: number, relative?: boolean): Promise<Guild>;
public setChannelPositions(channelPositions: ChannelPosition[]): Promise<Guild>;
public setDefaultMessageNotification(defaultMessageNotifications: DefaultMessageNotifications, reason: string): Promise<Guild>;
public setEmbed(embed: GuildEmbedData, reason?: string): Promise<Guild>;
public setExcplicitContentFilter(explicitContentFilter: number, reason?: string): Promise<Guild>;
public setIcon(icon: Base64Resolvable, reason?: string): Promise<Guild>;
public setName(name: string, reason?: string): Promise<Guild>;
Expand Down Expand Up @@ -1787,6 +1789,11 @@ declare module 'discord.js' {
splash?: Base64Resolvable;
};

type GuildEmbedData = {
enabled: boolean;
channel: ChannelResolvable;
};

type GuildMemberEditData = {
nick?: string;
roles?: Collection<Snowflake, Role> | Role[] | Snowflake[];
Expand Down

0 comments on commit 091b4fc

Please # to comment.