mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +01:00
feat(Widget): wrapper for widget.json (#5619)
Co-authored-by: monbrey <rsm999@uowmail.edu.au> Co-authored-by: BannerBomb <BannerBomb55@gmail.com> Co-authored-by: SpaceEEC <spaceeec@yahoo.com> Co-authored-by: Antonio Román <kyradiscord@gmail.com> Co-authored-by: Noel <icrawltogo@gmail.com>
This commit is contained in:
81
src/structures/Widget.js
Normal file
81
src/structures/Widget.js
Normal file
@@ -0,0 +1,81 @@
|
||||
'use strict';
|
||||
|
||||
const Base = require('./Base');
|
||||
const WidgetMember = require('./WidgetMember');
|
||||
const Collection = require('../util/Collection');
|
||||
|
||||
/**
|
||||
* Represents a Widget.
|
||||
*/
|
||||
class Widget extends Base {
|
||||
/**
|
||||
* @param {Client} client The instantiating client
|
||||
* @param {Object} data The raw data
|
||||
*/
|
||||
constructor(client, data) {
|
||||
super(client);
|
||||
this._patch(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the widget with the provided data.
|
||||
* @param {*} data The raw data of the widget
|
||||
* @private
|
||||
*/
|
||||
_patch(data) {
|
||||
/**
|
||||
* The id of the guild.
|
||||
* @type {Snowflake}
|
||||
*/
|
||||
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<Snowflake, WidgetChannel>}
|
||||
*/
|
||||
this.channels = new Collection();
|
||||
for (const channel of data.channels) {
|
||||
this.channels.set(channel.id, channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* The list of members in the guild.
|
||||
* These strings are just arbitrary numbers, they aren't Snowflakes.
|
||||
* @type {Collection<string, WidgetMember>}
|
||||
*/
|
||||
this.members = new Collection();
|
||||
for (const member of data.members) {
|
||||
this.members.set(member.id, new WidgetMember(this.client, member));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
this._patch(data);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Widget;
|
||||
Reference in New Issue
Block a user