From b98879bd7d1cf08ea5ae4964da2539fd5effd362 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Tue, 11 May 2021 17:51:24 -0700 Subject: [PATCH] Converted info command to Kotlin. --- .../thauvin/erik/mobibot/commands/Info.java | 109 ------------------ .../net/thauvin/erik/mobibot/commands/Info.kt | 80 +++++++++++++ 2 files changed, 80 insertions(+), 109 deletions(-) delete mode 100644 src/main/java/net/thauvin/erik/mobibot/commands/Info.java create mode 100644 src/main/kotlin/net/thauvin/erik/mobibot/commands/Info.kt diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/Info.java b/src/main/java/net/thauvin/erik/mobibot/commands/Info.java deleted file mode 100644 index 8597c8c..0000000 --- a/src/main/java/net/thauvin/erik/mobibot/commands/Info.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Info.java - * - * Copyright (c) 2004-2021, Erik C. Thauvin (erik@thauvin.net) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * Neither the name of this project nor the names of its contributors may be - * used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package net.thauvin.erik.mobibot.commands; - -import net.thauvin.erik.mobibot.Mobibot; -import net.thauvin.erik.mobibot.ReleaseInfo; -import net.thauvin.erik.mobibot.Utils; -import net.thauvin.erik.mobibot.commands.links.LinksMgr; -import org.jetbrains.annotations.NotNull; - -import java.lang.management.ManagementFactory; -import java.util.List; - -public class Info extends AbstractCommand { - private final List allVersions = List.of( - Utils.capitalize(ReleaseInfo.PROJECT) + " " + ReleaseInfo.VERSION - + " (" + Utils.green(ReleaseInfo.WEBSITE) + ')', - "Written by " + ReleaseInfo.AUTHOR + " (" + Utils.green(ReleaseInfo.AUTHOR_URL) + ')'); - - public Info(final Mobibot bot) { - super(bot); - } - - @NotNull - @Override - public String getName() { - return "info"; - } - - @NotNull - @Override - public List getHelp() { - return List.of("To view information about the bot:", Utils.helpFormat("%c " + getName())); - } - - @Override - public boolean isOp() { - return false; - } - - @Override - public boolean isPublic() { - return true; - } - - @Override - public boolean isVisible() { - return true; - } - - @Override - public void commandResponse(@NotNull final String sender, - @NotNull final String login, - @NotNull final String args, - final boolean isOp, - final boolean isPrivate) { - getBot().sendList(sender, allVersions, 1, isPrivate); - - final StringBuilder info = new StringBuilder(29); - - info.append("Uptime: ") - .append(Utils.uptime(ManagementFactory.getRuntimeMXBean().getUptime())) - .append(" [Entries: ") - .append(LinksMgr.entries.size()); - - if (isOp) { - if (getBot().getTell().isEnabled()) { - info.append(", Messages: ").append(getBot().getTell().size()); - } - if (getBot().getTwitter().isAutoPost()) { - info.append(", Twitter: ").append(getBot().getTwitter().entriesCount()); - } - } - - info.append(", Recap: ").append(Recap.recaps.size()).append(']'); - - getBot().send(sender, info.toString(), isPrivate); - } -} diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Info.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Info.kt new file mode 100644 index 0000000..d3df42e --- /dev/null +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Info.kt @@ -0,0 +1,80 @@ +/* + * Info.java + * + * Copyright (c) 2004-2021, Erik C. Thauvin (erik@thauvin.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of this project nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.thauvin.erik.mobibot.commands + +import net.thauvin.erik.mobibot.Mobibot +import net.thauvin.erik.mobibot.ReleaseInfo +import net.thauvin.erik.mobibot.Utils.capitalize +import net.thauvin.erik.mobibot.Utils.green +import net.thauvin.erik.mobibot.Utils.helpFormat +import net.thauvin.erik.mobibot.Utils.uptime +import net.thauvin.erik.mobibot.commands.links.LinksMgr +import java.lang.management.ManagementFactory + +class Info(bot: Mobibot?) : AbstractCommand(bot!!) { + private val allVersions = listOf( + capitalize(ReleaseInfo.PROJECT) + " ${ReleaseInfo.VERSION} (" + green(ReleaseInfo.WEBSITE) + ')', + "Written by ${ReleaseInfo.AUTHOR} (" + green(ReleaseInfo.AUTHOR_URL) + ')' + ) + override val name = "info" + override val help = listOf("To view information about the bot:", helpFormat("%c $name")) + override val isOp = false + override val isPublic = true + override val isVisible = true + + override fun commandResponse( + sender: String, + login: String, + args: String, + isOp: Boolean, + isPrivate: Boolean + ) { + with(bot) { + sendList(sender, allVersions, 1, isPrivate) + val info = StringBuilder() + info.append("Uptime: ") + .append(uptime(ManagementFactory.getRuntimeMXBean().uptime)) + .append(" [Entries: ") + .append(LinksMgr.entries.size) + if (isOp) { + if (tell.isEnabled()) { + info.append(", Messages: ").append(tell.size()) + } + if (twitter.isAutoPost) { + info.append(", Twitter: ").append(twitter.entriesCount()) + } + } + info.append(", Recap: ").append(Recap.recaps.size).append(']') + send(sender, info.toString(), isPrivate) + } + } +}