Skip to content

Commit

Permalink
✨ Soundboard Events
Browse files Browse the repository at this point in the history
  • Loading branch information
valzargaming committed Sep 29, 2024
1 parent e584be6 commit 1dd4760
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/Discord/WebSockets/Events/GuildSoundboardSoundCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\WebSockets\Events;

use Discord\Parts\Guild\Guild;
use Discord\Parts\Guild\Sound;
use Discord\Repository\Guild\SoundRepository;
use Discord\WebSockets\Event;

/**
* @link https://discord.com/developers/docs/topics/gateway-events#guild-soundboard-sound-create
*
* @since 10.0.0
*/
class GuildSoundboardSoundCreate extends Event
{
/**
* {@inheritDoc}
*/
public function handle($data)
{
/** @var Sound */
$part = $this->factory->part(Sound::class, (array) $data, true);

/** @var Guild|null */
$guild = yield $this->discord->guilds->cacheGet($data->guild_id);

if (! $guild instanceof Guild) {
return $part;
}

/** @var SoundRepository */
if ($repository = $guild->sounds) {
$repository->set($part->sound_id, $part);
}

return $part;
}
}
44 changes: 44 additions & 0 deletions src/Discord/WebSockets/Events/GuildSoundboardSoundDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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\WebSockets\Events;

use Discord\Parts\Guild\Guild;
use Discord\Parts\Guild\Sound;
use Discord\WebSockets\Event;

/**
* @link https://discord.com/developers/docs/topics/gateway-events#guild-soundboard-sound-delete
*
* @since 10.0.0
*/
class GuildSoundboardSoundDelete extends Event
{
/**
* {@inheritDoc}
*/
public function handle($data)
{
$part = null;

/** @var ?Guild */
if ($guild = yield $this->discord->guilds->cacheGet($data->guild_id)) {
/** @var ?Sound */
$part = yield $guild->soundboard_sounds->cachePull($data->sound_id);
if ($part instanceof Sound) {
$part->fill((array) $data);
$part->created = false;
}
}

return $part ?? $this->factory->part(Sound::class, (array) $data);
}
}
57 changes: 57 additions & 0 deletions src/Discord/WebSockets/Events/GuildSoundboardSoundUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\WebSockets\Events;

use Discord\Parts\Guild\Guild;
use Discord\Parts\Guild\Sound;
use Discord\WebSockets\Event;

/**
* @link https://discord.com/developers/docs/topics/gateway-events#guild-soundboard-sound-update
*
* @since 10.0.0
*/
class GuildSoundboardSoundUpdate extends Event
{
/**
* {@inheritDoc}
*/
public function handle($data)
{
$newPart = $oldPart = null;

/** @var Guild|null */
$guild = yield $this->discord->guilds->cacheGet($data->guild_id);
if (! $guild instanceof Guild) {
/** @var Sound */
$newPart = $this->factory->part(Sound::class, (array) $data, true);
return [$newPart, $oldPart];
}

/** @var ?Sound */
$oldPart = yield $guild->sounds->cacheGet($data->sound_id);
if ($oldPart instanceof Sound) {
$newPart = clone $oldPart;
$newPart->fill((array) $data);
} else {
/** @var Sound */
$newPart = $this->factory->part(Sound::class, (array) $data, true);
}

/** @var SoundRepository */
if ($repository = $guild->sounds) {
$repository->set($newPart->sound_id, $newPart);
}

return [$newPart, $oldPart];
}
}
49 changes: 49 additions & 0 deletions src/Discord/WebSockets/Events/GuildSoundboardSoundsUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\WebSockets\Events;

use Discord\Parts\Guild\Guild;
use Discord\Parts\Guild\Sound;
use Discord\Repository\Guild\SoundRepository;
use Discord\WebSockets\Event;

/**
* @link https://discord.com/developers/docs/topics/gateway-events#guild-soundboard-sounds-update
*
* @since 10.0.0
*/
class GuildSoundboardSoundsUpdate extends Event
{
/**
* {@inheritDoc}
*/
public function handle($data)
{
/** @var Guild|null */
$guild = yield $this->discord->guilds->cacheGet($data->guild_id);

if (! $guild instanceof Guild) {
return null;
}

/** @var SoundRepository */
$repository = $guild->sounds;

foreach ($data as $soundData) {
/** @var SoundboardSound */
$part = $this->factory->part(Sound::class, (array) $soundData, true);
$repository->set($part->sound_id, $part);
}

return $repository;
}
}
49 changes: 49 additions & 0 deletions src/Discord/WebSockets/Events/SoundboardSounds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\WebSockets\Events;

use Discord\Parts\Guild\Guild;
use Discord\Parts\Guild\Sound;
use Discord\Repository\Guild\SoundRepository;
use Discord\WebSockets\Event;

/**
* @link https://discord.com/developers/docs/topics/gateway-events#soundboard-sounds
*
* @since 10.0.0
*/
class SoundboardSounds extends Event
{
/**
* {@inheritDoc}
*/
public function handle($data)
{
/** @var Guild|null */
$guild = yield $this->discord->guilds->cacheGet($data->guild_id);

if (! $guild instanceof Guild) {
return null;
}

/** @var SoundRepository */
$repository = $guild->sounds;

foreach ($data->soundboard_sounds as $soundData) {
/** @var Sound */
$part = $this->factory->part(Sound::class, (array) $soundData, true);
$repository->set($part->sound_id, $part);
}

return $repository;
}
}

0 comments on commit 1dd4760

Please sign in to comment.