维护一个置顶
This commit is contained in:
parent
52b3a5016d
commit
c2ee4fca4f
1 changed files with 75 additions and 18 deletions
|
@ -1,6 +1,5 @@
|
||||||
package de.strifel.VTools.listeners;
|
package de.strifel.VTools.listeners;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
|
||||||
import com.pengrad.telegrambot.Callback;
|
import com.pengrad.telegrambot.Callback;
|
||||||
import com.pengrad.telegrambot.TelegramBot;
|
import com.pengrad.telegrambot.TelegramBot;
|
||||||
import com.pengrad.telegrambot.UpdatesListener;
|
import com.pengrad.telegrambot.UpdatesListener;
|
||||||
|
@ -8,8 +7,10 @@ import com.pengrad.telegrambot.model.Message;
|
||||||
import com.pengrad.telegrambot.model.Update;
|
import com.pengrad.telegrambot.model.Update;
|
||||||
import com.pengrad.telegrambot.model.User;
|
import com.pengrad.telegrambot.model.User;
|
||||||
import com.pengrad.telegrambot.request.EditMessageText;
|
import com.pengrad.telegrambot.request.EditMessageText;
|
||||||
|
import com.pengrad.telegrambot.request.GetChat;
|
||||||
import com.pengrad.telegrambot.request.SendMessage;
|
import com.pengrad.telegrambot.request.SendMessage;
|
||||||
import com.pengrad.telegrambot.response.BaseResponse;
|
import com.pengrad.telegrambot.response.BaseResponse;
|
||||||
|
import com.pengrad.telegrambot.response.GetChatResponse;
|
||||||
import com.pengrad.telegrambot.response.SendResponse;
|
import com.pengrad.telegrambot.response.SendResponse;
|
||||||
import com.velocitypowered.api.event.Subscribe;
|
import com.velocitypowered.api.event.Subscribe;
|
||||||
import com.velocitypowered.api.event.connection.DisconnectEvent;
|
import com.velocitypowered.api.event.connection.DisconnectEvent;
|
||||||
|
@ -49,6 +50,9 @@ public class TGBridge {
|
||||||
|
|
||||||
private long backoffSec = 1L;
|
private long backoffSec = 1L;
|
||||||
|
|
||||||
|
private static final String DIVIDER = "----------\n";
|
||||||
|
private String pinNote;
|
||||||
|
|
||||||
public TGBridge(@NotNull VTools plugin) {
|
public TGBridge(@NotNull VTools plugin) {
|
||||||
INSTANCE = this;
|
INSTANCE = this;
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
@ -88,6 +92,7 @@ public class TGBridge {
|
||||||
loadConfig();
|
loadConfig();
|
||||||
if (TOKEN.isEmpty() || CHAT_ID == 0L) return;
|
if (TOKEN.isEmpty() || CHAT_ID == 0L) return;
|
||||||
bot = new TelegramBot(TOKEN);
|
bot = new TelegramBot(TOKEN);
|
||||||
|
getPinnedMessage();
|
||||||
bot.setUpdatesListener(updates -> {
|
bot.setUpdatesListener(updates -> {
|
||||||
backoffSec = 1L;
|
backoffSec = 1L;
|
||||||
for (Update update : updates) {
|
for (Update update : updates) {
|
||||||
|
@ -100,22 +105,43 @@ public class TGBridge {
|
||||||
) {
|
) {
|
||||||
if (update.message().text() != null && !update.message().text().isEmpty()) {
|
if (update.message().text() != null && !update.message().text().isEmpty()) {
|
||||||
String msg = update.message().text();
|
String msg = update.message().text();
|
||||||
switch (msg) {
|
if (msg.startsWith("/")) {
|
||||||
case "/list":
|
String[] s = msg.split("(@[A-Za-z0-9_]bot)?[\t \n]+", 2);
|
||||||
outbound(genOnlineStatus());
|
String command = s[0];
|
||||||
break;
|
@Nullable String arg = s.length == 2 ? s[1] : null;
|
||||||
case "/setpin":
|
switch (command) {
|
||||||
Message replyTo = update.message().replyToMessage();
|
case "/list" -> outbound(genOnlineStatus());
|
||||||
if (replyTo == null || replyTo.from() == null || replyTo.from().id() != BOT_ID) {
|
case "/setpin" -> {
|
||||||
outbound("must reply a message that from the bot");
|
boolean shouldApplyEdit = true;
|
||||||
} else {
|
boolean needUpdate = false;
|
||||||
int messageId = replyTo.messageId();
|
Message replyTo = update.message().replyToMessage();
|
||||||
ONLINE_STATUS_MESSAGE_ID = messageId > 0 ? messageId : ONLINE_STATUS_MESSAGE_ID;
|
if (replyTo == null) {
|
||||||
outbound("done");
|
if (arg == null || arg.length() == 0)
|
||||||
|
outbound("""
|
||||||
|
usage:
|
||||||
|
use "/setpin" reply a message that from the bot to set that message to pin-message,
|
||||||
|
or use "/setpin <note>" to update current pinned message.""");
|
||||||
|
} else if (replyTo.from() == null || replyTo.from().id() != BOT_ID || replyTo.messageId() <= 0) {
|
||||||
|
outbound("must reply a message that from the bot (or reply nothing).");
|
||||||
|
shouldApplyEdit = false;
|
||||||
|
} else {
|
||||||
|
readOldPinnedMessage(replyTo);
|
||||||
|
if (arg == null || arg.length() == 0) {
|
||||||
|
outbound("done");
|
||||||
|
}else {
|
||||||
|
ONLINE_STATUS_MESSAGE_ID = replyTo.messageId();
|
||||||
|
}
|
||||||
|
needUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldApplyEdit && arg != null && arg.length() > 0) {
|
||||||
|
outbound("done. old pinned note: \n" + pinNote);
|
||||||
|
pinNote = arg;
|
||||||
|
needUpdate = true;
|
||||||
|
}
|
||||||
|
if (needUpdate) updateOnlineStatus();
|
||||||
}
|
}
|
||||||
break;
|
case "/genpin" -> outbound(genPinMessage(),
|
||||||
case "/genpin":
|
|
||||||
outbound(genOnlineStatus(),
|
|
||||||
(sendMessage, sendResponse) -> {
|
(sendMessage, sendResponse) -> {
|
||||||
if (!sendResponse.isOk()) {
|
if (!sendResponse.isOk()) {
|
||||||
plugin.logger.error(String.format("sendMessage error %d: %s", sendResponse.errorCode(), sendResponse.description()));
|
plugin.logger.error(String.format("sendMessage error %d: %s", sendResponse.errorCode(), sendResponse.description()));
|
||||||
|
@ -125,7 +151,7 @@ public class TGBridge {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
tgInbound(update.message().from(), msg);
|
tgInbound(update.message().from(), msg);
|
||||||
} else if (update.message().sticker() != null) {
|
} else if (update.message().sticker() != null) {
|
||||||
|
@ -156,6 +182,37 @@ public class TGBridge {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean getPinnedMessage(){
|
||||||
|
|
||||||
|
try {
|
||||||
|
GetChatResponse response = bot.execute(new GetChat(CHAT_ID));
|
||||||
|
Message pinnedMessage = response.chat().pinnedMessage();
|
||||||
|
if(pinnedMessage.from().id()==BOT_ID){
|
||||||
|
readOldPinnedMessage(pinnedMessage);
|
||||||
|
updateOnlineStatus();
|
||||||
|
}
|
||||||
|
}catch (RuntimeException e){
|
||||||
|
plugin.logger.error("get group info failed.");
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readOldPinnedMessage(Message message){
|
||||||
|
ONLINE_STATUS_MESSAGE_ID = message.messageId();
|
||||||
|
String text = message.text();
|
||||||
|
String[] s = text.split(DIVIDER, 2);
|
||||||
|
pinNote = s.length == 2 ? s[1] : "(use \"/setpin\" <note> to set note here)";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String genPinMessage() {
|
||||||
|
String onlineStatus = genOnlineStatus();
|
||||||
|
if (pinNote != null && pinNote.length() > 1) {
|
||||||
|
return onlineStatus + "\n" + DIVIDER + pinNote;
|
||||||
|
} else {
|
||||||
|
return onlineStatus;
|
||||||
|
}
|
||||||
|
}
|
||||||
private String genOnlineStatus() {
|
private String genOnlineStatus() {
|
||||||
ArrayList<String> out = new ArrayList<>();
|
ArrayList<String> out = new ArrayList<>();
|
||||||
String fmt = server.getAllPlayers().size() > 1 ? "%d players are currently connected to the proxy." : "%d player is currently connected to the proxy.";
|
String fmt = server.getAllPlayers().size() > 1 ? "%d players are currently connected to the proxy." : "%d player is currently connected to the proxy.";
|
||||||
|
@ -320,7 +377,7 @@ public class TGBridge {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean updateOnlineStatus() {
|
protected boolean updateOnlineStatus() {
|
||||||
return editOnlineStatusMessage(genOnlineStatus());
|
return editOnlineStatusMessage(genPinMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean setOnlineStatusNotAvailable() {
|
protected boolean setOnlineStatusNotAvailable() {
|
||||||
|
|
Loading…
Reference in a new issue