weather tz

This commit is contained in:
Ben Harris 2017-03-23 14:37:48 -04:00
parent b783df20c9
commit 8170c7c36a
2 changed files with 10 additions and 17 deletions

15
bot.php
View File

@ -174,11 +174,9 @@ $savecity = function($msg, $args) use ($cities, $discord) {
///////////////////////////////////////////////////////////
$time = $discord->registerCommand('time', function($msg, $args) use ($cities, $discord) {
$id = is_dm($msg) ? $msg->author->id : $msg->author->user->id;
$url = "http://api.geonames.org/timezoneJSON?username=benharri";
if (count($args) == 0) {
// lookup the person's time or tell them to save their time
$msg->channel->broadcastTyping();
if ($cities->get($id, true)) {
$ci = $cities->get($id);
send($msg, "It's " . Carbon::now($ci["timezone"])->format('g:i A \o\n l F j, Y') . " in {$ci["city"]}.");
@ -187,11 +185,9 @@ $time = $discord->registerCommand('time', function($msg, $args) use ($cities, $d
}
} else {
if (count($msg->mentions) > 0) {
// if users are mentioned
foreach ($msg->mentions as $mention) {
if ($cities->get($mention->id, true)) {
$msg->channel->broadcastTyping();
$ci = $cities->get($mention->id);
send($msg, "It's " . Carbon::now($ci["timezone"])->format('g:i A \o\n l F j, Y') . " in {$ci["city"]}.");
} else {
@ -235,14 +231,15 @@ register_help('time');
///////////////////////////////////////////////////////////
$weather = $discord->registerCommand('weather', function($msg, $args) use ($cities, $discord) {
$id = is_dm($msg) ? $msg->author->id : $msg->author->user->id;
$api_key = get_thing('weather_api_key');
$url = "http://api.openweathermap.org/data/2.5/weather?APPID=$api_key&units=metric&";
if (count($args) == 0) {
// look up for your saved city
if ($cities->get($msg->author->id, true)) {
$url .= "id=" . $cities->get($msg->author->id)["id"];
if ($cities->get($id, true)) {
$url .= "id=" . $cities->get($id)["id"];
$discord->http->get($url)->then(function($result) use ($msg) {
send($msg, "", format_weather($result));
send($msg, "", format_weather($result, $id));
});
} else {
$msg->reply("you can set your preferred city with `;weather save <city>`");
@ -255,7 +252,7 @@ $weather = $discord->registerCommand('weather', function($msg, $args) use ($citi
if ($cities->get($mention->id, true)) {
$url .= "id=" . $cities->get($mention->id)["id"];
$discord->http->get($url)->then(function($result) use ($msg) {
send($msg, "", format_weather($result));
send($msg, "", format_weather($result, $mention->id));
});
} else {
// mentioned user not found

View File

@ -69,10 +69,10 @@ function ascii_from_img($filepath) {
function fahr($celsius) {return $celsius * 9 / 5 + 32;}
function cels($fahrenh) {return $fahrenh * 5 / 9 - 32;}
function format_weather($json) {
function format_weather($json, $id = null) {
global $discord;
global $cities;
// $fahr = round($json->main->temp * 5 / 9 + 32);
return $discord->factory(Embed::class, [
'title' => "Weather in {$json->name}, {$json->sys->country}",
'thumbnail' => ['url' => "http://openweathermap.org/img/w/{$json->weather[0]->icon}.png"],
@ -83,16 +83,12 @@ function format_weather($json) {
['name' => 'Atmospheric Pressure', 'value' => "{$json->main->pressure} hPa", 'inline' => true],
['name' => 'Humidity', 'value' => "{$json->main->humidity} %", 'inline' => true],
['name' => 'Wind', 'value' => "{$json->wind->speed} meters/second, {$json->wind->deg}°", 'inline' => true],
['name' => 'Sunrise', 'value' => Carbon::createFromTimestampUTC($json->sys->sunrise)->toTimeString(), 'inline' => true],
['name' => 'Sunset', 'value' => Carbon::createFromTimestampUTC($json->sys->sunset)->toTimeString(), 'inline' => true],
['name' => 'Sunrise', 'value' => Carbon::createFromTimestampUTC($json->sys->sunrise, $cities->get($id)["timezone"])->toTimeString(), 'inline' => true],
['name' => 'Sunset', 'value' => Carbon::createFromTimestampUTC($json->sys->sunset, $cities->get($id)["timezone"])->toTimeString(), 'inline' => true],
],
'timestamp' => null,
]);
// $ret = <<<EOD
// it's {$json->main->temp}°C ({$fahr}°F) with {$json->weather[0]->description} in {$json->name}, {$json->sys->country}
// EOD;
// return $ret;
}