Skip to content

Commit

Permalink
Add ability for admins to send raw text json through bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrptonaught committed Apr 2, 2024
1 parent a040319 commit 93236a8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public class DiscordBridgeConfig extends AbstractConfigFile {
public long linkRoleID;
public long moderatorRoleID;

public long adminMessageRole;

public boolean canDiscordMSGCMDPing = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.MessageType;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
Expand Down Expand Up @@ -52,11 +53,14 @@ public void sendEmbed(String title, String msg, int hexColor) {
public void onMessageReceived(MessageReceivedEvent event) {
if (event != null && shouldRespondToMessage(event)) {
if (event.getMessage().getReferencedMessage() != null) {
Text message = FormatToMC.parseMessage(event.getMessage().getReferencedMessage(), Text.literal(" ┌──── ").formatted(Formatting.GRAY));
Text message = FormatToMC.parseMessage(event.getMessage().getReferencedMessage(), Text.literal(" ┌──── ").formatted(Formatting.GRAY), false);
server.getPlayerManager().broadcast(message, false);
}

Text message = FormatToMC.parseMessage(event.getMessage(), Text.literal("[Discord] ").formatted(Formatting.BLUE));
Role adminMessageRole = event.getGuild().getRoleById(DiscordBridgeMod.config().adminMessageRole);
boolean admin = event.getMember().getRoles().contains(adminMessageRole);

Text message = FormatToMC.parseMessage(event.getMessage(), Text.literal("[Discord] ").formatted(Formatting.BLUE), admin);
if (message != null)
server.getPlayerManager().broadcast(message, false);
}
Expand Down Expand Up @@ -94,7 +98,6 @@ private boolean shouldRespondToMessage(MessageReceivedEvent event) {
(isAllowedChannel(event.getChannel().getIdLong()));
}


public boolean isAllowedChannel(long channel) {
return getChannel() == channel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

public class FormatToMC {

public static Text parseMessage(Message discordMessage, MutableText prefix) {
public static Text parseMessage(Message discordMessage, MutableText prefix, boolean isAdmin) {
if(isAdmin && discordMessage.getContentRaw().startsWith("$PARSE="))
return parseAdminTextJson(discordMessage, prefix);

HashMap<String, String> replacementURLs = new HashMap<>();

for (MessageEmbed embed : discordMessage.getEmbeds())
Expand All @@ -28,7 +31,7 @@ public static Text parseMessage(Message discordMessage, MutableText prefix) {
replacementURLs.put("Sticker:" + stickerItem.getName(), stickerItem.getIcon().getUrl());


if (discordMessage.getContentDisplay().isEmpty() && replacementURLs.size() == 0) {
if (discordMessage.getContentDisplay().isEmpty() && replacementURLs.isEmpty()) {
return null;
}

Expand All @@ -38,7 +41,7 @@ public static Text parseMessage(Message discordMessage, MutableText prefix) {
message.append(parseText(str, replacementURLs));
}

if (replacementURLs.size() > 0) {
if (!replacementURLs.isEmpty()) {
message.append("{");

Iterator<String> urlIterator = replacementURLs.keySet().iterator();
Expand All @@ -52,6 +55,21 @@ public static Text parseMessage(Message discordMessage, MutableText prefix) {
return message;
}

private static Text parseAdminTextJson(Message discordMessage, MutableText prefix) {
MutableText message = Text.literal("").append(prefix).append(getAuthor(discordMessage));

MutableText parsed = null;
try {
parsed = Text.Serialization.fromLenientJson(discordMessage.getContentRaw().replaceFirst("\\$PARSE=", ""));
} catch (Exception e) {
e.printStackTrace();
}
if (parsed == null) message.append("PARSE ERROR");
else message.append(parsed);

return message;
}

private static Text getAuthor(Message discordMessage) {
int color = discordMessage.getMember() != null ? discordMessage.getMember().getColorRaw() : 0xffffff;
String author = discordMessage.getMember() != null ? discordMessage.getMember().getEffectiveName() : discordMessage.getAuthor().getEffectiveName();
Expand Down

0 comments on commit 93236a8

Please sign in to comment.