Skip to content

Commit

Permalink
Thread specific events.
Browse files Browse the repository at this point in the history
- Added create/update/delete events & protocol.
- Bumped DiscordPHP to aa3f3bc for another small issue fix.
  • Loading branch information
JaxkDev committed Aug 21, 2023
1 parent 6c4d2c4 commit 274e55a
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"require": {
"php": "^8.1",
"php-64bit": "*",
"team-reflex/discord-php": "dev-master#545f373",
"team-reflex/discord-php": "dev-master#aa3f3bc",
"react/promise": "v2.10.0",
"pocketmine/binaryutils": "0.2.4",
"composer/ca-bundle": "^1.3"
Expand Down
8 changes: 7 additions & 1 deletion src/Communication/NetworkApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
use JaxkDev\DiscordBot\Communication\Packets\Discord\RoleCreate;
use JaxkDev\DiscordBot\Communication\Packets\Discord\RoleDelete;
use JaxkDev\DiscordBot\Communication\Packets\Discord\RoleUpdate;
use JaxkDev\DiscordBot\Communication\Packets\Discord\ThreadCreate;
use JaxkDev\DiscordBot\Communication\Packets\Discord\ThreadDelete;
use JaxkDev\DiscordBot\Communication\Packets\Discord\ThreadUpdate;
use JaxkDev\DiscordBot\Communication\Packets\Discord\VoiceStateUpdate;
use JaxkDev\DiscordBot\Communication\Packets\Discord\WebhooksUpdate;
use JaxkDev\DiscordBot\Communication\Packets\External\Connect;
Expand Down Expand Up @@ -162,7 +165,10 @@ class NetworkApi{
MessageDeleteBulk::SERIALIZE_ID => MessageDeleteBulk::class,
BotUserUpdate::SERIALIZE_ID => BotUserUpdate::class,
WebhooksUpdate::SERIALIZE_ID => WebhooksUpdate::class,
//Reserved 36-39
ThreadCreate::SERIALIZE_ID => ThreadCreate::class,
ThreadDelete::SERIALIZE_ID => ThreadDelete::class,
ThreadUpdate::SERIALIZE_ID => ThreadUpdate::class,
//Reserved 39

//40-83 PMMP->Discord Packets
RequestAddReaction::SERIALIZE_ID => RequestAddReaction::class,
Expand Down
49 changes: 49 additions & 0 deletions src/Communication/Packets/Discord/ThreadCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* DiscordBot, PocketMine-MP Plugin.
*
* Licensed under the Open Software License version 3.0 (OSL-3.0)
* Copyright (C) 2020-present JaxkDev
*
* Twitter :: @JaxkDev
* Discord :: JaxkDev
* Email :: JaxkDev@gmail.com
*/

namespace JaxkDev\DiscordBot\Communication\Packets\Discord;

use JaxkDev\DiscordBot\Communication\BinaryStream;
use JaxkDev\DiscordBot\Communication\Packets\Packet;
use JaxkDev\DiscordBot\Models\Channels\Channel;

class ThreadCreate extends Packet{

public const SERIALIZE_ID = 36;

private Channel $thread;

public function __construct(Channel $thread, ?int $uid = null){
parent::__construct($uid);
$this->thread = $thread;
}

public function getThread(): Channel{
return $this->thread;
}

public function binarySerialize(): BinaryStream{
$stream = new BinaryStream();
$stream->putInt($this->getUID());
$stream->putSerializable($this->thread);
return $stream;
}

public static function fromBinary(BinaryStream $stream): self{
$uid = $stream->getInt();
return new self(
$stream->getSerializable(Channel::class), // thread
$uid
);
}
}
76 changes: 76 additions & 0 deletions src/Communication/Packets/Discord/ThreadDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* DiscordBot, PocketMine-MP Plugin.
*
* Licensed under the Open Software License version 3.0 (OSL-3.0)
* Copyright (C) 2020-present JaxkDev
*
* Twitter :: @JaxkDev
* Discord :: JaxkDev
* Email :: JaxkDev@gmail.com
*/

namespace JaxkDev\DiscordBot\Communication\Packets\Discord;

use JaxkDev\DiscordBot\Communication\BinaryStream;
use JaxkDev\DiscordBot\Communication\Packets\Packet;
use JaxkDev\DiscordBot\Models\Channels\ChannelType;

class ThreadDelete extends Packet{

public const SERIALIZE_ID = 37;

private ChannelType $type;

private string $id;

private string $guild_id;

private string $parent_id;

public function __construct(ChannelType $type, string $id, string $guild_id, string $parent_id, ?int $uid = null){
parent::__construct($uid);
$this->type = $type;
$this->id = $id;
$this->guild_id = $guild_id;
$this->parent_id = $parent_id;
}

public function getType(): ChannelType{
return $this->type;
}

public function getId(): string{
return $this->id;
}

public function getGuildId(): string{
return $this->guild_id;
}

public function getParentId(): string{
return $this->parent_id;
}

public function binarySerialize(): BinaryStream{
$stream = new BinaryStream();
$stream->putInt($this->getUID());
$stream->putByte($this->type->value);
$stream->putString($this->id);
$stream->putString($this->guild_id);
$stream->putString($this->parent_id);
return $stream;
}

public static function fromBinary(BinaryStream $stream): self{
$uid = $stream->getInt();
return new self(
ChannelType::from($stream->getByte()), // type
$stream->getString(), // id
$stream->getString(), // guild_id
$stream->getString(), // parent_id
$uid
);
}
}
49 changes: 49 additions & 0 deletions src/Communication/Packets/Discord/ThreadUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* DiscordBot, PocketMine-MP Plugin.
*
* Licensed under the Open Software License version 3.0 (OSL-3.0)
* Copyright (C) 2020-present JaxkDev
*
* Twitter :: @JaxkDev
* Discord :: JaxkDev
* Email :: JaxkDev@gmail.com
*/

namespace JaxkDev\DiscordBot\Communication\Packets\Discord;

use JaxkDev\DiscordBot\Communication\BinaryStream;
use JaxkDev\DiscordBot\Communication\Packets\Packet;
use JaxkDev\DiscordBot\Models\Channels\Channel;

class ThreadUpdate extends Packet{

public const SERIALIZE_ID = 38;

private Channel $thread;

public function __construct(Channel $thread, ?int $uid = null){
parent::__construct($uid);
$this->thread = $thread;
}

public function getThread(): Channel{
return $this->thread;
}

public function binarySerialize(): BinaryStream{
$stream = new BinaryStream();
$stream->putInt($this->getUID());
$stream->putSerializable($this->thread);
return $stream;
}

public static function fromBinary(BinaryStream $stream): self{
$uid = $stream->getInt();
return new self(
$stream->getSerializable(Channel::class), // thread
$uid
);
}
}
40 changes: 33 additions & 7 deletions src/InternalBot/Handlers/DiscordEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@
use JaxkDev\DiscordBot\Communication\Packets\Discord\RoleCreate as RoleCreatePacket;
use JaxkDev\DiscordBot\Communication\Packets\Discord\RoleDelete as RoleDeletePacket;
use JaxkDev\DiscordBot\Communication\Packets\Discord\RoleUpdate as RoleUpdatePacket;
use JaxkDev\DiscordBot\Communication\Packets\Discord\ThreadCreate;
use JaxkDev\DiscordBot\Communication\Packets\Discord\ThreadDelete;
use JaxkDev\DiscordBot\Communication\Packets\Discord\ThreadUpdate;
use JaxkDev\DiscordBot\Communication\Packets\Discord\VoiceStateUpdate as VoiceStateUpdatePacket;
use JaxkDev\DiscordBot\Communication\Packets\Discord\WebhooksUpdate as WebhooksUpdatePacket;
use JaxkDev\DiscordBot\Communication\ThreadStatus;
use JaxkDev\DiscordBot\InternalBot\Client;
use JaxkDev\DiscordBot\InternalBot\ModelConverter;
use JaxkDev\DiscordBot\Models\Channels\ChannelType;
use JaxkDev\DiscordBot\Models\Presence\ClientStatus;
use JaxkDev\DiscordBot\Models\Presence\Presence;
use JaxkDev\DiscordBot\Models\Presence\Status;
Expand Down Expand Up @@ -103,9 +107,9 @@ public function registerEvents(): void{
$discord->on(DiscordEvent::CHANNEL_DELETE, [$this, "onChannelDelete"]);
$discord->on(DiscordEvent::CHANNEL_PINS_UPDATE, [$this, "onChannelPinsUpdate"]);

$discord->on(DiscordEvent::THREAD_CREATE, [$this, "onChannelCreate"]); //todo
$discord->on(DiscordEvent::THREAD_UPDATE, [$this, "onChannelUpdate"]); //todo
$discord->on(DiscordEvent::THREAD_DELETE, [$this, "onChannelDelete"]); //todo (note object)
$discord->on(DiscordEvent::THREAD_CREATE, [$this, "onThreadCreate"]);
$discord->on(DiscordEvent::THREAD_UPDATE, [$this, "onThreadUpdate"]);
$discord->on(DiscordEvent::THREAD_DELETE, [$this, "onThreadDelete"]);

$discord->on(DiscordEvent::GUILD_ROLE_CREATE, [$this, "onRoleCreate"]);
$discord->on(DiscordEvent::GUILD_ROLE_UPDATE, [$this, "onRoleUpdate"]);
Expand All @@ -114,7 +118,8 @@ public function registerEvents(): void{
$discord->on(DiscordEvent::INVITE_CREATE, [$this, "onInviteCreate"]);
$discord->on(DiscordEvent::INVITE_DELETE, [$this, "onInviteDelete"]);

//$discord->on(DiscordEvent::GUILD_AUDIT_LOG_ENTRY_CREATE, [$this, "onAuditLogEntryCreate"]); //todo
//$discord->on(DiscordEvent::GUILD_AUDIT_LOG_ENTRY_CREATE, [$this, "onAuditLogEntryCreate"]);
//TODO-Next-Minor Decide on the model structure of this bad boy.

$discord->on(DiscordEvent::GUILD_BAN_ADD, [$this, "onBanAdd"]);
$discord->on(DiscordEvent::GUILD_BAN_REMOVE, [$this, "onBanRemove"]);
Expand Down Expand Up @@ -353,17 +358,38 @@ public function onGuildLeave(DiscordGuild|\stdClass $guild, Discord $discord, bo
$this->client->getThread()->writeOutboundData($packet);
}

public function onChannelCreate(DiscordChannel|DiscordThread $channel, Discord $discord): void{
public function onThreadCreate(DiscordThread $thread, Discord $discord): void{
$packet = new ThreadCreate(ModelConverter::genModelChannel($thread));
$this->client->getThread()->writeOutboundData($packet);
}

public function onThreadUpdate(DiscordThread $thread, Discord $discord): void{
$packet = new ThreadUpdate(ModelConverter::genModelChannel($thread));
$this->client->getThread()->writeOutboundData($packet);
}

/** @param DiscordThread|\stdClass $thread {"type": int, "id": string, "guild_id": string, "parent_id": string} */
public function onThreadDelete(DiscordThread|\stdClass $thread, Discord $discord): void{
$t = ChannelType::from($thread->type);
if(!$t->isThread()){
$this->logger->warning("Thread delete event with non-thread type, ignoring. ID: " . $thread->id . " Type: " . $t->name . " (" . $thread->type . ")");
return;
}
$packet = new ThreadDelete($t, $thread->id, $thread->guild_id, $thread->parent_id);
$this->client->getThread()->writeOutboundData($packet);
}

public function onChannelCreate(DiscordChannel $channel, Discord $discord): void{
$packet = new ChannelCreatePacket(ModelConverter::genModelChannel($channel));
$this->client->getThread()->writeOutboundData($packet);
}

public function onChannelUpdate(DiscordChannel|DiscordThread $channel, Discord $discord): void{
public function onChannelUpdate(DiscordChannel $channel, Discord $discord): void{
$packet = new ChannelUpdatePacket(ModelConverter::genModelChannel($channel));
$this->client->getThread()->writeOutboundData($packet);
}

public function onChannelDelete(DiscordChannel|DiscordThread|\stdClass $channel, Discord $discord): void{
public function onChannelDelete(DiscordChannel $channel, Discord $discord): void{
$packet = new ChannelDeletePacket($channel->guild_id, $channel->id);
$this->client->getThread()->writeOutboundData($packet);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Plugin/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,11 @@ public function kickMember(string $guild_id, string $user_id, ?string $reason =
return ApiResolver::create($pk->getUID());
}

/**
/*
* Sends the Message to discord.
*
* @return PromiseInterface Resolves with a Message model.
*/
*
public function sendMessage(Message $message): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
Expand All @@ -852,16 +852,16 @@ public function sendMessage(Message $message): PromiseInterface{
$pk = new RequestSendMessage($message);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
}*/

/**
/* TODO sendMessage
* Send a local file to a text channel.
*
* @param string $file_path Full file path on disk.
* @param string $message Optional text/message to send with the file
* @param string|null $file_name Optional file_name to show in discord, Prefix with 'SPOILER_' to make as spoiler.
* @return PromiseInterface Resolves with a Message model.
*/
*
public function sendFile(string $guild_id, string $channel_id, string $file_path, string $message = "",
string $file_name = null): PromiseInterface{
if(!$this->ready){
Expand All @@ -885,7 +885,7 @@ public function sendFile(string $guild_id, string $channel_id, string $file_path
$pk = new RequestSendFile($guild_id, $channel_id, $file_name, $file_path, $message);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
}*/

/**
* Edit a sent message.
Expand Down
25 changes: 25 additions & 0 deletions src/Plugin/BotCommunicationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
use JaxkDev\DiscordBot\Communication\Packets\Discord\RoleCreate as RoleCreatePacket;
use JaxkDev\DiscordBot\Communication\Packets\Discord\RoleDelete as RoleDeletePacket;
use JaxkDev\DiscordBot\Communication\Packets\Discord\RoleUpdate as RoleUpdatePacket;
use JaxkDev\DiscordBot\Communication\Packets\Discord\ThreadCreate as ThreadCreatePacket;
use JaxkDev\DiscordBot\Communication\Packets\Discord\ThreadDelete as ThreadDeletePacket;
use JaxkDev\DiscordBot\Communication\Packets\Discord\ThreadUpdate as ThreadUpdatePacket;
use JaxkDev\DiscordBot\Communication\Packets\Discord\VoiceStateUpdate as VoiceStateUpdatePacket;
use JaxkDev\DiscordBot\Communication\Packets\Discord\WebhooksUpdate as WebhooksUpdatePacket;
use JaxkDev\DiscordBot\Communication\Packets\Heartbeat as HeartbeatPacket;
Expand Down Expand Up @@ -80,6 +83,9 @@
use JaxkDev\DiscordBot\Plugin\Events\RoleCreated as RoleCreatedEvent;
use JaxkDev\DiscordBot\Plugin\Events\RoleDeleted as RoleDeletedEvent;
use JaxkDev\DiscordBot\Plugin\Events\RoleUpdated as RoleUpdatedEvent;
use JaxkDev\DiscordBot\Plugin\Events\ThreadCreated as ThreadCreatedEvent;
use JaxkDev\DiscordBot\Plugin\Events\ThreadDeleted as ThreadDeletedEvent;
use JaxkDev\DiscordBot\Plugin\Events\ThreadUpdated as ThreadUpdatedEvent;
use JaxkDev\DiscordBot\Plugin\Events\VoiceStateUpdated as VoiceStateUpdatedEvent;
use JaxkDev\DiscordBot\Plugin\Events\WebhooksUpdated as WebhooksUpdatedEvent;
use pocketmine\VersionInfo;
Expand Down Expand Up @@ -123,6 +129,9 @@ public function handle(Packet $packet): void{
elseif($packet instanceof ChannelUpdatePacket) $this->handleChannelUpdate($packet);
elseif($packet instanceof ChannelDeletePacket) $this->handleChannelDelete($packet);
elseif($packet instanceof ChannelPinsUpdatePacket) $this->handleChannelPinsUpdate($packet);
elseif($packet instanceof ThreadCreatePacket) $this->handleThreadCreate($packet);
elseif($packet instanceof ThreadUpdatePacket) $this->handleThreadUpdate($packet);
elseif($packet instanceof ThreadDeletePacket) $this->handleThreadDelete($packet);
elseif($packet instanceof RoleCreatePacket) $this->handleRoleCreate($packet);
elseif($packet instanceof RoleUpdatePacket) $this->handleRoleUpdate($packet);
elseif($packet instanceof RoleDeletePacket) $this->handleRoleDelete($packet);
Expand All @@ -146,11 +155,27 @@ private function handleReady(DiscordReadyPacket $packet): void{
$event = new DiscordReadyEvent($this->plugin, $packet->getBotUser(), $ac, Status::IDLE);
$event->call();

//TODO, Commands here.
//Use single event at ready event to register ALL commands.
//This will stop duplicates reaching discord side.

$this->plugin->getApi()->updateBotPresence($event->getStatus(), $event->getActivity())->otherwise(function(ApiRejection $a){
$this->plugin->getLogger()->logException($a);
});
}

private function handleThreadCreate(ThreadCreatePacket $packet): void{
(new ThreadCreatedEvent($this->plugin, $packet->getThread()))->call();
}

private function handleThreadUpdate(ThreadUpdatePacket $packet): void{
(new ThreadUpdatedEvent($this->plugin, $packet->getThread()))->call();
}

private function handleThreadDelete(ThreadDeletePacket $packet): void{
(new ThreadDeletedEvent($this->plugin, $packet->getType(), $packet->getId(), $packet->getGuildId(), $packet->getParentId()))->call();
}

private function handleWebhooksUpdate(WebhooksUpdatePacket $packet): void{
(new WebhooksUpdatedEvent($this->plugin, $packet->getGuildId(), $packet->getChannelId()))->call();
}
Expand Down
Loading

0 comments on commit 274e55a

Please sign in to comment.