Skip to content

Commit

Permalink
Merge pull request #78 from botuniverse/feature/onebot-event-msg-str
Browse files Browse the repository at this point in the history
修复一个 Response 的 Bug,新增 OneBotEvent 对象的方法
  • Loading branch information
crazywhalecc authored Dec 20, 2022
2 parents f3d2905 + 827f077 commit 019a46d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/OneBot/Driver/Event/WebSocket/WebSocketOpenEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class WebSocketOpenEvent extends DriverEvent
{
protected ServerRequestInterface $request;

protected ?ResponseInterface $response;
protected ?ResponseInterface $response = null;

protected int $fd;

Expand Down
23 changes: 23 additions & 0 deletions src/OneBot/V12/Object/OneBotEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public function __call(string $name, array $args = [])
return null;
}
if (str_starts_with($name, 'set')) {
if ($name === 'setMessage') {
$this->message_segment_cache = null;
}
$key = Utils::camelToSeparator(substr($name, 3));
if (isset($this->data[$key])) {
$this->data[$key] = $args[0];
Expand Down Expand Up @@ -132,6 +135,26 @@ public function getMessage(bool $return_assoc_array = false): ?array
return $this->message_segment_cache;
}

/**
* 获取纯文本消息
*/
public function getMessageString(): string
{
$message = $this->getMessage();
if ($message === null) {
return '';
}
$message_string = '';
foreach ($message as $segment) {
if ($segment->type === 'text') {
$message_string .= $segment->data['text'];
} else {
$message_string .= '[富文本:' . $segment->type . ']';
}
}
return $message_string;
}

public function jsonSerialize(): array
{
return $this->data;
Expand Down

0 comments on commit 019a46d

Please sign in to comment.