-
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.
- Loading branch information
1 parent
d81ec90
commit 3eefd3c
Showing
5 changed files
with
250 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,113 @@ | ||
<?php | ||
|
||
/* | ||
* This file is a part of the DiscordPHP project. | ||
* | ||
* Copyright (c) 2015-present David Cole <[email protected]> | ||
* | ||
* This file is subject to the MIT license that is bundled | ||
* with this source code in the LICENSE.md file. | ||
*/ | ||
|
||
namespace Discord\Parts\Guild; | ||
|
||
use Discord\Parts\Part; | ||
use Discord\Parts\User\User; | ||
use Stringable; | ||
|
||
/** | ||
* An sound object represents a soundboard sound. | ||
* | ||
* @link https://discord.com/developers/docs/resources/soundboard | ||
* | ||
* @since 10.0.0 | ||
* | ||
* @property string $name The name of this sound. | ||
* @property ?string $sound_id The identifier for this sound. | ||
* @property double $volume The volume of this sound, from 0 to 1. | ||
* @property ?string $emoji_id The identifier for this sound's custom emoji. | ||
* @property ?string $emoji_name The unicode character of this sound's standard emoji. | ||
* @property ?string $guild_id The identifier of the guild this sound is in. | ||
* @property bool $available Whether this sound can be used, may be false due to loss of Server Boosts. | ||
* @property User|null $user The user who created this sound. | ||
* @property-read Guild|null $guild The guild that owns the sound. | ||
*/ | ||
class Sound extends Part implements Stringable | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $fillable = [ | ||
'name', | ||
'sound_id', | ||
'volume', | ||
'emoji_id', | ||
'emoji_name', | ||
'guild_id', | ||
'available', | ||
'user', | ||
]; | ||
|
||
/** | ||
* Returns the guild attribute. | ||
* | ||
* @return Guild|null The guild the sound belongs to. | ||
*/ | ||
protected function getGuildAttribute(): ?Guild | ||
{ | ||
return $this->discord->guilds->get('id', $this->guild_id); | ||
} | ||
|
||
/** | ||
* Gets the user that created the sound. | ||
* | ||
* @return User|null | ||
*/ | ||
protected function getUserAttribute(): ?User | ||
{ | ||
if (! isset($this->attributes['user'])) { | ||
return null; | ||
} | ||
|
||
if ($user = $this->discord->users->get('id', $this->attributes['user']->id)) { | ||
return $user; | ||
} | ||
|
||
return $this->factory->part(User::class, (array) $this->attributes['user'], true); | ||
} | ||
|
||
/** | ||
* Converts the sound to a string. | ||
* | ||
* @return string | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @link https://discord.com/developers/docs/resources/soundboard#modify-guild-soundboard-sound | ||
*/ | ||
public function getUpdatableAttributes(): array | ||
{ | ||
return $this->makeOptionalAttributes([ | ||
'name' => $this->name, | ||
'volume' => $this->volume, | ||
'emoji_id' => $this->emoji_id, | ||
'emoji_name' => $this->emoji_name, | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getRepositoryAttributes(): array | ||
{ | ||
return [ | ||
'sound_id' => $this->sound_id, | ||
]; | ||
} | ||
} |
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,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is a part of the DiscordPHP project. | ||
* | ||
* Copyright (c) 2015-present David Cole <[email protected]> | ||
* | ||
* This file is subject to the MIT license that is bundled | ||
* with this source code in the LICENSE.md file. | ||
*/ | ||
|
||
namespace Discord\Repository\Guild; | ||
|
||
use Discord\Http\Endpoint; | ||
use Discord\Parts\Guild\Sound; | ||
use Discord\Repository\AbstractRepository; | ||
use React\Promise\ExtendedPromiseInterface; | ||
|
||
use function React\Promise\resolve; | ||
|
||
/** | ||
* Contains sounds of a guild. | ||
* | ||
* @see Sound | ||
* @see \Discord\Parts\Guild\Guild | ||
* | ||
* @since 10.0.0 | ||
* | ||
* @method Sound|null get(string $discrim, $key) | ||
* @method Sound|null pull(string|int $key, $default = null) | ||
* @method Sound|null first() | ||
* @method Sound|null last() | ||
* @method Sound|null find(callable $callback) | ||
*/ | ||
class SoundRepository extends AbstractRepository | ||
{ | ||
/** | ||
* The discriminator. | ||
* | ||
* @var string Discriminator. | ||
*/ | ||
protected $discrim = 'sound_id'; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $endpoints = [ | ||
'all' => Endpoint::GUILD_SOUNDBOARD_SOUNDS, | ||
'get' => Endpoint::GUILD_SOUNDBOARD_SOUND, | ||
'create' => Endpoint::GUILD_SOUNDBOARD_SOUNDS, | ||
'delete' => Endpoint::GUILD_SOUNDBOARD_SOUND, | ||
'update' => Endpoint::GUILD_SOUNDBOARD_SOUND, | ||
]; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $class = Sound::class; | ||
|
||
/** | ||
* @param object $response | ||
* | ||
* @return ExtendedPromiseInterface<static> | ||
*/ | ||
protected function cacheFreshen($response): ExtendedPromiseInterface | ||
{ | ||
foreach ($response as $value) foreach ($value as $value) { | ||
$value = array_merge($this->vars, (array) $value); | ||
$part = $this->factory->create($this->class, $value, true); | ||
$items[$part->{$this->discrim}] = $part; | ||
} | ||
|
||
if (empty($items)) { | ||
return resolve($this); | ||
} | ||
|
||
return $this->cache->setMultiple($items)->then(fn ($success) => $this); | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is a part of the DiscordPHP project. | ||
* | ||
* Copyright (c) 2015-present David Cole <[email protected]> | ||
* | ||
* This file is subject to the MIT license that is bundled | ||
* with this source code in the LICENSE.md file. | ||
*/ | ||
|
||
namespace Discord\Repository; | ||
|
||
use Discord\Http\Endpoint; | ||
use Discord\Parts\Guild\Sound; | ||
use Discord\Repository\AbstractRepository; | ||
|
||
/** | ||
* Contains sounds of a guild. | ||
* | ||
* @see Sound | ||
* @see \Discord\Parts\Guild\Guild | ||
* | ||
* @since 10.0.0 | ||
* | ||
* @method Sound|null get(string $discrim, $key) | ||
* @method Sound|null pull(string|int $key, $default = null) | ||
* @method Sound|null first() | ||
* @method Sound|null last() | ||
* @method Sound|null find(callable $callback) | ||
*/ | ||
class SoundRepository extends AbstractRepository | ||
{ | ||
/** | ||
* The discriminator. | ||
* | ||
* @var string Discriminator. | ||
*/ | ||
protected $discrim = 'sound_id'; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $endpoints = [ | ||
'all' => Endpoint::SOUNDBOARD_DEFAULT_SOUNDS, | ||
]; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $class = Sound::class; | ||
} |