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

Implement app subscriptions #111

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
21 changes: 21 additions & 0 deletions src/Disqord.Abstractions/Client/DiscordClientBase.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,27 @@ public event AsynchronousEventHandler<ChannelPinsUpdatedEventArgs> ChannelPinsUp
remove => GatewayClient.ChannelPinsUpdated -= value;
}

/// <inheritdoc/>
public event AsynchronousEventHandler<EntitlementCreatedEventArgs> EntitlementCreated
{
add => GatewayClient.EntitlementCreated += value;
remove => GatewayClient.EntitlementCreated -= value;
}

/// <inheritdoc/>
public event AsynchronousEventHandler<EntitlementUpdatedEventArgs> EntitlementUpdated
{
add => GatewayClient.EntitlementUpdated += value;
remove => GatewayClient.EntitlementUpdated -= value;
}

/// <inheritdoc/>
public event AsynchronousEventHandler<EntitlementDeletedEventArgs> EntitlementDeleted
{
add => GatewayClient.EntitlementDeleted += value;
remove => GatewayClient.EntitlementDeleted -= value;
}

/// <inheritdoc/>
public event AsynchronousEventHandler<GuildAvailableEventArgs> GuildAvailable
{
Expand Down
5 changes: 5 additions & 0 deletions src/Disqord.Core/Discord/Limits/Rest/Discord.Limits.Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public static class Rest
/// Represents the page size for fetching event users.
/// </summary>
public const int FetchGuildEventUsersPageSize = 100;

/// <summary>
/// Represents the page size for fetching entitlements.
/// </summary>
public const int FetchEntitlementsPageSize = 100;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Disqord.Models;

namespace Disqord;

/// <summary>
/// Represents an entitlement of a user or a guild to a premium offering.
/// </summary>
public interface IEntitlement: ISnowflakeEntity, IPossiblyGuildEntity, IJsonUpdatable<EntitlementJsonModel>
{
/// <summary>
/// Gets the ID of the SKU this entitlement offers.
/// </summary>
Snowflake SkuId { get; }

/// <summary>
/// Gets the ID of the application this entitlement belongs to.
/// </summary>
Snowflake ApplicationId { get; }

/// <summary>
/// Gets the ID of the user this entitlement is for.
/// </summary>
Snowflake? UserId { get; }

/// <summary>
/// Gets the type of this entitlement.
/// </summary>
EntitlementType Type { get; }

/// <summary>
/// Gets whether this entitlement is deleted.
/// </summary>
bool Deleted { get; }

/// <summary>
/// Gets the date from which the entitlement is valid.
/// </summary>
DateTimeOffset? StartsAt { get; }

/// <summary>
/// Gets the date from which the entitlement is no longer valid.
/// </summary>
DateTimeOffset? EndsAt { get; }
}
29 changes: 29 additions & 0 deletions src/Disqord.Core/Entities/Core/Application/Monetization/ISku.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Disqord.Models;

namespace Disqord;

/// <summary>
/// Represents a Discord SKU (stock-keeping unit) for premium offerings.
/// </summary>
public interface ISku : ISnowflakeEntity, INamableEntity, IJsonUpdatable<SkuJsonModel>
{
/// <summary>
/// Gets the type of this SKU.
/// </summary>
SkuType Type { get; }

/// <summary>
/// Gets the ID of the application this SKU belongs to.
/// </summary>
Snowflake ApplicationId { get; }

/// <summary>
/// Gets the system-generated URL slug of this SKU.
/// </summary>
string Slug { get; }

/// <summary>
/// Gets the flags of this SKU.
/// </summary>
SkuFlags Flags { get; }
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Globalization;

namespace Disqord;
Expand Down Expand Up @@ -50,4 +51,9 @@ public interface IUserInteraction : IInteraction, IPossiblyGuildEntity, IChannel
/// The locale of the guild or <see langword="null"/> if this interaction was triggered in a private channel.
/// </returns>
CultureInfo? GuildLocale { get; }

/// <summary>
/// Gets the entitlements of the user who triggered this interaction.
/// </summary>
IReadOnlyList<IEntitlement> Entitlements { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Disqord.Models;
using Qommon;

namespace Disqord;

/// <inheritdoc cref="IEntitlement"/>
public class TransientEntitlement : TransientClientEntity<EntitlementJsonModel>, IEntitlement
{
/// <inheritdoc/>
public Snowflake Id => Model.Id;

/// <inheritdoc/>
public Snowflake? GuildId => Model.GuildId.GetValueOrDefault();

/// <inheritdoc/>
public Snowflake SkuId => Model.SkuId;

/// <inheritdoc/>
public Snowflake ApplicationId => Model.ApplicationId;

/// <inheritdoc/>
public Snowflake? UserId => Model.UserId.GetValueOrDefault();

/// <inheritdoc/>
public EntitlementType Type => Model.Type;

/// <inheritdoc/>
public bool Deleted => Model.Deleted;

/// <inheritdoc/>
public DateTimeOffset? StartsAt => Model.StartsAt;

/// <inheritdoc/>
public DateTimeOffset? EndsAt => Model.StartsAt;

public TransientEntitlement(IClient client, EntitlementJsonModel model)
: base(client, model)
{ }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Disqord.Models;

namespace Disqord;

/// <inheritdoc cref="ISku"/>
public class TransientSku : TransientClientEntity<SkuJsonModel>, ISku
{
/// <inheritdoc/>
public Snowflake Id => Model.Id;

/// <inheritdoc cref="INamableEntity.Name"/>
public string Name => Model.Name;

/// <inheritdoc/>
public SkuType Type => Model.Type;

/// <inheritdoc/>
public Snowflake ApplicationId => Model.ApplicationId;

/// <inheritdoc/>
public string Slug => Model.Slug;

/// <inheritdoc/>
public SkuFlags Flags => Model.Flags;

public TransientSku(IClient client, SkuJsonModel model)
: base(client, model)
{ }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Globalization;
using System.Collections.Generic;
using System.Globalization;
using Disqord.Models;
using Qommon;
using Qommon.Collections.ReadOnly;

namespace Disqord;

Expand Down Expand Up @@ -69,6 +71,11 @@ public Permissions AuthorPermissions
/// <inheritdoc/>
public CultureInfo? GuildLocale => Optional.ConvertOrDefault(Model.GuildLocale, Discord.Internal.GetLocale);

/// <inheritdoc/>
public IReadOnlyList<IEntitlement> Entitlements => _entitlements ??= Model.Entitlements.ToReadOnlyList(Client, (model, client) => new TransientEntitlement(client, model));

private IReadOnlyList<IEntitlement>? _entitlements;

public TransientUserInteraction(IClient client, long __receivedAt, InteractionJsonModel model)
: base(client, __receivedAt, model)
{ }
Expand Down
10 changes: 10 additions & 0 deletions src/Disqord.Core/Enums/EntitlementOwnerType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Disqord;

/// <summary>
/// Represents the type of the owner of an entitlement.
/// </summary>
public enum EntitlementOwnerType
{
Guild = 1,
User = 2
}
12 changes: 12 additions & 0 deletions src/Disqord.Core/Enums/EntitlementType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Disqord;

/// <summary>
/// Represents the type of a entitlement.
/// </summary>
public enum EntitlementType
{
/// <summary>
/// The entitlement was purchased as an app subscription.
/// </summary>
ApplicationSubscription = 8
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public enum InteractionResponseType

ApplicationCommandAutoComplete = 8,

Modal = 9
}
Modal = 9,

PremiumRequired = 10
}
27 changes: 27 additions & 0 deletions src/Disqord.Core/Enums/SkuFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Disqord;

/// <summary>
/// Represents flags for SKUs.
/// </summary>
[Flags]
public enum SkuFlags
{
/// <summary>
/// The SKU is available for purchase.
/// </summary>
Available = 1 << 2,

/// <summary>
/// A recurring SKU that can be purchased for a single guild.
/// Grants access to every user in the guild.
/// </summary>
GuildSubscription = 1 << 7,

/// <summary>
/// A recurring SKU that can be purchased for a user.
/// Grants access to the purchasing user in every guild.
/// </summary>
UserSubscription = 1 << 8
}
17 changes: 17 additions & 0 deletions src/Disqord.Core/Enums/SkuType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Disqord;

/// <summary>
/// Represents the type of a SKU.
/// </summary>
public enum SkuType
{
/// <summary>
/// Represents a recurring subscription.
/// </summary>
Subscription = 5,

/// <summary>
/// Represents a system-generated group for subscriptions.
/// </summary>
SubscriptionGroup = 6
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Disqord.Serialization.Json;
using Qommon;

namespace Disqord.Models;

public class EntitlementJsonModel : JsonModel
{
[JsonProperty("id")]
public Snowflake Id;

[JsonProperty("type")]
public EntitlementType Type;

[JsonProperty("sku_id")]
public Snowflake SkuId;

[JsonProperty("application_id")]
public Snowflake ApplicationId;

[JsonProperty("user_id")]
public Optional<Snowflake> UserId;

[JsonProperty("deleted")]
public bool Deleted;

[JsonProperty("starts_at")]
public DateTimeOffset? StartsAt;

[JsonProperty("ends_at")]
public DateTimeOffset? EndsAt;

[JsonProperty("guild_id")]
public Optional<Snowflake> GuildId;
}
24 changes: 24 additions & 0 deletions src/Disqord.Core/Models/Application/Monetization/SkuJsonModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Disqord.Serialization.Json;

namespace Disqord.Models;

public class SkuJsonModel : JsonModel
{
[JsonProperty("id")]
public Snowflake Id;

[JsonProperty("type")]
public SkuType Type;

[JsonProperty("application_id")]
public Snowflake ApplicationId;

[JsonProperty("name")]
public string Name = null!;

[JsonProperty("slug")]
public string Slug = null!;

[JsonProperty("flags")]
public SkuFlags Flags;
}
3 changes: 3 additions & 0 deletions src/Disqord.Core/Models/Interactions/InteractionJsonModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ public class InteractionJsonModel : JsonModel

[JsonProperty("guild_locale")]
public Optional<string> GuildLocale;

[JsonProperty("entitlements")]
public EntitlementJsonModel[] Entitlements = null!;
}
Loading
Loading