Added nl.chimpnut.quizbot.attachment.poll polls, added unknown poll parsing

This commit is contained in:
aymm 2021-03-11 15:53:17 +00:00
parent 7eb3f2aad0
commit 734ed57fa3
2 changed files with 50 additions and 20 deletions

View File

@ -511,9 +511,26 @@ class APnutI
} }
} }
public function getAvatar(int $user_id, array $args = []): array public function getAvatar(int $user_id, array $args = []): string
{ {
return $this->get('/users/'.$user_id.'/avatar', $args); //get returns an array with the url at idx 0
return $this->get('/users/'.$user_id.'/avatar', $args)[0];
}
public function getAvatarUrl(
int $user_id,
?int $width = null,
?int $height = null
): string {
//get returns an array with the url at idx 0
$args = [];
if (!empty($width)) {
$args['w'] = $width;
}
if (!empty($height)) {
$args['h'] = $height;
}
return $this->get('/users/'.$user_id.'/avatar', $args)[0];
} }
public function updateAvatar( public function updateAvatar(

View File

@ -25,7 +25,8 @@ class Poll
'general.poll', 'general.poll',
'net.unsweets.beta', 'net.unsweets.beta',
'io.pnut.core.poll', 'io.pnut.core.poll',
'io.broadsword.poll' 'io.broadsword.poll',
'nl.chimpnut.quizbot.attachment.poll'
]; ];
public function __construct(array $data) public function __construct(array $data)
@ -42,6 +43,21 @@ class Poll
$this->token = $val['poll_token']; $this->token = $val['poll_token'];
$this->prompt = $val['prompt']; $this->prompt = $val['prompt'];
} elseif (in_array($data['type'], Poll::$poll_types)) { } elseif (in_array($data['type'], Poll::$poll_types)) {
$this->parsePoll($data);
} elseif (strpos($data['type'], '.poll') !== false) {
// Try parsing unknown types if they *might* be a poll
try {
$this->parsePoll($data);
} catch (\Exception $e) {
throw new NotSupportedPollException($data['type']);
}
} else {
throw new NotSupportedPollException($data['type']);
}
}
private function parsePoll(array $data)
{
$this->created_at = new \DateTime($data['created_at']); $this->created_at = new \DateTime($data['created_at']);
$this->closed_at = new \DateTime($data['closed_at']); $this->closed_at = new \DateTime($data['closed_at']);
$this->id = (int)$data['id']; $this->id = (int)$data['id'];
@ -60,9 +76,6 @@ class Poll
if (!empty($data['source'])) { if (!empty($data['source'])) {
$this->source = new Source($data['source']); $this->source = new Source($data['source']);
} }
} else {
throw new NotSupportedPollException($data['type']);
}
} }
/** /**