This commit is contained in:
Erik C. Thauvin 2021-05-01 21:46:33 -07:00
parent 67f618da71
commit 9f3dc273f0
12 changed files with 49 additions and 51 deletions

View file

@ -202,6 +202,9 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
try {
connect(ircServer, ircPort)
} catch (e: Exception) {
if (logger.isDebugEnabled) {
logger.debug("Unable to connect to $ircServer, will try again.", e)
}
var retries = 0
while (retries++ < MAX_RECONNECT && !isConnected) {
sleep(10)
@ -212,7 +215,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
if (logger.isDebugEnabled) {
logger.debug("Unable to reconnect to $ircServer, after $MAX_RECONNECT retries.", ex)
}
e.printStackTrace(System.err)
System.err.println("An error has occurred while reconnecting; ${ex.message}")
exitProcess(1)
}
}
@ -528,7 +531,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
commandLine = parser.parse(options, args)
} catch (e: ParseException) {
System.err.println("CLI Parsing failed. Reason: ${e.message}")
e.printStackTrace(System.err)
exitProcess(1)
}
when {
@ -550,13 +552,11 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
).use { fis ->
p.load(fis)
}
} catch (e: FileNotFoundException) {
} catch (ignore: FileNotFoundException) {
System.err.println("Unable to find properties file.")
e.printStackTrace(System.err)
exitProcess(1)
} catch (e: IOException) {
} catch (ignore: IOException) {
System.err.println("Unable to open properties file.")
e.printStackTrace(System.err)
exitProcess(1)
}
val nickname = p.getProperty("nick", Mobibot::class.java.name.lowercase())
@ -574,9 +574,8 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
), true
)
System.setOut(stdout)
} catch (e: IOException) {
} catch (ignore: IOException) {
System.err.println("Unable to open output (stdout) log file.")
e.printStackTrace(System.err)
exitProcess(1)
}
try {
@ -586,9 +585,8 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
), true
)
System.setErr(stderr)
} catch (e: IOException) {
} catch (ignore: IOException) {
System.err.println("Unable to open error (stderr) log file.")
e.printStackTrace(System.err)
exitProcess(1)
}
}

View file

@ -32,6 +32,7 @@
package net.thauvin.erik.mobibot.modules
import net.objecthunter.exp4j.ExpressionBuilder
import net.objecthunter.exp4j.tokenizer.UnknownFunctionOrVariableException
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import java.text.DecimalFormat
@ -47,7 +48,15 @@ class Calc(bot: Mobibot) : AbstractModule(bot) {
isPrivate: Boolean
) {
if (args.isNotBlank()) {
bot.send(calc(args))
try {
bot.send(calculate(args))
} catch (e: IllegalArgumentException) {
if (bot.logger.isWarnEnabled) bot.logger.warn("Failed to calcualte: $args", e)
bot.send("No idea. This is the kind of math I don't get.")
} catch (e: UnknownFunctionOrVariableException) {
if (bot.logger.isWarnEnabled) bot.logger.warn("Unable to calcualte: $args", e)
bot.send("No idea. I must've some form of Dyscalculia.")
}
} else {
helpResponse(sender, isPrivate)
}
@ -61,14 +70,11 @@ class Calc(bot: Mobibot) : AbstractModule(bot) {
* Performs a calculation. e.g.: 1 + 1 * 2
*/
@JvmStatic
fun calc(query: String): String {
@Throws(IllegalArgumentException::class)
fun calculate(query: String): String {
val decimalFormat = DecimalFormat("#.##")
return try {
val calc = ExpressionBuilder(query).build()
query.replace(" ", "") + " = " + Utils.bold(decimalFormat.format(calc.evaluate()))
} catch (e: IllegalArgumentException) {
"No idea. This is the kind of math I don't get."
}
val calc = ExpressionBuilder(query).build()
return query.replace(" ", "") + " = " + Utils.bold(decimalFormat.format(calc.evaluate()))
}
}

View file

@ -52,7 +52,7 @@ class Lookup(bot: Mobibot) : AbstractModule(bot) {
if (args.matches("(\\S.)+(\\S)+".toRegex())) {
with(bot) {
try {
lookup(args).split(',').forEach {
nslookup(args).split(',').forEach {
send(it.trim())
}
} catch (ignore: UnknownHostException) {
@ -105,7 +105,7 @@ class Lookup(bot: Mobibot) : AbstractModule(bot) {
*/
@JvmStatic
@Throws(UnknownHostException::class)
fun lookup(query: String): String {
fun nslookup(query: String): String {
val buffer = StringBuilder()
val results = InetAddress.getAllByName(query)
var hostInfo: String

View file

@ -68,10 +68,10 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) {
}
/**
* Returns the world time.
* Returns the time for the given timezone/city.
*/
@JvmStatic
fun worldTime(query: String): Message {
fun time(query: String): Message {
val tz = COUNTRIES_MAP[(query.substring(query.indexOf(' ') + 1).trim()).uppercase()]
val response: String = if (tz != null) {
if (BEATS_KEYWORD == tz) {
@ -193,7 +193,7 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) {
send(sender, "The supported countries/zones are: ", isPrivate)
sendList(sender, ArrayList(COUNTRIES_MAP.keys), 17, isPrivate = false)
} else {
val msg = worldTime(args)
val msg = time(args)
if (isPrivate) {
send(sender, msg.msg, true)
} else {