Skip to content

Commit

Permalink
linking system
Browse files Browse the repository at this point in the history
  • Loading branch information
Taah committed Feb 9, 2024
1 parent 7db66d0 commit 3ded799
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/java/dev/plex/futura/Futura.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.plex.futura;

import dev.plex.futura.bot.BotHandler;
import dev.plex.futura.bot.linking.LinkingManager;
import dev.plex.futura.bot.listener.LinkingListener;
import dev.plex.futura.config.Config;
import dev.plex.futura.listener.ChatListener;
import org.bstats.bukkit.Metrics;
Expand All @@ -13,6 +15,7 @@ public final class Futura extends JavaPlugin
private static Futura plugin;

private BotHandler botHandler;
private LinkingManager linkingManager;

public Config config;
public Config messages;
Expand All @@ -31,6 +34,7 @@ public void onLoad()
this.saveResource("messages.yml", false);
}
botHandler = new BotHandler();
linkingManager = new LinkingManager();
}

@Override
Expand All @@ -43,6 +47,7 @@ public void onEnable()
if (this.botHandler.ready())
{
this.botHandler.jda().addEventListener(chatListener);
this.botHandler.jda().addEventListener(new LinkingListener());
}

// Metrics @ https://bstats.org/plugin/bukkit/Futura/20848
Expand All @@ -63,4 +68,9 @@ public BotHandler botHandler()
{
return this.botHandler;
}

public LinkingManager linkingManager()
{
return this.linkingManager;
}
}
74 changes: 74 additions & 0 deletions src/main/java/dev/plex/futura/bot/linking/LinkingManager.java
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 src/main/java/dev/plex/futura/bot/listener/LinkingListener.java
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
}
}
12 changes: 12 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ discord:
#
linking:
enabled: false
# Time in seconds the code will be valid for until it expires and a new one will have to be requested for
expirationTime: 60
# You can leave this blank for no role to be given on linking
role: ""
# Roles sync if LuckPerms is on the server only
Expand All @@ -18,3 +20,13 @@ discord:
# luckpermsGroupName: "discordRoleId"
roles:
sync: false

database:
# sqlite, mysql
type: sqlite
auth:
user: ""
password: ""
host: ""
port: 3306
database: ""
7 changes: 6 additions & 1 deletion src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ joinServer: "**%player% has joined the server!"
leftServer: "**%player% has left the server!"

serverOffline: ":octagonal_sign: **Server is now offline!**"
serverOnline: ":white_check_mark: **Server is now online!**"
serverOnline: ":white_check_mark: **Server is now online!**"


linkingDisabled: "<red>Discord to Minecraft account linking is currently disabled."
accountLinkingInProgress: "<red>Can't request a new code currently, user has already requested a code. Please try again in a minute."
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 %expiration-time% seconds."

0 comments on commit 3ded799

Please sign in to comment.