-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Entitlements, SKUs, and Subscriptions parts, repositories, events, an…
…d handlers
- Loading branch information
1 parent
e584be6
commit 657c709
Showing
16 changed files
with
884 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,156 @@ | ||
<?php | ||
|
||
/* | ||
* This file is a part of the DiscordPHP project. | ||
* | ||
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com> | ||
* | ||
* This file is subject to the MIT license that is bundled | ||
* with this source code in the LICENSE.md file. | ||
*/ | ||
|
||
namespace Discord\Parts; | ||
|
||
use Carbon\Carbon; | ||
use Discord\Http\Endpoint; | ||
use Discord\Parts\Part; | ||
use Discord\Parts\User\User; | ||
use Discord\Parts\Guild\Guild; | ||
use React\Promise\ExtendedPromiseInterface; | ||
|
||
/** | ||
* An entitlement object represents a premium offering in the application that a user or guild has access to. | ||
* | ||
* @link https://discord.com/developers/docs/resources/entitlement | ||
* | ||
* @since 10.0.0 | ||
* | ||
* @property string $id ID of the entitlement. | ||
* @property string $sku_id ID of the SKU. | ||
* @property string $application_id ID of the parent application. | ||
* @property string|null $user_id ID of the user that is granted access to the entitlement's SKU. | ||
* @property int $type Type of entitlement. | ||
* @property bool $deleted Entitlement was deleted. | ||
* @property Carbon|null $starts_at Start date at which the entitlement is valid. | ||
* @property Carbon|null $ends_at Date at which the entitlement is no longer valid. | ||
* @property string|null $guild_id ID of the guild that is granted access to the entitlement's SKU. | ||
* @property bool|null $consumed For consumable items, whether or not the entitlement has been consumed. | ||
* @property-read Guild|null $guild The guild that is granted access to the entitlement's SKU. | ||
* @property-read User|null $user The user that is granted access to the entitlement's SKU. | ||
*/ | ||
class Entitlement extends Part | ||
{ | ||
public const OWNER_TYPE_GUILD = 1; | ||
public const OWNER_TYPE_USER = 2; | ||
|
||
public const TYPE_PURCHASE = 1; | ||
public const TYPE_PREMIUM_SUBSCRIPTION = 2; | ||
public const TYPE_DEVELOPER_GIFT = 3; | ||
public const TYPE_TEST_MODE_PURCHASE = 4; | ||
public const TYPE_FREE_PURCHASE = 5; | ||
public const TYPE_USER_GIFT = 6; | ||
public const TYPE_PREMIUM_PURCHASE = 7; | ||
public const TYPE_APPLICATION_SUBSCRIPTION = 8; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $fillable = [ | ||
'id', | ||
'sku_id', | ||
'application_id', | ||
'user_id', | ||
'type', | ||
'deleted', | ||
'starts_at', | ||
'ends_at', | ||
'guild_id', | ||
'consumed', | ||
|
||
// @internal | ||
'guild', | ||
'user', | ||
]; | ||
|
||
/** | ||
* Returns the guild attribute that is granted access to the entitlement's sku. | ||
* | ||
* @return Guild|null | ||
*/ | ||
protected function getGuildAttribute(): ?Guild | ||
{ | ||
return $this->discord->guilds->get('id', $this->guild_id); | ||
} | ||
|
||
/** | ||
* Gets the user that is granted access to the entitlement's sku. | ||
* | ||
* @return User|null | ||
*/ | ||
protected function getUserAttribute(): ?User | ||
{ | ||
return $this->discord->users->get('id', $this->user_id); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getRepositoryAttributes(): array | ||
{ | ||
return [ | ||
'application_id' => $this->application_id, | ||
'entitlement_id' => $this->id, | ||
]; | ||
} | ||
|
||
/** | ||
* Returns the starts at attribute. | ||
* | ||
* @return Carbon|null The time that the invite was created. | ||
* | ||
* @throws \Exception | ||
*/ | ||
protected function getStartsAtAttribute(): ?Carbon | ||
{ | ||
if (! isset($this->attributes['starts_at'])) { | ||
return null; | ||
} | ||
|
||
return new Carbon($this->attributes['starts_at']); | ||
} | ||
|
||
/** | ||
* Returns the ends at attribute. | ||
* | ||
* @return Carbon|null The time that the invite was created. | ||
* | ||
* @throws \Exception | ||
*/ | ||
protected function getEndsAtAttribute(): ?Carbon | ||
{ | ||
if (! isset($this->attributes['ends_at'])) { | ||
return null; | ||
} | ||
|
||
return new Carbon($this->attributes['ends_at']); | ||
} | ||
|
||
/** | ||
* Consumes an entitlement. | ||
* | ||
* For One-Time Purchase consumable SKUs, marks a given entitlement for the user as consumed. | ||
* The entitlement will have consumed: true when using List Entitlements. | ||
* | ||
* @param Entitlement $part | ||
* | ||
* @return ExtendedPromiseInterface A promise that resolves to an Entitlement object. | ||
*/ | ||
public function consume(Entitlement $part): ExtendedPromiseInterface | ||
{ | ||
return $this->http->post(Endpoint::bind(Endpoint::APPLICATION_ENTITLEMENT_CONSUME, $this->application_id, $part->id)) | ||
->then(function ($response) use ($part) { // Returns a 204 No Content on success. | ||
$part->consumed = true; | ||
return $part; | ||
}); | ||
} | ||
} |
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,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is a part of the DiscordPHP project. | ||
* | ||
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com> | ||
* | ||
* This file is subject to the MIT license that is bundled | ||
* with this source code in the LICENSE.md file. | ||
*/ | ||
|
||
namespace Discord\Parts\SKUs; | ||
|
||
use Discord\Parts\Part; | ||
|
||
/** | ||
* An SKU object represents a premium offering in the application that a user or guild can purchase. | ||
* | ||
* @link https://discord.com/developers/docs/resources/sku | ||
* | ||
* @since 10.0.0 | ||
* | ||
* @property string $id ID of the SKU. | ||
* @property int $type Type of SKU. | ||
* @property string $application_id ID of the parent application. | ||
* @property string $name Customer-facing name of the premium offering. | ||
* @property string $slug System-generated URL slug based on the SKU's name. | ||
* @property int $flags SKU flags combined as a bitfield. | ||
*/ | ||
class SKU extends Part | ||
{ | ||
public const TYPE_DURABLE = 2; | ||
public const TYPE_CONSUMABLE = 3; | ||
public const TYPE_SUBSCRIPTION = 5; | ||
public const TYPE_SUBSCRIPTION_GROUP = 6; | ||
|
||
public const FLAG_AVAILABLE = 1 << 2; | ||
public const FLAG_GUILD_SUBSCRIPTION = 1 << 7; | ||
public const FLAG_USER_SUBSCRIPTION = 1 << 8; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $fillable = [ | ||
'id', | ||
'type', | ||
'application_id', | ||
'name', | ||
'slug', | ||
'flags', | ||
]; | ||
|
||
/** | ||
* Checks if the SKU is available for purchase. | ||
* | ||
* @return bool | ||
*/ | ||
public function isAvailable(): bool | ||
{ | ||
return ($this->flags & self::FLAG_AVAILABLE) === self::FLAG_AVAILABLE; | ||
} | ||
|
||
/** | ||
* Checks if the SKU is a guild subscription. | ||
* | ||
* @return bool | ||
*/ | ||
public function isGuildSubscription(): bool | ||
{ | ||
return ($this->flags & self::FLAG_GUILD_SUBSCRIPTION) === self::FLAG_GUILD_SUBSCRIPTION; | ||
} | ||
|
||
/** | ||
* Checks if the SKU is a user subscription. | ||
* | ||
* @return bool | ||
*/ | ||
public function isUserSubscription(): bool | ||
{ | ||
return ($this->flags & self::FLAG_USER_SUBSCRIPTION) === self::FLAG_USER_SUBSCRIPTION; | ||
} | ||
} |
Oops, something went wrong.