From 2578866c83ddba6b0440a0278349b9addd47d69f Mon Sep 17 00:00:00 2001 From: svinerus Date: Sat, 19 Sep 2020 03:13:53 +0300 Subject: [PATCH] =?UTF-8?q?version=203.1=20add=20replay=20display=20add=20?= =?UTF-8?q?world=20near=20name=20in=20online=20add=20=D0=BA=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D1=8B=D0=BB=D0=B8=20=D0=B5=D0=B1=D0=B0=D0=BD=D1=8B=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- src/main/java/ua/kpi/mc/mctotg/Bot.java | 14 ++++--- src/main/java/ua/kpi/mc/mctotg/Core.java | 35 +++++++---------- src/main/java/ua/kpi/mc/mctotg/Utils.java | 47 +++++++++++++++-------- src/main/resources/config.yml | 3 +- 5 files changed, 56 insertions(+), 45 deletions(-) diff --git a/pom.xml b/pom.xml index 9b2bfdd..2146748 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ ua.kpi.mc mctotg - 3.0 + 3.1 jar Mc2TgBridge diff --git a/src/main/java/ua/kpi/mc/mctotg/Bot.java b/src/main/java/ua/kpi/mc/mctotg/Bot.java index 40e8e3d..a5a7fda 100644 --- a/src/main/java/ua/kpi/mc/mctotg/Bot.java +++ b/src/main/java/ua/kpi/mc/mctotg/Bot.java @@ -4,6 +4,7 @@ import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.UpdatesListener; import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.GetMe; import com.pengrad.telegrambot.request.SendMessage; @@ -13,12 +14,14 @@ import java.io.IOException; public class Bot { + private final Integer botId; TelegramBot bot; Long chatId; public Bot(String token, Long chatId) { bot = new TelegramBot(token); this.chatId = chatId; + this.botId = Integer.parseInt(token.split(":")[0]); bot.setUpdatesListener(updates -> { for (Update update: updates) { @@ -33,10 +36,6 @@ public void stop() { bot.removeGetUpdatesListener(); } - public void send_msg(String text) { - send_msg(text, null); - } - public void send_msg(String text, Integer replyTo) { SendMessage request = new SendMessage(chatId, text).parseMode(ParseMode.HTML); if (replyTo != null) @@ -50,11 +49,16 @@ public void onResponse(SendMessage sendMessage, SendResponse sendResponse) {} public void onFailure(SendMessage sendMessage, IOException e) {e.printStackTrace();} }); } + public void send_msg(String text) { + send_msg(text, null); + } + public GetMeResponse getMe() { return bot.execute(new GetMe()); } + public boolean isMe(User user) { return user.id().equals(botId); } void processUpdate(Update update) { @@ -83,8 +87,6 @@ else if (message.isAdmin()) Utils.runCommand(command); else send_msg("Только для админов"); - - } } diff --git a/src/main/java/ua/kpi/mc/mctotg/Core.java b/src/main/java/ua/kpi/mc/mctotg/Core.java index 002179d..11c0085 100644 --- a/src/main/java/ua/kpi/mc/mctotg/Core.java +++ b/src/main/java/ua/kpi/mc/mctotg/Core.java @@ -3,10 +3,8 @@ import com.pengrad.telegrambot.Callback; import com.pengrad.telegrambot.request.GetChat; import com.pengrad.telegrambot.request.SetChatDescription; -import com.pengrad.telegrambot.response.BaseResponse; import com.pengrad.telegrambot.response.GetChatResponse; import org.bukkit.Bukkit; -import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import java.io.IOException; @@ -18,17 +16,20 @@ class Core { static void TgToMc(MyMessage message) { String cmd = "{\"text\":\"[" + message.senderName + "] \",\"color\":\"aqua\"," + - "\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"/tg " + message.id + " \"}," + + "\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"/tg " + message.originalMessage.messageId() + " \"}," + "\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Нажми, что бы ответить\"}}," + + (message.reply == null ? "" : "{\"text\":\"[реплай] \",\"color\":\"gold\"," + + "\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + new MyMessage(message.originalMessage.replyToMessage()).getLink() + "\"}," + + "\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"" + message.reply + "\"}},") + (message.media == null ? "" : "{\"text\":\"[" + message.media + "] \",\"color\":\"gold\"," + - "\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + message.link + "\"}," + + "\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + message.getLink() + "\"}," + "\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Нажми, что бы открыть телегу\"}},") + "{\"text\":\"" + message.text + "\",\"color\":\"white\"," + - "\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + message.link + "\"}," + + "\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + message.getLink() + "\"}," + "\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Нажми, что бы открыть телегу\"}}]"; Utils.tellraw(cmd); - Utils.CACHE.put(message.id, message); + Utils.CACHE.put(message.originalMessage.messageId(), message); } @@ -37,8 +38,7 @@ static void McToTg(Player sender, String text, Integer replyTo) { return; String senderName = sender.getName(); - String world = sender.getWorld().getName(); - world = Main.config.worldNamesDict.getOrDefault(world, world); + String world = Utils.getWorldIcon(sender.getWorld()); String msg = world + "" + senderName + "" + ": " + text; Main.bot.send_msg(msg, replyTo); @@ -53,7 +53,7 @@ static void McToTgReplay(Player sender, String text, Integer replyTo) { String cmd = "{\"text\":\"" + sender.getDisplayName() + " \",\"color\":\"white\"}," + "{\"text\":\"[в ответ" + replyToName + "] \",\"color\":\"gold\"" + - (cachedMessage == null ? "" : ",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + cachedMessage.link + "\"}," + + (cachedMessage == null ? "" : ",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + cachedMessage.getLink() + "\"}," + "\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"" + cachedMessage.text +"\"}") + "}," + "{\"text\":\"" + text + "\",\"color\":\"white\"}]"; @@ -68,7 +68,9 @@ static String getOnline() { if (players.size() == 0) return "Никого.."; - return players.size() + " тел. \n" + players.stream().map(HumanEntity::getName).collect(Collectors.joining(", ")); + return players.size() + " тел. \n" + players.stream() + .map(p -> Utils.getWorldIcon(p.getWorld()) + p.getName()) + .collect(Collectors.joining("\n")); } static void UpdateDescription(String online) { @@ -82,18 +84,7 @@ public void onResponse(GetChat getChat, GetChatResponse response) { description = description.split("Online")[0]; description += "Online: " + online; - SetChatDescription request = new SetChatDescription(Main.bot.chatId, description); - Main.bot.bot.execute(request, new Callback() { - - @Override - public void onResponse(SetChatDescription setChatDescription, BaseResponse baseResponse) { - } - - @Override - public void onFailure(SetChatDescription setChatDescription, IOException e) { - e.printStackTrace(); - } - }); + Main.bot.bot.execute(new SetChatDescription(Main.bot.chatId, description)); } @Override diff --git a/src/main/java/ua/kpi/mc/mctotg/Utils.java b/src/main/java/ua/kpi/mc/mctotg/Utils.java index 0d6926b..0271d3f 100644 --- a/src/main/java/ua/kpi/mc/mctotg/Utils.java +++ b/src/main/java/ua/kpi/mc/mctotg/Utils.java @@ -1,11 +1,11 @@ package ua.kpi.mc.mctotg; -import com.pengrad.telegrambot.model.Chat; import com.pengrad.telegrambot.model.ChatMember; import com.pengrad.telegrambot.model.Message; import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.request.GetChatMember; import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.scheduler.BukkitRunnable; import java.util.LinkedHashMap; @@ -30,12 +30,22 @@ public void run() { public static String replaceNonMinecraftSymbols(String text) { + if (text == null) return ""; return text .replaceAll("[^\\x00-\\x7Fа-яА-ЯёЁіІїЇ]", "✭") .replaceAll("\n", " ") .replaceAll("\"", "'"); } + + public static String getWorldIcon(World world) { + String worldName = world.getName(); + return Main.config.worldNamesDict.getOrDefault(worldName, + Main.config.worldNamesDict.getOrDefault("default", worldName) + ); + } + + } @@ -50,26 +60,20 @@ class MaxSizeHashMap extends LinkedHashMap { class MyMessage { - public Integer id; - public User sender; - public Chat chat; + public Message originalMessage; + public String text = ""; public String senderName; public String media; - public String link; + public String reply; public MyMessage(Message message) { - id = message.messageId(); - sender = message.from(); - chat = message.chat(); - + originalMessage = message; + senderName = getSenderName(message.from()); - senderName = getSenderName(sender); - link = "https://t.me/" + chat.username() + "/" + id; - - if (message.text() != null) + if (message.text() != null) { text = Utils.replaceNonMinecraftSymbols(message.text()); - else { + } else { if (message.caption() != null) text = Utils.replaceNonMinecraftSymbols(message.caption()); if (message.audio() != null) media = "аудио"; @@ -83,15 +87,28 @@ public MyMessage(Message message) { else media = "медиа"; } + if (message.replyToMessage() != null) { + MyMessage replyMessage = new MyMessage(message.replyToMessage()); + + if (Main.bot.isMe(replyMessage.originalMessage.from())) + reply = replyMessage.text; + else + reply = replyMessage.senderName + ": " + + (replyMessage.media == null ? "" : "[" + replyMessage.media + "] ") + + replyMessage.text; + } + } + public String getLink() { return "https://t.me/" + originalMessage.chat().username() + "/" + originalMessage.messageId(); } + public boolean isCommand() { return text != null && text.startsWith("/"); } public String getCommand() { return text.substring(1); } public boolean isAdmin() { ChatMember.Status status = Main.bot.bot.execute( - new GetChatMember(chat.id(), sender.id()) + new GetChatMember(originalMessage.chat().id(), originalMessage.from().id()) ).chatMember().status(); return status == ChatMember.Status.administrator || status == ChatMember.Status.creator; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b2d7e33..1ee7ce1 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -5,4 +5,5 @@ msgStartWith: '' worldNamesDict: world: ' ⛏' world_nether: ' 🌋' - world_the_end: ' 🔚' \ No newline at end of file + world_the_end: ' 🔚' + deafult: ' ' # if nothing else \ No newline at end of file