Skip to content

Commit

Permalink
refactor: Make a function to get username
Browse files Browse the repository at this point in the history
  • Loading branch information
alt-art committed Jun 16, 2024
1 parent 86987dd commit 9887b59
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +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();
if (username == null) {
username = String.format("%s %s", message.getFrom().getFirstName(), message.getFrom().getLastName());
}
if (username.trim().isEmpty()) {
username = "Anonymous";
}
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 @@ -118,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
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 9887b59

Please sign in to comment.