-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add first implemention of link account feature
- Loading branch information
Showing
7 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/org/altart/telegrambridge/bot/feature/LinkAccount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
src/main/java/org/altart/telegrambridge/commands/LinkCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters