Skip to content

Commit

Permalink
Merge pull request #18 from alt-art/fix-config-and-null-username
Browse files Browse the repository at this point in the history
Fix config and null username
  • Loading branch information
alt-art authored Jun 16, 2024
2 parents de9db12 + 9887b59 commit 7c00dc1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void onUpdateReceived(@NotNull Update update) {
Message message = update.getMessage();
String text = message.getText();
if (text != null && !text.startsWith("/") && TelegramBridge.config.sendToChat) {
String username = message.getFrom().getUserName();
String username = getUserName(message);
TelegramBridge.log.info("Telegram message received from " + username + ": " + text);
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.hasPermission(Permissions.RECEIVE.getString())) {
Expand Down Expand Up @@ -112,6 +112,24 @@ public void onUpdateReceived(@NotNull Update update) {
}
}

private static @NotNull String getUserName(Message message) {
String username = message.getFrom().getUserName();
if (username == null) {
String firstName = message.getFrom().getFirstName();
String lastName = message.getFrom().getLastName();
username = firstName;
if (lastName != null) {
username += " " + message.getFrom().getLastName();
}
}

if (username.trim().isEmpty()) {
username = "Anonymous";
}

return username;
}

@SuppressWarnings("deprecation")
private static TextComponent replyComponent(String username, String message, @Nullable String lang) {
HashMap<String, String> replyValues = makeMessageMap(username, shrinkReplyText(message));
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/org/altart/telegrambridge/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,15 @@ public static List<Chat> chatsFrom(List<Map<?, ?>> chats_map) {
List<Chat> chats = new ArrayList<>();
for (Map<?, ?> chat : chats_map) {
String id = (String) chat.get("id");
Object threadObj = chat.get("thread");
Integer thread = null;
if (chat.get("thread") != null) {
thread = Integer.valueOf(String.valueOf(chat.get("thread")));
if (threadObj != null) {
thread = Integer.valueOf(String.valueOf(threadObj));
}
Object pinnedMessageIdObj = chat.get("pinnedMessageId");
Integer pinnedMessageId = null;
if (chat.get("pinnedMessageId") != null) {
pinnedMessageId = Integer.valueOf(String.valueOf(chat.get("pinnedMessageId")));
if (pinnedMessageIdObj != null) {
pinnedMessageId = Integer.valueOf(String.valueOf(pinnedMessageIdObj));
}
chats.add(new Chat(id, thread, pinnedMessageId));
}
Expand All @@ -175,8 +177,12 @@ public static List<Map<String, String>> chatsToMaps(List<Chat> chats) {
for (Chat chat : chats) {
Map<String, String> chat_map = new HashMap<>();
chat_map.put("id", chat.id);
chat_map.put("thread", String.valueOf(chat.thread));
chat_map.put("pinnedMessageId", String.valueOf(chat.pinnedMessageId));
if (chat.thread != null) {
chat_map.put("thread", String.valueOf(chat.thread));
}
if (chat.pinnedMessageId != null) {
chat_map.put("pinnedMessageId", String.valueOf(chat.pinnedMessageId));
}
chats_map.add(chat_map);
}
return chats_map;
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/altart/telegrambridge/utils/Format.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package org.altart.telegrambridge.utils;

import org.altart.telegrambridge.TelegramBridge;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public class Format {
public static String string(String format, HashMap<String, String> values) {
public static String string(@NotNull String format, @NotNull HashMap<String, String> values) {
String result = format;
result = result.replace("\\n", "\n");
for (Map.Entry<String, String> entry : values.entrySet()) {
result = result.replace("%" + entry.getKey() + "%", entry.getValue());
String value = entry.getValue();
if (value == null) {
value = "";
}
result = result.replace("%" + entry.getKey() + "%", value);
}
return result;
}

public static String string(String format, String key, String value) {
public static String string(@NotNull String format, @NotNull String key, @NotNull String value) {
String result = format;
result = result.replace("\\n", "\n");
result = result.replace("%" + key + "%", value);
Expand Down

0 comments on commit 7c00dc1

Please sign in to comment.