-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict'; | ||
|
||
const { Collection } = require('@discordjs/collection'); | ||
const BaseManager = require('./BaseManager'); | ||
const GuildBoost = require('../structures/GuildBoost'); | ||
|
||
/** | ||
* Manages the API methods of a data model. | ||
* @extends {CachedManager} | ||
*/ | ||
class BillingManager extends BaseManager { | ||
constructor(client) { | ||
super(client); | ||
/** | ||
* All the payment sources of the client | ||
* @type {Collection<Snowflake, Object>} | ||
*/ | ||
this.paymentSources = new Collection(); | ||
/** | ||
* All the guild boosts of the client | ||
* @type {Collection<Snowflake, GuildBoost>} | ||
*/ | ||
this.guildBoosts = new Collection(); | ||
/** | ||
* The current subscription of the client | ||
* @type {Collection<Snowflake, Object>} | ||
*/ | ||
this.currentSubscription = new Collection(); | ||
} | ||
|
||
/** | ||
* Fetches all the payment sources of the client | ||
* @returns {Collection<Snowflake, Object>} | ||
*/ | ||
async fetchPaymentSources() { | ||
// https://discord.com/api/v9/users/@me/billing/payment-sources | ||
const d = await this.client.api.users('@me').billing['payment-sources'].get(); | ||
// ! TODO: Create a PaymentSource class | ||
this.paymentSources = new Collection(d.map(s => [s.id, s])); | ||
return this.paymentSources; | ||
} | ||
|
||
/** | ||
* Fetches all the guild boosts of the client | ||
* @returns {Collection<Snowflake, GuildBoost>} | ||
*/ | ||
async fetchGuildBoosts() { | ||
// https://discord.com/api/v9/users/@me/guilds/premium/subscription-slots | ||
const d = await this.client.api.users('@me').guilds.premium['subscription-slots'].get(); | ||
this.guildBoosts = new Collection(d.map(s => [s.id, new GuildBoost(this.client, s)])); | ||
return this.guildBoosts; | ||
} | ||
|
||
/** | ||
* Fetches the current subscription of the client | ||
* @returns {Collection<Snowflake, Object>} | ||
*/ | ||
async fetchCurrentSubscription() { | ||
// https://discord.com/api/v9/users/@me/billing/subscriptions | ||
const d = await this.client.api.users('@me').billing.subscriptions.get(); | ||
this.currentSubscription = new Collection(d.map(s => [s.id, s])); | ||
return this.currentSubscription; | ||
} | ||
} | ||
|
||
module.exports = BillingManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
'use strict'; | ||
|
||
const Base = require('./Base'); | ||
|
||
/** | ||
* Represents a guild boost in a guild on Discord. | ||
* @extends {Base} | ||
*/ | ||
class GuildBoost extends Base { | ||
constructor(client, data) { | ||
super(client); | ||
this._patch(data); | ||
} | ||
|
||
_patch(data) { | ||
if ('id' in data) { | ||
/** | ||
* The id of the guild boost | ||
* @type {Snowflake} | ||
*/ | ||
this.id = data.id; | ||
} | ||
if ('subscription_id' in data) { | ||
/** | ||
* The id of the subscription | ||
* @type {Snowflake} | ||
*/ | ||
this.subscriptionId = data.subscription_id; | ||
} | ||
if (typeof data.premium_guild_subscription === 'object') { | ||
/** | ||
* The premium guild subscription id | ||
* @type {?Snowflake} | ||
*/ | ||
this.premiumGuildSubscriptionId = data.premium_guild_subscription.id; | ||
/** | ||
* Guild id | ||
* @type {?Snowflake} | ||
*/ | ||
this.guildId = data.premium_guild_subscription.guild_id; | ||
/** | ||
* Ended ??? | ||
* @type {?boolean} | ||
*/ | ||
this.ended = data.premium_guild_subscription.ended; | ||
} | ||
if ('canceled' in data) { | ||
/** | ||
* Whether the subscription is canceled | ||
* @type {boolean} | ||
*/ | ||
this.canceled = data.canceled; | ||
} | ||
if ('cooldown_ends_at' in data) { | ||
/** | ||
* The cooldown end date | ||
* @type {Date} | ||
*/ | ||
this.cooldownEndsAt = new Date(data.cooldown_ends_at); | ||
} | ||
} | ||
/** | ||
* The guild of the boost | ||
* @type {?Guild} | ||
* @readonly | ||
*/ | ||
get guilld() { | ||
return this.client.guilds.cache.get(this.guildId); | ||
} | ||
|
||
/** | ||
* Cancel the boost | ||
* @returns {Promise<GuildBoost>} | ||
*/ | ||
async unsubscribe() { | ||
// https://discord.com/api/v9/guilds/:id/premium/subscriptions/:id | ||
if (!this.guildId) throw new Error('BOOST_UNUSED'); | ||
if (!this.premiumGuildSubscriptionId) throw new Error('BOOST_UNCACHED'); | ||
await this.client.api.guilds(this.guildId).premium.subscriptions(this.premiumGuildSubscriptionId).delete(); | ||
this.guildId = null; | ||
this.premiumGuildSubscriptionId = null; | ||
this.ended = null; | ||
return this; | ||
} | ||
|
||
/** | ||
* Use the boost | ||
* @param {GuildResolvable} guild The guild to use the boost on | ||
* @returns {Promise<GuildBoost>} | ||
*/ | ||
async subscribe(guild) { | ||
// https://discord.com/api/v9/guilds/:id/premium/subscriptions | ||
if (this.guildId || this.premiumGuildSubscriptionId) throw new Error('BOOST_USED'); | ||
const id = this.client.guilds.resolveId(guild); | ||
if (!id) throw new Error('UNKNOWN_GUILD'); | ||
const d = await this.client.api.guilds(id).premium.subscriptions.put({ | ||
data: { | ||
user_premium_guild_subscription_slot_ids: [this.id], | ||
}, | ||
}); | ||
this._patch({ | ||
premium_guild_subscription: d, | ||
}); | ||
return this; | ||
} | ||
} | ||
|
||
module.exports = GuildBoost; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters