-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Taah
committed
Feb 9, 2024
1 parent
7db66d0
commit 3ded799
Showing
5 changed files
with
134 additions
and
1 deletion.
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
74 changes: 74 additions & 0 deletions
74
src/main/java/dev/plex/futura/bot/linking/LinkingManager.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,74 @@ | ||
package dev.plex.futura.bot.linking; | ||
|
||
import com.google.common.collect.Maps; | ||
import dev.plex.futura.Futura; | ||
import net.kyori.adventure.text.minimessage.MiniMessage; | ||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
/** | ||
* @author Taah | ||
* @since 6:51 PM [02-08-2024] | ||
*/ | ||
public class LinkingManager | ||
{ | ||
private static final Map<UUID, String> CODES = Maps.newHashMap(); | ||
private final boolean enabled; | ||
|
||
public LinkingManager() | ||
{ | ||
this.enabled = Futura.plugin().config.getBoolean("discord.linking.enabled", false); | ||
} | ||
|
||
public void requestCode(Player player) | ||
{ | ||
if (!Futura.plugin().botHandler().ready()) | ||
{ | ||
player.sendMessage("<red>The bot is not connected / online. Please contact an administrator."); | ||
return; | ||
} | ||
if (!this.enabled) | ||
{ | ||
player.sendMessage(MiniMessage.miniMessage().deserialize(Futura.plugin().messages.getString("linkingDisabled", "<red>Discord to Minecraft account linking is currently disabled."))); | ||
return; | ||
} | ||
|
||
//TODO: Check if user is already linked on cache loaded from database | ||
|
||
final UUID uuid = player.getUniqueId(); | ||
|
||
if (CODES.containsKey(uuid)) | ||
{ | ||
player.sendMessage(MiniMessage.miniMessage().deserialize(Futura.plugin().messages.getString("accountLinkingInProgress", "<red>Can't request a new code currently, user has already requested a code. Please try again in a minute."))); | ||
return; | ||
} | ||
|
||
final String code = RandomStringUtils.randomAlphanumeric(8).toLowerCase(); | ||
CODES.put(uuid, code); | ||
|
||
final int expirationTime = Futura.plugin().config.getInt("discord.linking.expirationTime", 60); | ||
final String codeRequested = Futura.plugin().messages.getString("codeRequested", "<green>The code you have requested is %code%. Please do not share this. In order to link your account, message the %bot-name% bot with your code. This code will expire in %expirationTime% seconds.") | ||
.replace("%code%", code) | ||
.replace("%bot-name%", Futura.plugin().botHandler().jda().getSelfUser().getName()) | ||
.replace("%expiration-time%", String.valueOf(expirationTime)); | ||
|
||
player.sendMessage(MiniMessage.miniMessage().deserialize(codeRequested)); | ||
Futura.plugin().getServer().getScheduler().runTaskLater(Futura.plugin(), () -> | ||
{ | ||
CODES.remove(uuid); | ||
}, 20L * expirationTime); | ||
} | ||
|
||
public UUID matchCode(String code) | ||
{ | ||
final Map.Entry<UUID, String> uuidStringEntry = CODES.entrySet().stream().filter(entry -> entry.getValue().equals(code)).findFirst().orElse(null); | ||
if (uuidStringEntry == null) | ||
{ | ||
return null; | ||
} | ||
return uuidStringEntry.getKey(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/dev/plex/futura/bot/listener/LinkingListener.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,32 @@ | ||
package dev.plex.futura.bot.listener; | ||
|
||
import dev.plex.futura.Futura; | ||
import net.dv8tion.jda.api.entities.channel.ChannelType; | ||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
import net.dv8tion.jda.api.hooks.ListenerAdapter; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* @author Taah | ||
* @since 7:05 PM [02-08-2024] | ||
*/ | ||
public class LinkingListener extends ListenerAdapter | ||
{ | ||
@Override | ||
public void onMessageReceived(@NotNull MessageReceivedEvent event) | ||
{ | ||
if (event.getAuthor().isBot()) return; | ||
if (event.getChannelType() != ChannelType.PRIVATE) return; | ||
final String messageContent = event.getMessage().getContentRaw().toLowerCase(); | ||
final UUID uuid = Futura.plugin().linkingManager().matchCode(messageContent); | ||
if (uuid == null) | ||
{ | ||
// Invalid Code | ||
return; | ||
} | ||
|
||
//TODO: Code was valid, link user, store user in cache and future database | ||
} | ||
} |
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