2022-03-16 17:51:56 +08:00
|
|
|
package cc.sukazyo.cono.morny.bot.event;
|
|
|
|
|
|
|
|
import cc.sukazyo.cono.morny.MornyCoeur;
|
|
|
|
import cc.sukazyo.cono.morny.bot.api.EventListener;
|
2022-10-04 15:18:42 +08:00
|
|
|
import cc.sukazyo.cono.morny.util.UniversalCommand;
|
2022-03-16 17:51:56 +08:00
|
|
|
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;
|
|
|
|
|
2022-10-04 15:18:42 +08:00
|
|
|
final String[] preProcess = UniversalCommand.format(update.message().text());
|
2022-03-16 17:51:56 +08:00
|
|
|
if (preProcess.length > 1) return false;
|
|
|
|
final String query = preProcess[0];
|
|
|
|
|
2022-03-17 01:31:56 +08:00
|
|
|
// ----- START CODE BLOCK COMMENT -----
|
|
|
|
// 这里实现思路和代码优化有至少一半是 copilot 和 IDEA 提供的
|
|
|
|
// 实现思路都可以从人类手里抢一半贡献太恐怖了aba
|
2022-03-16 17:51:56 +08:00
|
|
|
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);
|
|
|
|
}
|
2022-03-17 01:31:56 +08:00
|
|
|
// ----- STOP CODE BLOCK COMMENT -----
|
2022-03-16 17:51:56 +08:00
|
|
|
|
|
|
|
if (result == null) return false;
|
|
|
|
MornyCoeur.extra().exec(new SendMessage(
|
|
|
|
update.message().chat().id(), result
|
|
|
|
).replyToMessageId(update.message().messageId()));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|