Skip to content

Commit

Permalink
✨ Soundboard Events and Handlers (#1258) (#1259)
Browse files Browse the repository at this point in the history
  • Loading branch information
valzargaming authored Sep 30, 2024
1 parent 5775919 commit ab11832
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Discord/WebSockets/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ abstract class Event

public const GUILD_AUDIT_LOG_ENTRY_CREATE = 'GUILD_AUDIT_LOG_ENTRY_CREATE';

public const GUILD_SOUNDBOARD_SOUND_CREATE = 'GUILD_SOUNDBOARD_SOUND_CREATE';
public const GUILD_SOUNDBOARD_SOUND_UPDATE = 'GUILD_SOUNDBOARD_SOUND_UPDATE';
public const GUILD_SOUNDBOARD_SOUND_DELETE = 'GUILD_SOUNDBOARD_SOUND_DELETE';
public const SOUNDBOARD_SOUNDS = 'SOUNDBOARD_SOUNDS';

// Channel
public const CHANNEL_CREATE = 'CHANNEL_CREATE';
public const CHANNEL_DELETE = 'CHANNEL_DELETE';
Expand Down
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;
}
}
6 changes: 6 additions & 0 deletions src/Discord/WebSockets/Handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public function __construct()
$this->addHandler(Event::AUTO_MODERATION_RULE_DELETE, \Discord\WebSockets\Events\AutoModerationRuleDelete::class);
$this->addHandler(Event::AUTO_MODERATION_ACTION_EXECUTION, \Discord\WebSockets\Events\AutoModerationActionExecution::class);

// Soundboard Event Handlers
$this->addHandler(Event::GUILD_SOUNDBOARD_SOUND_CREATE, \Discord\WebSockets\Events\GuildSoundboardSoundCreate::class);
$this->addHandler(Event::GUILD_SOUNDBOARD_SOUND_UPDATE, \Discord\WebSockets\Events\GuildSoundboardSoundUpdate::class);
$this->addHandler(Event::GUILD_SOUNDBOARD_SOUND_DELETE, \Discord\WebSockets\Events\GuildSoundboardSoundDelete::class);
$this->addHandler(Event::SOUNDBOARD_SOUNDS, \Discord\WebSockets\Events\SoundboardSounds::class);

// Entitlement Event Handlers
$this->addHandler(Event::ENTITLEMENT_CREATE, \Discord\WebSockets\Events\EntitlementCreate::class);
$this->addHandler(Event::ENTITLEMENT_UPDATE, \Discord\WebSockets\Events\EntitlementUpdate::class);
Expand Down

0 comments on commit ab11832

Please sign in to comment.