From cef31aa1fea16cc61daf910c8c775dc9a5991215 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 26 Nov 2021 22:26:02 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20gradle=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8F=B7=E4=BB=A3=E7=A0=81=E5=86=85=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=AC=A2=E8=BF=8E?= =?UTF-8?q?=E8=AF=AD=E5=BC=80=E5=85=B3=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 17 +++++++---------- .../java/cc/sukazyo/cono/morny/MornyCoeur.java | 5 ++++- .../java/cc/sukazyo/cono/morny/MornySystem.java | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index fb0257b..709257b 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group 'cc.sukazyo' -version '0.3.0' +version '0.3.1' project.ext.archiveBaseName = 'Coeur_Morny_Cono' project.ext.artifactId = 'morny-coeur' mainClassName = 'cc.sukazyo.cono.morny.MornyCoeur' @@ -24,6 +24,12 @@ dependencies { } +compileJava.doFirst { + ant.replaceregexp(match:'VERSION = ["a-zA-Z0-9.\\-_+@]+;', replace:"VERSION = \"$project.version\";", flags:'g', byline:true) { + fileset(dir: 'src/main/java/cc/sukazyo/cono/morny', includes: 'MornySystem.java') + } +} + test { useJUnitPlatform() } @@ -46,15 +52,6 @@ tasks.test { useJUnitPlatform() } -apply plugin: 'com.github.johnrengelman.shadow' -apply plugin: 'application' - -shadowJar { - archiveBaseName.set("${project.ext.archiveBaseName}") - archiveVersion.set("${project.version}") - archiveClassifier.set("fat") -} - shadowJar { archiveBaseName.set("${project.ext.archiveBaseName}") archiveVersion.set("${project.version}") diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index e6fd6cd..f652188 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -15,7 +15,10 @@ public class MornyCoeur { public static void main (String[] args) { - logger.info(MornyHello.MORNY_PREVIEW_IMAGE_ASCII); + if (!(args.length > 1 && "--no-hello".equals(args[1]))) + logger.info(MornyHello.MORNY_PREVIEW_IMAGE_ASCII); + if (args.length > 1 && "--only-hello".equals(args[1])) + return; logger.info("System Starting"); configureSafeExit(); diff --git a/src/main/java/cc/sukazyo/cono/morny/MornySystem.java b/src/main/java/cc/sukazyo/cono/morny/MornySystem.java index bedb6b3..4fbfe17 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 = "0.3.0"; + public static final String VERSION = "@G_DEV_VERSION@"; public static String getJarMd5() { try { From ed5d3e00bc90602b60dc1047b912a9218a498ffc Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 26 Nov 2021 23:31:10 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E5=88=B0=E5=91=BD=E4=BB=A4=E6=95=B0=E7=BB=84=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=EF=BC=8C=E6=B7=BB=E5=8A=A0=20/user=20=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../morny/bot/event/OnCommandExecute.java | 12 ++- .../event/on_commands/GetUsernameAndId.java | 100 ++++++++++++++++++ .../sukazyo/cono/morny/util/StringUtils.java | 39 +++++++ 4 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java diff --git a/build.gradle b/build.gradle index 709257b..39fa77f 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group 'cc.sukazyo' -version '0.3.1' +version '0.3.2' 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/bot/event/OnCommandExecute.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java index 67af138..389b329 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java @@ -4,6 +4,8 @@ import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornySystem; import cc.sukazyo.cono.morny.MornyTrusted; import cc.sukazyo.cono.morny.bot.api.EventListener; +import cc.sukazyo.cono.morny.bot.event.on_commands.GetUsernameAndId; +import cc.sukazyo.cono.morny.util.StringUtils; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.SendMessage; @@ -23,7 +25,13 @@ public class OnCommandExecute extends EventListener { if (event.message().text() == null) { return false; } - switch (event.message().text()) { + String[] command = StringUtils.formatCommand(event.message().text()); + if (command.length == 0) return false; + switch (command[0]) { + case "/user": + case "/user@" + MornyCoeur.USERNAME: + GetUsernameAndId.exec(command, event); + break; case "/o": case "/o@" + MornyCoeur.USERNAME: onCommandOnExec(event); @@ -94,7 +102,7 @@ public class OnCommandExecute extends EventListener { MornySystem.VERSION, MornySystem.getJarMd5() ) - ).parseMode(ParseMode.HTML)); + ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java new file mode 100644 index 0000000..f896e50 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java @@ -0,0 +1,100 @@ +package cc.sukazyo.cono.morny.bot.event.on_commands; + +import cc.sukazyo.cono.morny.MornyCoeur; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.User; +import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.GetChatMember; +import com.pengrad.telegrambot.request.SendMessage; +import com.pengrad.telegrambot.response.GetChatMemberResponse; + +public class GetUsernameAndId { + + public static void exec (String[] command, Update event) { + + if (command.length > 2) { MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Unavailable] Too much arguments." + ).replyToMessageId(event.message().messageId())); return; } + + long userId = 0; + + if ( event.message().replyToMessage()!= null) { + userId = event.message().replyToMessage().from().id(); + } + if (command.length > 1) { + try { + userId = Long.parseLong(command[1]); + } catch (NumberFormatException e) { + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Unavailable] " + e.getMessage() + ).replyToMessageId(event.message().messageId())); + return; + } + } + + if (userId == 0) { + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Unavailable] no userid given." + ).replyToMessageId(event.message().messageId())); + return; + } + + GetChatMemberResponse response = MornyCoeur.getAccount().execute( + new GetChatMember(event.message().chat().id(), userId) + ); + + if (response.chatMember() == null) { + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Unavailable] user not found." + ).replyToMessageId(event.message().messageId())); + return; + } + + User user = response.chatMember().user(); + + StringBuilder userInformation = new StringBuilder(); + userInformation.append(String.format( + "userid :\n" + + "- %d\n" + + "username :\n" + + "- %s", + userId, user.username() + )); + if (user.firstName() == null) { + userInformation.append("\nfirstname : null"); + } else { + userInformation.append(String.format( + "\nfirstname :\n" + + "- %s", + user.firstName() + )); + } + if (user.lastName() == null) { + userInformation.append("\nlastname : null"); + } else { + userInformation.append(String.format( + "\nlastname :\n" + + "- %s", + user.lastName() + )); + } + if (user.languageCode() != null) { + userInformation.append(String.format( + "\nlanguage-code :\n" + + "- %s", + user.languageCode() + )); + } + + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + userInformation.toString() + ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); + + } + +} 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 7190670..cf35c87 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java @@ -1,5 +1,6 @@ package cc.sukazyo.cono.morny.util; +import java.util.ArrayList; import java.util.Arrays; public class StringUtils { @@ -10,4 +11,42 @@ public class StringUtils { return new String(chars); } + public static String[] formatCommand (String com) { + + ArrayList arr = new ArrayList<>(); + + StringBuilder tmp = new StringBuilder(); + char[] coma = com.toCharArray(); + for (int i = 0; i < coma.length; i++) { + if (coma[i] == ' ') { + if (!tmp.toString().equals("")) { arr.add(tmp.toString()); } + tmp.setLength(0); + } else if (coma[i] == '"') { + while (true) { + i++; + if (coma[i] == '"') { + break; + } else if (coma[i] == '\\' && (coma[i+1] == '"' || coma[i+1] == '\\')) { + i++; + tmp.append(coma[i]); + } else { + tmp.append(coma[i]); + } + } + } else if (coma[i] == '\\' && (coma[i+1] == ' ' || coma[i+1] == '"' || coma[i+1] == '\\')) { + i++; + tmp.append(coma[i]); + } else { + tmp.append(coma[i]); + } + } + if (!tmp.toString().equals("")) { arr.add(tmp.toString()); } + tmp.setLength(0); + + String[] out = new String[arr.size()]; + arr.toArray(out); + return out; + + } + } From 4e89f6749605a5e6182097dd4969b069c0645f84 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 27 Nov 2021 19:59:48 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0"=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=8F=91=E8=B5=B7=E5=8A=A8=E4=BD=9C=E5=93=8D=E5=BA=94"?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 支援动词后缀和名词前缀关闭选项 --- build.gradle | 2 +- .../cc/sukazyo/cono/morny/MornySystem.java | 2 +- .../cono/morny/bot/event/EventListeners.java | 4 +- .../morny/bot/event/OnUserSlashAction.java | 63 +++++++++++++++++++ .../sukazyo/cono/morny/util/StringUtils.java | 10 +++ 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java 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(); + } + }