Cleanup.
This commit is contained in:
parent
ced0aa97f8
commit
968823828a
2 changed files with 43 additions and 36 deletions
|
@ -92,7 +92,7 @@ public class Weather2 extends ThreadedModule {
|
||||||
@SuppressWarnings("AvoidEscapedUnicodeCharacters")
|
@SuppressWarnings("AvoidEscapedUnicodeCharacters")
|
||||||
private static String getTemps(final Double d) {
|
private static String getTemps(final Double d) {
|
||||||
final double c = (d - 32) * 5 / 9;
|
final double c = (d - 32) * 5 / 9;
|
||||||
return Math.round(d) + " \u00B0F, " + Math.round(c) + " \u00B0C";
|
return Math.round(d) + " °F, " + Math.round(c) + " °C";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -138,7 +138,8 @@ public class Weather2 extends ThreadedModule {
|
||||||
cwd = owm.currentWeatherByCityName(city, getCountry(country));
|
cwd = owm.currentWeatherByCityName(city, getCountry(country));
|
||||||
}
|
}
|
||||||
if (cwd.hasCityName()) {
|
if (cwd.hasCityName()) {
|
||||||
messages.add(new PublicMessage("City: " + cwd.getCityName() + " [" + country + "]"));
|
messages.add(new PublicMessage(
|
||||||
|
"City: " + cwd.getCityName() + " [" + StringUtils.upperCase(country) + "]"));
|
||||||
|
|
||||||
final Main main = cwd.getMainData();
|
final Main main = cwd.getMainData();
|
||||||
if (main != null) {
|
if (main != null) {
|
||||||
|
@ -159,15 +160,13 @@ public class Weather2 extends ThreadedModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cwd.hasWeatherList()) {
|
if (cwd.hasWeatherList()) {
|
||||||
final StringBuilder condition = new StringBuilder("Condition: ");
|
final StringBuilder condition = new StringBuilder("Condition:");
|
||||||
final List<Weather> list = cwd.getWeatherList();
|
final List<Weather> list = cwd.getWeatherList();
|
||||||
if (list != null) {
|
if (list != null) {
|
||||||
for (final Weather w : list) {
|
for (final Weather w : list) {
|
||||||
if (condition.indexOf(",") == -1) {
|
condition.append(' ')
|
||||||
condition.append(StringUtils.capitalize(w.getDescription()));
|
.append(StringUtils.capitalize(w.getDescription()))
|
||||||
} else {
|
.append('.');
|
||||||
condition.append(", ").append(w.getDescription());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
messages.add(new NoticeMessage(condition.toString()));
|
messages.add(new NoticeMessage(condition.toString()));
|
||||||
}
|
}
|
||||||
|
@ -182,7 +181,8 @@ public class Weather2 extends ThreadedModule {
|
||||||
"https://openweathermap.org/find"))
|
"https://openweathermap.org/find"))
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.addQueryParameter("q",
|
.addQueryParameter("q",
|
||||||
city + ',' + country)
|
city + ','
|
||||||
|
+ StringUtils.upperCase(country))
|
||||||
.build();
|
.build();
|
||||||
messages.add(new NoticeMessage(url.toString(), Colors.GREEN));
|
messages.add(new NoticeMessage(url.toString(), Colors.GREEN));
|
||||||
}
|
}
|
||||||
|
@ -210,26 +210,28 @@ public class Weather2 extends ThreadedModule {
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
|
public void helpResponse(final Mobibot bot, final String sender, final boolean isPrivate) {
|
||||||
bot.send(sender, bold("To display weather information:"));
|
bot.send(sender, "To display weather information:", isPrivate);
|
||||||
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + WEATHER_CMD + " <city> [, <country code>]"));
|
bot.send(sender,
|
||||||
bot.send(sender, "For example:");
|
Utils.helpIndent(bot.getNick() + ": " + WEATHER_CMD + " <city> [, <country code>]"),
|
||||||
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + WEATHER_CMD + " paris, fr"));
|
isPrivate);
|
||||||
|
bot.send(sender, "For example:", isPrivate);
|
||||||
|
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + WEATHER_CMD + " paris, fr"), isPrivate);
|
||||||
bot.send(sender,
|
bot.send(sender,
|
||||||
"The default ISO 3166 country code is " + bold("US")
|
"The default ISO 3166 country code is " + bold("US")
|
||||||
+ ". Zip codes are supported in most countries.");
|
+ ". Zip codes are supported in most countries.", isPrivate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches the weather data from a specific city.
|
* Fetches the weather data from a specific city.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
void run(final Mobibot bot, final String sender, final String cmd, final String args) {
|
void run(final Mobibot bot, final String sender, final String cmd, final String args, final boolean isPrivate) {
|
||||||
if (StringUtils.isNotBlank(args)) {
|
if (StringUtils.isNotBlank(args)) {
|
||||||
try {
|
try {
|
||||||
final List<Message> messages = getWeather(args, properties.get(OWM_API_KEY_PROP));
|
final List<Message> messages = getWeather(args, properties.get(OWM_API_KEY_PROP));
|
||||||
if (messages.get(0).isError()) {
|
if (messages.get(0).isError()) {
|
||||||
helpResponse(bot, sender, args, true);
|
helpResponse(bot, sender, isPrivate);
|
||||||
} else {
|
} else {
|
||||||
for (final Message msg : messages) {
|
for (final Message msg : messages) {
|
||||||
bot.send(sender, msg);
|
bot.send(sender, msg);
|
||||||
|
@ -240,7 +242,7 @@ public class Weather2 extends ThreadedModule {
|
||||||
bot.send(e.getMessage());
|
bot.send(e.getMessage());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
helpResponse(bot, sender, args, true);
|
helpResponse(bot, sender, isPrivate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,12 +44,11 @@ import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.time.temporal.ChronoField;
|
import java.time.temporal.ChronoField;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
import static net.thauvin.erik.mobibot.Utils.bold;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The WorldTime module.
|
* The WorldTime module.
|
||||||
*
|
*
|
||||||
|
@ -195,14 +194,16 @@ public final class WorldTime extends AbstractModule {
|
||||||
|
|
||||||
if (tz != null) {
|
if (tz != null) {
|
||||||
if (BEATS_KEYWORD.equals(tz)) {
|
if (BEATS_KEYWORD.equals(tz)) {
|
||||||
response = ("The current Internet Time is: " + internetTime() + ' ' + BEATS_KEYWORD);
|
response = ("The current Internet Time is: " + Utils.bold(internetTime() + ' ' + BEATS_KEYWORD));
|
||||||
} else {
|
} else {
|
||||||
response = ZonedDateTime.now().withZoneSameInstant(ZoneId.of(tz)).format(
|
response = ZonedDateTime.now().withZoneSameInstant(ZoneId.of(tz)).format(
|
||||||
DateTimeFormatter.ofPattern("'The time is 'HH:mm' on 'EEEE, d MMMM yyyy' in '"))
|
DateTimeFormatter
|
||||||
+ tz.substring(tz.indexOf('/') + 1).replace('_', ' ');
|
.ofPattern("'The time is " + Utils.bold("'HH:mm'") + " on " + Utils.bold(
|
||||||
|
"'EEEE, d MMMM yyyy'") + " in '"))
|
||||||
|
+ Utils.bold(tz.substring(tz.indexOf('/') + 1).replace('_', ' '));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return new ErrorMessage("The supported countries/zones are: " + COUNTRIES_MAP.keySet());
|
return new ErrorMessage("Unsupported country/zone. Please try again.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new PublicMessage(response);
|
return new PublicMessage(response);
|
||||||
|
@ -217,29 +218,33 @@ public final class WorldTime extends AbstractModule {
|
||||||
final String cmd,
|
final String cmd,
|
||||||
final String args,
|
final String args,
|
||||||
final boolean isPrivate) {
|
final boolean isPrivate) {
|
||||||
|
if (args.length() == 0) {
|
||||||
|
bot.send(sender, "The supported countries/zones are: ", isPrivate);
|
||||||
|
bot.sendCommandsList(sender, new ArrayList<>(COUNTRIES_MAP.keySet()), 17, false, false);
|
||||||
|
} else {
|
||||||
final Message msg = worldTime(args);
|
final Message msg = worldTime(args);
|
||||||
|
|
||||||
if (isPrivate) {
|
if (isPrivate) {
|
||||||
bot.send(sender, msg.getMessage(), true);
|
bot.send(sender, msg.getMessage(), true);
|
||||||
} else {
|
} else {
|
||||||
if (msg.isError()) {
|
if (msg.isError()) {
|
||||||
bot.send(sender, msg.getMessage());
|
bot.send(sender, msg.getMessage(), false);
|
||||||
} else {
|
} else {
|
||||||
bot.send(msg.getMessage());
|
bot.send(msg.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
|
public void helpResponse(final Mobibot bot, final String sender, final boolean isPrivate) {
|
||||||
bot.send(sender, bold("To display a country's current date/time:"));
|
bot.send(sender, "To display a country's current date/time:", isPrivate);
|
||||||
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + TIME_CMD) + " [<country code>]");
|
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + TIME_CMD) + " [<country code>]", isPrivate);
|
||||||
|
|
||||||
bot.send(sender, bold("For a listing of the supported countries:"));
|
bot.send(sender, "For a listing of the supported countries:", isPrivate);
|
||||||
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + TIME_CMD));
|
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + TIME_CMD), isPrivate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue