Skip to content

Commit

Permalink
✨ Soundboards (#1253)
Browse files Browse the repository at this point in the history
  • Loading branch information
valzargaming authored Sep 26, 2024
1 parent d81ec90 commit 3eefd3c
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Discord/Parts/Guild/Guild.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Discord\Repository\Guild\AutoModerationRuleRepository;
use Discord\Repository\Guild\CommandPermissionsRepository;
use Discord\Repository\Guild\GuildCommandRepository;
use Discord\Repository\Guild\SoundRepository;
use Discord\Repository\Guild\StickerRepository;
use Discord\Repository\Guild\ScheduledEventRepository;
use Discord\Repository\Guild\GuildTemplateRepository;
Expand Down Expand Up @@ -144,6 +145,7 @@
* @property CommandPermissionsRepository $command_permissions
* @property IntegrationRepository $integrations
* @property InviteRepository $invites
* @property SoundRepository $sounds
* @property GuildTemplateRepository $templates
*/
class Guild extends Part
Expand Down Expand Up @@ -296,6 +298,7 @@ class Guild extends Part
'command_permissions' => CommandPermissionsRepository::class,
'integrations' => IntegrationRepository::class,
'invites' => InviteRepository::class,
'sounds' => SoundRepository::class,
'templates' => GuildTemplateRepository::class,
];

Expand Down
113 changes: 113 additions & 0 deletions src/Discord/Parts/Guild/Sound.php
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,
];
}
}
3 changes: 3 additions & 0 deletions src/Discord/Parts/User/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Discord\Repository\EmojiRepository;
use Discord\Repository\GuildRepository;
use Discord\Repository\PrivateChannelRepository;
use Discord\Repository\SoundRepository;
use Discord\Repository\UserRepository;
use React\Promise\ExtendedPromiseInterface;

Expand All @@ -44,6 +45,7 @@
* @property EmojiRepository $emojis
* @property GuildRepository $guilds
* @property PrivateChannelRepository $private_channels
* @property SoundRepository $sounds
* @property UserRepository $users
*/
class Client extends Part
Expand Down Expand Up @@ -76,6 +78,7 @@ class Client extends Part
'emojis' => EmojiRepository::class,
'guilds' => GuildRepository::class,
'private_channels' => PrivateChannelRepository::class,
'sounds' => SoundRepository::class,
'users' => UserRepository::class,
];

Expand Down
79 changes: 79 additions & 0 deletions src/Discord/Repository/Guild/SoundRepository.php
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);
}
}
52 changes: 52 additions & 0 deletions src/Discord/Repository/SoundRepository.php
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;
}

0 comments on commit 3eefd3c

Please sign in to comment.