diff --git a/src/OneBot/V12/Object/Action.php b/src/OneBot/V12/Object/Action.php index 54cc5cd..813d153 100644 --- a/src/OneBot/V12/Object/Action.php +++ b/src/OneBot/V12/Object/Action.php @@ -4,12 +4,10 @@ namespace OneBot\V12\Object; -use ReturnTypeWillChange; - /** * OneBot 12 标准的 Action 请求对象 */ -class Action implements \JsonSerializable, \IteratorAggregate +class Action implements \JsonSerializable, \IteratorAggregate, \Stringable { /** @var string 动作名称 */ public string $action = ''; @@ -20,6 +18,8 @@ class Action implements \JsonSerializable, \IteratorAggregate /** @var mixed 回包消息 */ public $echo; + public ?array $self; + /** * 创建新的动作实例 * @@ -27,11 +27,17 @@ class Action implements \JsonSerializable, \IteratorAggregate * @param array $params 动作参数 * @param mixed $echo */ - public function __construct(string $action, array $params = [], $echo = null) + public function __construct(string $action, array $params = [], $echo = null, ?array $self = null) { $this->action = $action; $this->params = $params; $this->echo = $echo; + $this->self = $self; + } + + public function __toString() + { + return json_encode($this->jsonSerialize(), JSON_UNESCAPED_SLASHES); } /** @@ -39,22 +45,22 @@ public function __construct(string $action, array $params = [], $echo = null) */ public static function fromArray(array $arr): Action { - return new self($arr['action'], $arr['params'] ?? [], $arr['echo'] ?? null); + return new self($arr['action'], $arr['params'] ?? [], $arr['echo'] ?? null, $arr['self'] ?? null); } public function jsonSerialize(): array { - if ($this->echo === null) { - return [ - 'action' => $this->action, - 'params' => $this->params, - ]; - } - return [ + $d = [ 'action' => $this->action, 'params' => $this->params, - 'echo' => $this->echo, ]; + if ($this->echo !== null) { + $d['echo'] = $this->echo; + } + if ($this->self !== null) { + $d['self'] = $this->self; + } + return $d; } /** @@ -63,6 +69,6 @@ public function jsonSerialize(): array #[\ReturnTypeWillChange] public function getIterator(): \ArrayIterator { - return new \ArrayIterator($this); + return new \ArrayIterator($this->jsonSerialize()); } } diff --git a/src/OneBot/V12/Object/OneBotEvent.php b/src/OneBot/V12/Object/OneBotEvent.php index ec33e53..488bdd3 100644 --- a/src/OneBot/V12/Object/OneBotEvent.php +++ b/src/OneBot/V12/Object/OneBotEvent.php @@ -22,6 +22,20 @@ * @method getChannelId() * @method getOperatorId() * @method getMessageId() + * @method setId(string $id) + * @method setType(string $type) + * @method setSelf(array $self) + * @method setDetailType(string $detail_type) + * @method setSubType(string $sub_type) + * @method setTime(float|int $time) + * @method setAltMessage(string $alt_message) + * @method setGroupId(string $group_id) + * @method setUserId(string $user_id) + * @method setGuildId(string $guild_id) + * @method setChannelId(string $channel_id) + * @method setOperatorId(string $operator_id) + * @method setMessageId(string $message_id) + * @method setMessage(array|MessageSegment|string|\Stringable $message) * @property string $id * @property string $type * @property array $self @@ -53,6 +67,14 @@ public function __call(string $name, array $args = []) } return null; } + if (str_starts_with($name, 'set')) { + $key = Utils::camelToSeparator(substr($name, 3)); + if (isset($this->data[$key])) { + $this->data[$key] = $args[0]; + return true; + } + return false; + } throw new \BadMethodCallException('Call to undefined method ' . __CLASS__ . '::' . $name . '()'); } diff --git a/src/OneBot/global_defines.php b/src/OneBot/global_defines.php index 2ecbf59..ad684bb 100644 --- a/src/OneBot/global_defines.php +++ b/src/OneBot/global_defines.php @@ -13,7 +13,7 @@ use ZM\Logger\ConsoleLogger; const ONEBOT_VERSION = '12'; -const ONEBOT_LIBOB_VERSION = '0.4.3'; +const ONEBOT_LIBOB_VERSION = '0.4.4'; const ONEBOT_JSON = 1; const ONEBOT_MSGPACK = 2;