3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-23 19:22:40 +01:00

Plugins/Wttr: update -u to use new Users; fix JSON error handling

This commit is contained in:
Pragmatic Software 2020-04-27 16:06:15 -07:00
parent e98cd759fd
commit 51f17c0b52

View File

@ -83,13 +83,18 @@ sub wttrcmd {
return $usage if exists $options{h}; return $usage if exists $options{h};
$arguments = "@opt_args"; $arguments = "@opt_args";
my $hostmask = defined $options{u} ? $options{u} : "$nick!$user\@$host"; if (defined $options{u}) {
my $location_override = $self->{pbot}->{users}->get_user_metadata($from, $hostmask, 'location') // ''; my $username = delete $options{u};
$arguments = $location_override if not length $arguments;
if (defined $options{u} and not length $location_override) { return "No location set or user account does not exist."; } my $userdata = $self->{pbot}->{users}->{users}->get_data($username);
return "No such user account $username." if not defined $userdata;
delete $options{u}; return "User account does not have `location` set." if not exists $userdata->{location};
$arguments = $userdata->{location};
} else {
if (not length $arguments) {
$arguments = $self->{pbot}->{users}->get_user_metadata($from, "$nick!$user\@$host", 'location') // '';
}
}
if (not length $arguments) { return $usage; } if (not length $arguments) { return $usage; }
@ -124,7 +129,16 @@ sub get_wttr {
else { return "Failed to fetch weather data: " . $response->status_line; } else { return "Failed to fetch weather data: " . $response->status_line; }
my $wttr = eval { decode_json $json }; my $wttr = eval { decode_json $json };
return $json if $@; # error decoding json so it must not be json -- return as-is
if ($@) {
# error decoding json so it must not be json -- return as-is
$@ = undef;
my $error = $json;
if ($error =~ /^Unknown location/) {
$error = "Unknown location: $location";
}
return $error;
}
if (exists $wttr->{nearest_area}) { if (exists $wttr->{nearest_area}) {
my $areaName = $wttr->{nearest_area}->[0]->{areaName}->[0]->{value}; my $areaName = $wttr->{nearest_area}->[0]->{areaName}->[0]->{value};