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(Widget): wrapper for widget.json #5619

Merged
merged 30 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
50100f9
add(Widget): wrapper for widget.json
Thumuss May 13, 2021
3b79d53
Use Async
Thumuss May 13, 2021
44d8239
fix: Create an error when widget is disabled
Thumuss May 13, 2021
a523414
fix: throw directly errors (async)
Thumuss May 13, 2021
9a70ef4
fix: change jsdoc + camelCase
Thumuss May 13, 2021
64cbb65
add(Widget#fetch): added to update the widget
Thumuss May 13, 2021
9ec7170
fix: .d.ts with Promise<Widget> in Widget#fetch
Thumuss May 13, 2021
cde478d
Update typings/index.d.ts
Thumuss May 27, 2021
490e52e
Update typings/index.d.ts
Thumuss May 27, 2021
3ead9e6
test
Thumuss May 27, 2021
d3e90ba
Merge branch 'master' of https://github.com/ThumusLive/feature-widget
Thumuss May 27, 2021
c9f6860
Probleme resolved
Thumuss May 27, 2021
ee4e737
String to Snowflake
Thumuss May 27, 2021
96faa34
Update typings/index.d.ts
Thumuss May 27, 2021
7218a7b
Update typings/index.d.ts
Thumuss May 27, 2021
5e33c7d
String to Snowflake
Thumuss May 27, 2021
e5fba6c
String to Snowflake
Thumuss May 27, 2021
a65d340
Delete check data.code => Gives nothing
Thumuss Jun 6, 2021
250eb88
Delete check data.code => Gives nothing
Thumuss Jun 6, 2021
d22da62
Add documentation to Widget#members
Thumuss Jun 6, 2021
6b029fc
Try to resolve the conflict
Thumuss Jun 6, 2021
ab311d7
Add own structure for WidgetMember
Thumuss Jun 7, 2021
b346322
Forgot client in the function
Thumuss Jun 7, 2021
47a6f9f
avatarUrl => avatarURL
Thumuss Jun 8, 2021
cf8ed85
Typedef updated for client & WidgetMember added & Widget#instantInvit…
Thumuss Jun 8, 2021
0911dd8
Add WidgetActivity doc in WidgetMember
Thumuss Jun 9, 2021
11cc73f
Update src/index.js
iCrawl Jun 9, 2021
e4167a4
docs: format lines for consistency
kyranet Jun 9, 2021
e162ece
Fixed docstring + string=>number for WidgetMember#id
Thumuss Jun 9, 2021
41c797d
Revert parseInt
Thumuss Jun 14, 2021
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
3 changes: 2 additions & 1 deletion esm/discord.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,6 @@ export const {
VoiceRegion,
VoiceState,
Webhook,
WebSocket
WebSocket,
Widget
} = Discord;
14 changes: 14 additions & 0 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const GuildTemplate = require('../structures/GuildTemplate');
const Invite = require('../structures/Invite');
const VoiceRegion = require('../structures/VoiceRegion');
const Webhook = require('../structures/Webhook');
const Widget = require('../structures/Widget');
const Collection = require('../util/Collection');
const { Events, DefaultOptions, InviteScopes } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
Expand Down Expand Up @@ -365,6 +366,19 @@ class Client extends BaseClient {
.then(data => new GuildPreview(this, data));
}

/**
* Obtains the widget of a guild from Discord, available for guilds with the widget enabled.
* @param {GuildResolvable} guild The guild to fetch the widget for
* @returns {Promise<Widget>}
*/
async fetchWidget(guild) {
const id = this.guilds.resolveID(guild);
if (!id) throw new TypeError('INVALID_TYPE', 'guild', 'GuildResolvable');
const data = await this.api.guilds(id, 'widget.json').get();
if (data.code) throw new Error('WIDGET_DISABLED');
Thumuss marked this conversation as resolved.
Show resolved Hide resolved
return new Widget(this, data);
}

/**
* Options for {@link Client#generateInvite}.
* @typedef {Object} InviteGenerationOptions
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ module.exports = {
VoiceRegion: require('./structures/VoiceRegion'),
VoiceState: require('./structures/VoiceState'),
Webhook: require('./structures/Webhook'),
Widget: require('./structures/Widget'),
iCrawl marked this conversation as resolved.
Show resolved Hide resolved

WebSocket: require('./WebSocket'),
};
79 changes: 79 additions & 0 deletions src/structures/Widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict';

const Base = require('./Base');
const Collection = require('../util/Collection');

/**
* Represents a Widget.
*/
class Widget extends Base {
/**
* @param {Client} client - The instantiating client
kyranet marked this conversation as resolved.
Show resolved Hide resolved
* @param {Object} data - The raw data
kyranet marked this conversation as resolved.
Show resolved Hide resolved
*/
constructor(client, data) {
super(client);
this._patch(data);
}
/**
kyranet marked this conversation as resolved.
Show resolved Hide resolved
* Builds the widget with the provided data.
* @param {*} data The raw data of the widget
* @private
*/
_patch(data) {
/**
* The id of the guild.
* @type {string}
Thumuss marked this conversation as resolved.
Show resolved Hide resolved
*/
this.id = data.id;

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

/**
* The invite of the guild.
* @type {?string}
*/
this.instantInvite = data.instant_invite;

/**
* The list of channels in the guild.
* @type {Collection<string, WidgetChannel>}
Thumuss marked this conversation as resolved.
Show resolved Hide resolved
*/
this.channels = new Collection();
for (const channel of data.channels) {
this.channels.set(channel.id, channel);
}

/**
* The list of members in the guild.
SpaceEEC marked this conversation as resolved.
Show resolved Hide resolved
* @type {Collection<string, WidgetMember>}
SpaceEEC marked this conversation as resolved.
Show resolved Hide resolved
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
*/
this.members = new Collection();
for (const member of data.members) {
this.members.set(member.id, member);
kyranet marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* The number of the members online.
* @type {number}
*/
this.presenceCount = data.presence_count;
}

/**
* Update the Widget.
* @returns {Promise<Widget>}
*/
async fetch() {
const data = await this.client.api.guilds(this.id, 'widget.json').get();
if (data.code) throw new Error('WIDGET_DISABLED');
Thumuss marked this conversation as resolved.
Show resolved Hide resolved
this._patch(data);
return this;
}
}

module.exports = Widget;
35 changes: 35 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,17 @@ declare module 'discord.js' {
public once(event: string, listener: (...args: any[]) => void): this;
}

export class Widget extends Base {
constructor(client: Client, data: object);
private _patch(data: object): void;
public fetch(): Promise<Widget>;
public id: string;
Thumuss marked this conversation as resolved.
Show resolved Hide resolved
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
public instant_invite: string | null;
Thumuss marked this conversation as resolved.
Show resolved Hide resolved
public channels: Collection<string, WidgetChannel>;
Thumuss marked this conversation as resolved.
Show resolved Hide resolved
public members: Collection<string, WidgetMember>;
SpaceEEC marked this conversation as resolved.
Show resolved Hide resolved
kyranet marked this conversation as resolved.
Show resolved Hide resolved
public presence_count: number;
Thumuss marked this conversation as resolved.
Show resolved Hide resolved
}

//#endregion

//#region Collections
Expand Down Expand Up @@ -3661,6 +3672,30 @@ declare module 'discord.js' {
$device?: string;
}

interface WidgetMember {
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
id: string;
SpaceEEC marked this conversation as resolved.
Show resolved Hide resolved
username: string;
discriminator: string;
avatar: null;
status: PresenceStatus;
deaf?: boolean;
mute?: boolean;
self_deaf?: boolean;
self_mute?: boolean;
suppress?: boolean;
channel_id?: string;
Thumuss marked this conversation as resolved.
Show resolved Hide resolved
avatar_url: string;
activity?: {
name: string;
};
}

interface WidgetChannel {
vladfrangu marked this conversation as resolved.
Show resolved Hide resolved
id: string;
Thumuss marked this conversation as resolved.
Show resolved Hide resolved
name: string;
position: number;
}

Thumuss marked this conversation as resolved.
Show resolved Hide resolved
type WSEventType =
| 'READY'
| 'RESUMED'
Expand Down