Skip to content

Commit

Permalink
feat(DiscordSRVListener): sync town ranks to Discord roles
Browse files Browse the repository at this point in the history
  • Loading branch information
BrycensRanch committed Dec 7, 2024
1 parent a23be4c commit 4e05732
Showing 1 changed file with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

package me.romvnly.TownyPlus.listeners;

import github.scarsz.discordsrv.DiscordSRV;
import github.scarsz.discordsrv.api.ListenerPriority;
import github.scarsz.discordsrv.api.Subscribe;
import github.scarsz.discordsrv.api.events.DiscordReadyEvent;
Expand All @@ -19,16 +20,25 @@
import github.scarsz.discordsrv.dependencies.jda.api.events.guild.GuildUnavailableEvent;
import github.scarsz.discordsrv.dependencies.jda.api.events.message.MessageReceivedEvent;
import github.scarsz.discordsrv.dependencies.jda.api.hooks.ListenerAdapter;
import github.scarsz.discordsrv.dependencies.json.JSONObject;
import github.scarsz.discordsrv.util.DiscordUtil;
import me.romvnly.TownyPlus.TownyPlusMain;
import me.romvnly.TownyPlus.command.CommandManager;
import me.romvnly.TownyPlus.model.SavedTownData;
import me.romvnly.TownyPlus.util.Debug;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;

import org.bukkit.Bukkit;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.NotNull;

import com.palmergames.bukkit.towny.TownyAPI;

import java.sql.SQLException;
import java.util.UUID;

import javax.annotation.Nonnull;

public class DiscordSRVListener extends ListenerAdapter {
Expand All @@ -49,10 +59,68 @@ public void discordReadyEvent(DiscordReadyEvent event) {
// see https://ci.dv8tion.net/job/JDA/javadoc/ for JDA's javadoc
// see https://github.com/DV8FromTheWorld/JDA/wiki for JDA's wiki
}
public void onGuildUnavailable(@NotNull GuildUnavailableEvent event) {
@Subscribe(priority = ListenerPriority.MONITOR)
public void onGuildUnavailable(@SuppressWarnings("null") GuildUnavailableEvent event) {
plugin.logger.warn("Oh no " + event.getGuild().getName() + " went unavailable :(");
}
public void onMessageReceived(final MessageReceivedEvent event) {
public void assignTownRoles(final MessageReceivedEvent event) {
var linkedMinecraftAccountUUID = ((DiscordSRV) Bukkit.getPluginManager().getPlugin("DiscordSRV")).getAccountLinkManager().getUuid(event.getAuthor().getId());
Debug.log(linkedMinecraftAccountUUID.toString());
if (linkedMinecraftAccountUUID != null && !linkedMinecraftAccountUUID.equals(UUID.fromString("00000000-0000-0000-0000-000000000000"))) {
Debug.log("NOT NULL");
var resident = TownyAPI.getInstance().getResident(linkedMinecraftAccountUUID);
Debug.log(resident.getFormattedName());
var town = resident.getTownOrNull();
Debug.log(town.getFormattedName());
SavedTownData savedTownData;
try {
savedTownData = plugin.database.findTownByName(town.getName());
} catch (Exception e) {
e.printStackTrace();
savedTownData = null;
}
if (town != null && savedTownData != null) {
Debug.log("TOWN DISCORD DATA EXISTS");
var discordRolesJson = savedTownData.getTownDiscordRoles();
JSONObject discordRoles = new JSONObject(discordRolesJson);
var guild = event.getGuild();
Debug.log(discordRolesJson);
try {
String globalTownMayorRoleName = "Town Mayor";
String globalTownAssistantRoleName = "Town Assistant";
String globalTownResidentRoleName = "Town Resident";
guild.addRoleToMember(event.getAuthor().getId(), guild.getRolesByName(globalTownMayorRoleName, false).get(0)).complete();
guild.addRoleToMember(event.getAuthor().getId(), guild.getRolesByName(globalTownAssistantRoleName, false).get(0)).complete();
guild.addRoleToMember(event.getAuthor().getId(), guild.getRolesByName(globalTownResidentRoleName, false).get(0)).complete();

} catch(Exception e) {}
for (var rank : discordRoles.keySet()) {
Debug.log(rank);
String discordRoleId;
try {
discordRoleId = discordRoles.getString(rank);
} catch(Exception e) {
e.printStackTrace();
discordRoleId = null;
}
Debug.log(discordRoleId);
if (discordRoleId != null) {
try {
guild.addRoleToMember(event.getMember(), guild.getRoleById(discordRoleId)).complete();
} catch(Exception e) {
e.printStackTrace();
}

}
}


}
}

}
@Subscribe(priority = ListenerPriority.MONITOR)
public void onMessageReceived( final MessageReceivedEvent event) {
if (event.getAuthor().isBot()) {
Debug.log(Component.text("Author is bot").appendNewline().append(MiniMessage.miniMessage().deserialize("From type <type> <user> <msg>", Placeholder.unparsed("type", event.getChannelType().name()), Placeholder.unparsed("user", event.getAuthor().getAsTag()), Placeholder.unparsed("msg", event.getMessage().getContentDisplay()))));
return;
Expand All @@ -65,6 +133,12 @@ public void onMessageReceived(final MessageReceivedEvent event) {
Debug.log(Component.text("Not from type text").appendNewline().append(MiniMessage.miniMessage().deserialize("From type <type> <user> <msg>", Placeholder.unparsed("type", event.getChannelType().name()), Placeholder.unparsed("user", event.getAuthor().getAsTag()), Placeholder.unparsed("msg", event.getMessage().getContentDisplay()))));
return;
}
try {
assignTownRoles(event);

} catch(Exception e) {
e.printStackTrace();
}
// This checks for codes and generates appropriate channels and messages
// It was moved to a new class because it was getting too big
try {
Expand Down

0 comments on commit 4e05732

Please sign in to comment.