Skip to content

Commit

Permalink
version 3.1
Browse files Browse the repository at this point in the history
add replay display
add world near name in online
add костыли ебаные
  • Loading branch information
SvineruS committed Sep 19, 2020
1 parent 9920f7d commit 2578866
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 45 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ua.kpi.mc</groupId>
<artifactId>mctotg</artifactId>
<version>3.0</version>
<version>3.1</version>
<packaging>jar</packaging>

<name>Mc2TgBridge</name>
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/ua/kpi/mc/mctotg/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -83,8 +87,6 @@ else if (message.isAdmin())
Utils.runCommand(command);
else
send_msg("Только для админов");


}

}
35 changes: 13 additions & 22 deletions src/main/java/ua/kpi/mc/mctotg/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}


Expand All @@ -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 + "<b>" + senderName + "</b>" + ": " + text;
Main.bot.send_msg(msg, replyTo);
Expand All @@ -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\"}]";

Expand All @@ -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) {
Expand All @@ -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<SetChatDescription, BaseResponse>() {

@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
Expand Down
47 changes: 32 additions & 15 deletions src/main/java/ua/kpi/mc/mctotg/Utils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
);
}


}


Expand All @@ -50,26 +60,20 @@ class MaxSizeHashMap<K, V> extends LinkedHashMap<K, V> {

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 = "аудио";
Expand All @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ msgStartWith: ''
worldNamesDict:
world: ''
world_nether: ' 🌋'
world_the_end: ' 🔚'
world_the_end: ' 🔚'
deafult: ' ' # if nothing else

0 comments on commit 2578866

Please sign in to comment.