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,29 +43,41 @@ 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->created_at = new \DateTime($data['created_at']); $this->parsePoll($data);
$this->closed_at = new \DateTime($data['closed_at']); } elseif (strpos($data['type'], '.poll') !== false) {
$this->id = (int)$data['id']; // Try parsing unknown types if they *might* be a poll
$this->is_anonymous = (bool)$data['is_anonymous']; try {
$this->is_public = (bool)$data['is_public']; $this->parsePoll($data);
foreach ($data['options'] as $option) { } catch (\Exception $e) {
$this->options[] = new PollOption($option); throw new NotSupportedPollException($data['type']);
}
if (!empty($data['poll_token'])) {
$this->token = $data['poll_token'];
}
$this->prompt = $data['prompt'];
if (!empty($data['user'])) {
$this->user = new User($data['user']);
}
if (!empty($data['source'])) {
$this->source = new Source($data['source']);
} }
} else { } else {
throw new NotSupportedPollException($data['type']); throw new NotSupportedPollException($data['type']);
} }
} }
private function parsePoll(array $data)
{
$this->created_at = new \DateTime($data['created_at']);
$this->closed_at = new \DateTime($data['closed_at']);
$this->id = (int)$data['id'];
$this->is_anonymous = (bool)$data['is_anonymous'];
$this->is_public = (bool)$data['is_public'];
foreach ($data['options'] as $option) {
$this->options[] = new PollOption($option);
}
if (!empty($data['poll_token'])) {
$this->token = $data['poll_token'];
}
$this->prompt = $data['prompt'];
if (!empty($data['user'])) {
$this->user = new User($data['user']);
}
if (!empty($data['source'])) {
$this->source = new Source($data['source']);
}
}
/** /**
* Returns the most voted option. If multiple options have the same amount * Returns the most voted option. If multiple options have the same amount
* of voted, return all of them. Always returns an array! * of voted, return all of them. Always returns an array!