diff --git a/README.md b/README.md index 83b599d..008a032 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ telegram to minecraft and vice versa. | telegrambridge.commands.config | Allows the user to configure the TelegramBridge | true | | telegrambridge.commands.config.default-language | Allows the user to set the language that will be used in telegram channel | op | | telegrambridge.commands.config.language | Allows the user to set your own language | true | +| telegrambridge.commands.link | Allows the user to link their telegram account to their minecraft account | true | ## Config diff --git a/src/main/java/org/altart/telegrambridge/TelegramBridge.java b/src/main/java/org/altart/telegrambridge/TelegramBridge.java index 0e47286..e806a7c 100644 --- a/src/main/java/org/altart/telegrambridge/TelegramBridge.java +++ b/src/main/java/org/altart/telegrambridge/TelegramBridge.java @@ -66,6 +66,7 @@ public void onEnable() { PluginCommand configCommand = Objects.requireNonNull(getCommand("tbconfig")); configCommand.setExecutor(new ConfigCommand()); configCommand.setTabCompleter(new ConfigTabCompletion()); + Objects.requireNonNull(getCommand("tblink")).setExecutor(new LinkCommand()); } catch (NullPointerException e) { log.severe("Error registering command: " + e.getMessage()); Arrays.stream(e.getStackTrace()).forEach(line -> TelegramBridge.log.severe(line.toString())); diff --git a/src/main/java/org/altart/telegrambridge/bot/TelegramBot.java b/src/main/java/org/altart/telegrambridge/bot/TelegramBot.java index 0afea2d..3eef688 100644 --- a/src/main/java/org/altart/telegrambridge/bot/TelegramBot.java +++ b/src/main/java/org/altart/telegrambridge/bot/TelegramBot.java @@ -1,12 +1,9 @@ package org.altart.telegrambridge.bot; +import org.altart.telegrambridge.bot.feature.*; import org.altart.telegrambridge.config.Config; import org.altart.telegrambridge.TelegramBridge; import org.altart.telegrambridge.bot.commands.*; -import org.altart.telegrambridge.bot.feature.MessageListener; -import org.altart.telegrambridge.bot.feature.PinMessage; -import org.altart.telegrambridge.bot.feature.SentMedia; -import org.altart.telegrambridge.bot.feature.UserAutocomplete; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -38,6 +35,7 @@ public class TelegramBot extends TelegramLongPollingBot { public UserAutocomplete userAutocompleteFeature = new UserAutocomplete(this); public MessageListener messageListenerFeature = new MessageListener(this); public final SentMedia sentMediaFeature = new SentMedia(this); + public LinkAccount linkAccountFeature = new LinkAccount(this); public TelegramBot(Plugin plugin) { super(TelegramBridge.config.botToken); @@ -54,6 +52,7 @@ public TelegramBot(Plugin plugin) { features.add(userAutocompleteFeature); features.add(messageListenerFeature); features.add(sentMediaFeature); + features.add(linkAccountFeature); } private void onCommand(String command_text, Message message) { diff --git a/src/main/java/org/altart/telegrambridge/bot/feature/LinkAccount.java b/src/main/java/org/altart/telegrambridge/bot/feature/LinkAccount.java new file mode 100644 index 0000000..7b51023 --- /dev/null +++ b/src/main/java/org/altart/telegrambridge/bot/feature/LinkAccount.java @@ -0,0 +1,47 @@ +package org.altart.telegrambridge.bot.feature; + +import org.altart.telegrambridge.TelegramBridge; +import org.altart.telegrambridge.bot.TelegramBot; +import org.altart.telegrambridge.bot.TelegramFeature; +import org.apache.commons.lang.RandomStringUtils; +import org.bukkit.entity.Player; +import org.telegram.telegrambots.meta.api.objects.Update; + +import java.util.Collection; +import java.util.HashMap; + +public class LinkAccount extends TelegramFeature { + private final HashMap sessions = new HashMap<>(); + + public LinkAccount(TelegramBot telegramBot) { + super(telegramBot); + } + + @Override + public void onUpdateReceived(Update update) { + if (update.hasMessage() && update.getMessage().hasText() && update.getMessage().getText().startsWith("#link-")) { + String code = update.getMessage().getText().substring(6); + Long telegram_id = update.getMessage().getFrom().getId(); + if (sessions.containsKey(code)) { + String nickname = sessions.get(code); + sessions.remove(code); + TelegramBridge.database.linkAccount(nickname, telegram_id); + telegramBot.reply("Account linked successfully!", update.getMessage().getChatId().toString(), update.getMessage().getMessageId()); + } else { + telegramBot.reply("Invalid code!", update.getMessage().getChatId().toString(), update.getMessage().getMessageId()); + } + } + } + + public String addSession(String nickname) { + purgeOldSessions(); + String code = RandomStringUtils.randomAlphanumeric(6).toUpperCase(); + sessions.put(code, nickname); + return code; + } + + private void purgeOldSessions() { + Collection players = TelegramBridge.plugin.getServer().getOnlinePlayers(); + sessions.entrySet().removeIf(entry -> players.stream().noneMatch(player -> player.getName().equals(entry.getKey()))); + } +} diff --git a/src/main/java/org/altart/telegrambridge/commands/LinkCommand.java b/src/main/java/org/altart/telegrambridge/commands/LinkCommand.java new file mode 100644 index 0000000..17c0f2f --- /dev/null +++ b/src/main/java/org/altart/telegrambridge/commands/LinkCommand.java @@ -0,0 +1,16 @@ +package org.altart.telegrambridge.commands; + +import org.altart.telegrambridge.TelegramBridge; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +public class LinkCommand implements CommandExecutor { + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command _command, @NotNull String _label, String[] _args) { + String code = TelegramBridge.telegramBot.linkAccountFeature.addSession(sender.getName()); + sender.sendMessage("Please send the following code to the telegram chat: #link-" + code); + return true; + } +} diff --git a/src/main/java/org/altart/telegrambridge/database/SQLite.java b/src/main/java/org/altart/telegrambridge/database/SQLite.java index a4d0c74..88b5f4f 100644 --- a/src/main/java/org/altart/telegrambridge/database/SQLite.java +++ b/src/main/java/org/altart/telegrambridge/database/SQLite.java @@ -24,6 +24,7 @@ public SQLite() { private void init() { try { statement.execute("CREATE TABLE IF NOT EXISTS players (uuid TEXT PRIMARY KEY, lang TEXT)"); + statement.execute("CREATE TABLE IF NOT EXISTS linked_accounts (nickname TEXT PRIMARY KEY NOT NULL UNIQUE, telegram_id INTEGER NOT NULL)"); } catch (SQLException e) { TelegramBridge.log.severe(e.getMessage()); Arrays.stream(e.getStackTrace()).sequential().forEach(line -> TelegramBridge.log.severe(line.toString())); @@ -58,6 +59,41 @@ public void setLang(UUID uuid, String lang) { } } + public void linkAccount(String nickname, Long telegramId) { + try { + PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO linked_accounts (nickname, telegram_id) VALUES (?, ?)"); + preparedStatement.setString(1, nickname); + preparedStatement.setLong(2, telegramId); + preparedStatement.executeUpdate(); + } catch (SQLException e) { + TelegramBridge.log.severe(e.getMessage()); + Arrays.stream(e.getStackTrace()).sequential().forEach(line -> TelegramBridge.log.severe(line.toString())); + } + } + + public void removeAccount(String nickname) { + try { + PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM linked_accounts WHERE nickname = ?"); + preparedStatement.setString(1, nickname); + preparedStatement.executeUpdate(); + } catch (SQLException e) { + TelegramBridge.log.severe(e.getMessage()); + Arrays.stream(e.getStackTrace()).sequential().forEach(line -> TelegramBridge.log.severe(line.toString())); + } + } + + public long getTelegramId(String nickname) { + try { + PreparedStatement preparedStatement = connection.prepareStatement("SELECT telegram_id FROM linked_accounts WHERE nickname = ?"); + preparedStatement.setString(1, nickname); + return preparedStatement.executeQuery().getLong("telegram_id"); + } catch (SQLException e) { + TelegramBridge.log.severe(e.getMessage()); + Arrays.stream(e.getStackTrace()).sequential().forEach(line -> TelegramBridge.log.severe(line.toString())); + return -1; + } + } + public void close() { try { connection.close(); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 935421d..e22f5d3 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -25,6 +25,10 @@ commands: usage: /tbmention permission: telegrambridge.commands.mention aliases: [tbm] + tblink: + description: Link your telegram account to your minecraft account + usage: /tblink + permission: telegrambridge.commands.link permissions: telegrambridge.commands.reload: @@ -45,6 +49,9 @@ permissions: telegrambridge.commands.mention: description: Allows the user to mention a user in a telegram chat default: true + telegrambridge.commands.link: + description: Allows the user to link their telegram account to their minecraft account + default: true telegrambridge.send: description: Allows the user to send messages to telegram chat default: true