Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(GuildPreview): implement support for "preview" endpoint #3965

Merged
merged 34 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
75112a3
feat(GuildPreview): method — fetchGuildPreview
kernel-dev Mar 19, 2020
db1629e
feat(GuildPreview): structure — GuildPreview
kernel-dev Mar 19, 2020
0e9ef65
feat(GuildPreview): update typings
kernel-dev Mar 19, 2020
4cefd9a
fix(GuildPreview): remove typedef for Features — already exists
SouSinner Mar 19, 2020
8517b2d
refactor(GuildPreview): update JSDocs & method
SouSinner Mar 19, 2020
645b7fa
feat(GuildPreview): implement DiscoverySplash function
SouSinner Mar 19, 2020
6b14afa
fix(GuildPreview): description & error handling for id
SouSinner Mar 19, 2020
debfb86
fix(GuildPreview): misleading description, assign emojis correctly
SouSinner Mar 19, 2020
ab3822d
feat(GuildPreview): func DisplaySplash & GuildPreviewEmoji interface
SouSinner Mar 19, 2020
da110a0
fix(Typings): satisfy TSLint
SouSinner Mar 19, 2020
7656115
fix(GuildPreview): toJSON - returns a value now
SouSinner Mar 19, 2020
13ab20f
feat(GuildPreview): add fetchPreview method on instance of Guild
SouSinner Mar 19, 2020
8c24e09
feat(GuildPreview): update typings
SouSinner Mar 19, 2020
79ab522
fix: missing client constructor
SouSinner Mar 19, 2020
2d07c03
fix: typo in typings
SouSinner Mar 20, 2020
4639fa7
Merge branch 'master' into master
iCrawl Mar 21, 2020
b11b2be
feat(BaseEmoji): implement BaseEmoji — parent for emoji instances
SouSinner Mar 21, 2020
7381111
feat(BaseEmoji): refactor - GuildEmoji extends BaseEmoji now
SouSinner Mar 21, 2020
2dc9dc1
feat(BaseEmoji): refactor - adjust emojis prop to BaseEmoji instance
SouSinner Mar 21, 2020
f98b432
feat(BaseEmoji): not documented fully - GuildPreviewEmoji
SouSinner Mar 21, 2020
83194b4
feat(BaseEmoji): update typings
SouSinner Mar 21, 2020
3eedf6f
Merge branch 'master' of https://github.com/SouSinner/discord.js
SouSinner Mar 21, 2020
83b8f00
fix: remove duplicate typing properties - inherited
SouSinner Mar 21, 2020
702e613
fix: remove redundant methods & properties - inherited / already set
SouSinner Mar 21, 2020
9174bef
fix: let -> const
SouSinner Mar 21, 2020
2fbfeef
fix: typings - put BaseEmoji after BaseClient
SouSinner Mar 22, 2020
28c7c56
fix: remove _clone method - redundant
SouSinner Mar 22, 2020
ec27d04
Merge branch 'master' into master
iCrawl Mar 23, 2020
f88ed88
refactor(GuildPreview): emojis should be a collection
SpaceEEC Mar 24, 2020
094c21d
refactor: rename base class, move relevant props there and expose roles
SpaceEEC Mar 24, 2020
8ce72cd
fix(GuildPreview): update emojis in _patch
SpaceEEC Mar 27, 2020
24e8a1a
fix(Typings): remove empty line, add Client#fetchGuildPreview
SpaceEEC Mar 27, 2020
1071856
feat: export GuildPreview
SpaceEEC Mar 27, 2020
a47b90b
fix(Typings): add GuildPreview#discoverSplash, icon, and splash
SpaceEEC Mar 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const GuildManager = require('../managers/GuildManager');
const UserManager = require('../managers/UserManager');
const ShardClientUtil = require('../sharding/ShardClientUtil');
const ClientApplication = require('../structures/ClientApplication');
const GuildPreview = require('../structures/GuildPreview');
const Invite = require('../structures/Invite');
const VoiceRegion = require('../structures/VoiceRegion');
const Webhook = require('../structures/Webhook');
Expand Down Expand Up @@ -338,6 +339,19 @@ class Client extends BaseClient {
.then(app => new ClientApplication(this, app));
}

/**
* Tries to fetch the specified guild, if available to the "preview" endpoint.
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
* @param {GuildResolvable} guild The GuildResolvable to resolve
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Promise<GuildPreview>}
*/
fetchGuildPreview(guild) {
const id = this.guilds.resolveID(guild);
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
return this.api
.guilds(id, 'preview')
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
.get()
.then(data => new GuildPreview(this, data));
}

/**
* Generates a link that can be used to invite the bot to a guild.
* @param {PermissionResolvable} [permissions] Permissions to request
Expand Down
154 changes: 154 additions & 0 deletions src/structures/GuildPreview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
'use strict';

const Base = require('./Base');
const GuildEmojiManager = require('../managers/GuildEmojiManager');

/**
* Represents the data about the guild any bot can preivew, connected to the specified public guild
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
*/
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
class GuildPreview extends Base {
constructor(client, data) {
super(client);

if (!data) return;
this._patch(data);
}

/**
* Builds the public guild with the provided data
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
* @param {*} data The raw data of the public guild
* @private
*/
_patch(data) {
/**
* The id of this public guild
* @type {string}
*/
this.id = data.id;

/**
* The name of this public guild
* @type {string}
*/
this.name = data.name;

/**
* The icon of this public guild
* @type {?string}
*/
this.icon = data.icon;

/**
* The splash icon of this public guild
* @type {?string}
*/
this.splash = data.splash;

/**
* The discovery splash icon of this public guild
* @type {?string}
*/
this.discoverySplash = data.discovery_splash;

/**
* An array of enabled guild features
* @type {Features[]}
*/
this.features = data.features;

/**
* The approximate count of members in this public guild
* @type {number}
*/
this.approximateMemberCount = data.approximate_member_count;

/**
* The approximate count of online members in this public guild
* @type {number}
*/
this.approximatePresenceCount = data.approximate_presence_count;

/**
* The description for this public guild
* @type {?string}
*/
this.description = data.description;

if (!this.emojis) {
/**
* A manager of the emojis belonging to this public guild
* @type {GuildEmojiManager}
*/
this.emojis = new GuildEmojiManager(this);
if (data.emojis) for (const emoji of data.emojis) this.emojis.add(emoji);
} else if (data.emojis) {
this.client.actions.GuildemojisUpdate.handle({
guild_id: this.id,
emojis: data.emojis,
});
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* The URL to this public guild's splash
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
* @param {ImageURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
splashURL({ format, size } = {}) {
if (!this.splash) return null;
return this.client.rest.cdn.Splash(this.id, this.splash, format, size);
}

/**
* The URL to this public guild's discovery splash
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
* @param {ImageURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
discoverySplashURL({ format, size } = {}) {
if (!this.discoverySplash) return null;
return this.client.rest.cdn.Splash(this.id, this.discoverySplash, format, size, 'discovery');
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* The URL to this public guild's icon
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
* @param {ImageURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
iconURL({ format, size, dynamic } = {}) {
if (!this.icon) return null;
return this.client.rest.cdn.Icon(this.id, this.icon, format, size, dynamic);
}

/**
* Fetches this public guild.
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Promise<GuildPreview>}
*/
fetch() {
return this.client.api
.guilds(this.id, 'preview')
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
.get()
.then(data => {
this._patch(data);
return this;
});
}

/**
* When concatenated with a string, this automatically returns the guild's name instead of the Guild object.
* @returns {string}
* @example
* // Logs: Hello from My Guild!
* console.log(`Hello from ${previewGuild}!`);
*/
toString() {
return this.name;
}

toJSON() {
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
const json = super.toJSON();
json.iconURL = this.iconURL();
json.splashURL = this.splashURL();
}
}

module.exports = GuildPreview;
76 changes: 38 additions & 38 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,24 @@ declare module 'discord.js' {
public valueOf(): string;
}

export class GuildPreview extends Base {
constructor(client: Client, data: object);
public approximateMemberCount: number;
public approximatePresenceCount: number;
public description?: string;
public emojis: GuildEmojiManager;
public features: GuildFeatures;
public id: string;
public name: string;

public discoverySplashURL(options?: ImageURLOptions): string | null;
public iconURL(options?: ImageURLOptions & { dynamic?: boolean }): string | null;
public splashURL(options?: ImageURLOptions): string | null;
public fetch(): Promise<GuildPreview>;
public toJSON(): object;
public toString(): string;
private _patch(data: object);
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
}
export class HTTPError extends Error {
constructor(message: string, name: string, code: number, method: string, path: string);
public code: number;
Expand Down Expand Up @@ -2771,26 +2789,18 @@ declare module 'discord.js' {
partial: true;
fetch(): Promise<T>;
} & {
[K in keyof Omit<T,
'client' |
'createdAt' |
'createdTimestamp' |
'id' |
'partial' |
'fetch' | O>
// tslint:disable-next-line:ban-types
]: T[K] extends Function ? T[K] : T[K] | null;
[K in keyof Omit<
T,
'client' | 'createdAt' | 'createdTimestamp' | 'id' | 'partial' | 'fetch' | O
>]: // tslint:disable-next-line:ban-types
T[K] extends Function ? T[K] : T[K] | null;
};

interface PartialDMChannel extends Partialize<DMChannel,
'lastMessage' |
'lastMessageID' |
'messages' |
'recipient' |
'type' |
'typing' |
'typingCount'
> {
interface PartialDMChannel
extends Partialize<
DMChannel,
'lastMessage' | 'lastMessageID' | 'messages' | 'recipient' | 'type' | 'typing' | 'typingCount'
> {
lastMessage: null;
lastMessageID: undefined;
messages: MessageManager;
Expand All @@ -2814,16 +2824,11 @@ declare module 'discord.js' {
}[];
}

interface PartialGuildMember extends Partialize<GuildMember,
'bannable' |
'displayColor' |
'displayHexColor' |
'displayName' |
'guild' |
'kickable' |
'permissions' |
'roles'
> {
interface PartialGuildMember
extends Partialize<
GuildMember,
'bannable' | 'displayColor' | 'displayHexColor' | 'displayName' | 'guild' | 'kickable' | 'permissions' | 'roles'
> {
readonly bannable: boolean;
readonly displayColor: number;
readonly displayHexColor: string;
Expand All @@ -2836,16 +2841,11 @@ declare module 'discord.js' {
readonly roles: GuildMember['roles'];
}

interface PartialMessage extends Partialize<Message,
'attachments' |
'channel' |
'deletable' |
'editable' |
'mentions' |
'pinnable' |
'system' |
'url'
> {
interface PartialMessage
extends Partialize<
Message,
'attachments' | 'channel' | 'deletable' | 'editable' | 'mentions' | 'pinnable' | 'system' | 'url'
> {
attachments: Message['attachments'];
channel: Message['channel'];
readonly deletable: boolean;
Expand Down