添加 XXX还是XXX 的功能 通过斜线前缀触发 #2

This commit is contained in:
A.C.Sukazyo Eyre 2022-03-16 17:51:56 +08:00
parent 5046a27266
commit ce53e70dfa
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
4 changed files with 53 additions and 3 deletions

View File

@ -1,6 +1,6 @@
## Core ## Core
VERSION = 0.7.0.1 VERSION = 0.7.0.2
# dependencies # dependencies

View File

@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny;
* the final field that will be updated by gradle automatically. * the final field that will be updated by gradle automatically.
*/ */
public class GradleProjectConfigures { public class GradleProjectConfigures {
public static final String VERSION = "0.7.0.1"; public static final String VERSION = "0.7.0.2";
public static final long COMPILE_TIMESTAMP = 1647424091182L; public static final long COMPILE_TIMESTAMP = 1647451512009L;
} }

View File

@ -12,6 +12,7 @@ public class EventListeners {
public static final OnCallMe CALL_ME = new OnCallMe(); public static final OnCallMe CALL_ME = new OnCallMe();
public static final OnEventHackHandle EVENT_HACK_HANDLE = new OnEventHackHandle(); public static final OnEventHackHandle EVENT_HACK_HANDLE = new OnEventHackHandle();
public static final OnKuohuanhuanNeedSleep KUOHUANHUAN_NEED_SLEEP = new OnKuohuanhuanNeedSleep(); public static final OnKuohuanhuanNeedSleep KUOHUANHUAN_NEED_SLEEP = new OnKuohuanhuanNeedSleep();
public static final OnUserRandoms USER_RANDOMS = new OnUserRandoms();
public static void registerAllListeners () { public static void registerAllListeners () {
EventListenerManager.addListener( EventListenerManager.addListener(
@ -19,6 +20,7 @@ public class EventListeners {
UPDATE_TIMESTAMP_OFFSET_LOCK, UPDATE_TIMESTAMP_OFFSET_LOCK,
KUOHUANHUAN_NEED_SLEEP, KUOHUANHUAN_NEED_SLEEP,
COMMANDS_LISTENER, COMMANDS_LISTENER,
USER_RANDOMS,
USER_SLASH_ACTION, USER_SLASH_ACTION,
INLINE_QUERY, INLINE_QUERY,
CALL_ME, CALL_ME,

View File

@ -0,0 +1,48 @@
package cc.sukazyo.cono.morny.bot.event;
import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.bot.api.EventListener;
import cc.sukazyo.untitled.util.command.CommonCommand;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.request.SendMessage;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class OnUserRandoms extends EventListener {
private static final Pattern USER_OR_CN_QUERY = Pattern.compile("(.+)还是(.+)");
private static final Pattern USER_OR_EN_QUERY = Pattern.compile("(.+)or(.+)");
@Override
public boolean onMessage (@NotNull Update update) {
if (update.message().text() == null) return false;
if (!update.message().text().startsWith("/")) return false;
final String[] preProcess = CommonCommand.format(update.message().text());
if (preProcess.length > 1) return false;
final String query = preProcess[0];
String result = null;
final Matcher matcher;
if (query.contains("还是")) {
matcher = USER_OR_CN_QUERY.matcher(query);
} else {
matcher = USER_OR_EN_QUERY.matcher(query);
}
if (matcher.find()) {
result = ThreadLocalRandom.current().nextBoolean() ? matcher.group(1) : matcher.group(2);
}
if (result == null) return false;
MornyCoeur.extra().exec(new SendMessage(
update.message().chat().id(), result
).replyToMessageId(update.message().messageId()));
return true;
}
}