diff --git a/build.gradle b/build.gradle index 39fa77f..0bd3331 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group 'cc.sukazyo' -version '0.3.2' +version '0.3.3' project.ext.archiveBaseName = 'Coeur_Morny_Cono' project.ext.artifactId = 'morny-coeur' mainClassName = 'cc.sukazyo.cono.morny.MornyCoeur' diff --git a/src/main/java/cc/sukazyo/cono/morny/MornySystem.java b/src/main/java/cc/sukazyo/cono/morny/MornySystem.java index 4fbfe17..49e8a08 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornySystem.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornySystem.java @@ -6,7 +6,7 @@ import java.net.URISyntaxException; public class MornySystem { - public static final String VERSION = "@G_DEV_VERSION@"; + public static final String VERSION = "0.3.3"; public static String getJarMd5() { try { diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java index bd26c6b..8359fef 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java @@ -6,11 +6,13 @@ public class EventListeners { public static final OnCommandExecute COMMANDS_LISTENER = new OnCommandExecute(); public static final OnActivityRecord ACTIVITY_RECORDER = new OnActivityRecord(); + public static final OnUserSlashAction USER_SLASH_ACTION = new OnUserSlashAction(); public static void registerAllListeners () { EventListenerManager.addListener( ACTIVITY_RECORDER, - COMMANDS_LISTENER + COMMANDS_LISTENER, + USER_SLASH_ACTION ); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java new file mode 100644 index 0000000..b43b554 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java @@ -0,0 +1,63 @@ +package cc.sukazyo.cono.morny.bot.event; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.bot.api.EventListener; +import cc.sukazyo.cono.morny.util.StringUtils; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.User; +import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.SendMessage; + +public class OnUserSlashAction extends EventListener { + + @Override + public boolean onMessage (Update event) { + String text = event.message().text(); + if (text == null) return false; + + if (text.startsWith("/")) { + int prefixLength = 1; + boolean useVerbSuffix = true; + boolean useObjectPrefix = true; + if (text.startsWith("//#") || text.startsWith("///")) { + useVerbSuffix = false; + useObjectPrefix = false; + prefixLength = 3; + } else if (text.startsWith("/#")) { + useObjectPrefix = false; + prefixLength = 2; + } else if (text.startsWith("//")) { + useVerbSuffix = false; + prefixLength = 2; + } + + String[] action = StringUtils.formatCommand(text.substring(prefixLength)); + String verb = action[0]; + boolean hasObject = action.length != 1; + String object = StringUtils.connectStringArray(action, " ", 1, action.length-1); + User origin = event.message().from(); + User target = (event.message().replyToMessage() == null ? ( + origin + ): ( + event.message().replyToMessage().from() + )); + + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + String.format( + "%s %s%s %s%s%s", + origin.id(), origin.firstName(), + verb, (useVerbSuffix?"了":""), + target.id(), (origin==target ? "自己" : target.firstName()), + (hasObject ? (useObjectPrefix ?" 的": " ") : ""), + (hasObject ? object : "") + ) + ).parseMode(ParseMode.HTML)); + + return true; + + } + return false; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java b/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java index cf35c87..1a31249 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java @@ -49,4 +49,14 @@ public class StringUtils { } + public static String connectStringArray (String[] array, String connector, int startIndex, int stopIndex) { + StringBuilder builder = new StringBuilder(); + for (int i = startIndex; i < stopIndex; i++) { + builder.append(array[i]); + builder.append(connector); + } + builder.append(array[stopIndex]); + return builder.toString(); + } + }