Skip to content
Advertisement

Emojis and special characters in Discord webhook message from Java not working

So what I’m basically trying to accomplish is I want to copy a user’s message from one channel, and using a Webhook, I want to rewrite it out exactly as they input it in another channel. The problem is that emojis come out as ‘?’s, and many special characters (examples including £, é) completely break it.

My code looks something like this:

package uniqueimpact.discordbot;

import java.io.IOException;

import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

public class MessageEvent extends ListenerAdapter {
    private static final String WEBHOOK = "webhook-url";

    public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
        if (!event.getAuthor().isBot()) {
            String messageSent = event.getMessage().getContentRaw();
            String formattedMessage = "";
            for (int i = 0; i < messageSent.length(); i++) {
                char character = messageSent.charAt(i);
                switch (character) {
                    case '\':
                        formattedMessage += "\\";
                        break;
                    case '"':
                        formattedMessage += "\"";
                        break;
                    case 'n':
                        formattedMessage += "\n";
                        break;
                    default:
                        formattedMessage += character;
                }
            }

            String webhook = WEBHOOK;
            DiscordWebhook disWebhook = new DiscordWebhook(webhook);
            disWebhook.setContent(formattedMessage);

            try {
                disWebhook.execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

This code simply listens for a message, then formats it to escape backslashes, quotes and newlines, and then uses this code which I copied to send the message to a Webhook.

I’m aware that emojis and these special characters are a part of the extended Unicode character set, but I’m not sure what to do with this information. So if anyone knows how I can fix this, that would be very appreciated. 🙂

Advertisement

Answer

I am not sure you even need to escape your special characters, but that’s besides the point (Actually, my first try would be just to send the received String as is without ANY modifications). One of the simpler solutions is to convert your String into unicode sequences ‘U****’. In this case all your symbols (including emojis) should pass without a glitch. There is an Open Source java library MgntUtils that has a Utility that converts Strings to unicode sequence and vise versa:

result = "Hello World";
result = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result);
System.out.println(result);
result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString(result);
System.out.println(result);

The output of this code is:

u0048u0065u006cu006cu006fu0020u0057u006fu0072u006cu0064
Hello World

The library can be found at Maven Central or at Github It comes as maven artifact and with sources and javadoc

Here is javadoc for the class StringUnicodeEncoderDecoder

So, I suggest taking your incoming message and just convert it to Unicode sequences and send them over. The receiving side should display it already as symbols. BTW, the same tool can help you to diagnose the problem. You can see what you receive and decode it back to String.

Disclaimer: The library is written by me

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement