Skip to content

Commit

Permalink
feat: Add first implemention of link account feature
Browse files Browse the repository at this point in the history
  • Loading branch information
alt-art committed Nov 25, 2024
1 parent 0c70bfb commit 1dba06f
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/altart/telegrambridge/bot/TelegramBot.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<? extends Player> players = TelegramBridge.plugin.getServer().getOnlinePlayers();
sessions.entrySet().removeIf(entry -> players.stream().noneMatch(player -> player.getName().equals(entry.getKey())));
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/altart/telegrambridge/commands/LinkCommand.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
36 changes: 36 additions & 0 deletions src/main/java/org/altart/telegrambridge/database/SQLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ commands:
usage: /tbmention <username> <message>
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:
Expand All @@ -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
Expand Down

0 comments on commit 1dba06f

Please sign in to comment.