A non-comprehensive collection of common pitfalls/questions we see a lot when using Discord4J.
"What is a snowflake? How do I create one from an ID I got from Discord?"
Snowflakes are the format Discord uses for their IDs. Discord4J encodes this in the type located at
discord4j.common.util.Snowflake in v3.1 or
discord4j.core.object.util.Snowflake in v3.0 which you will see all over the library. This provides a way to convert back and forth between different representations of snowflakes and some utilities around them.
To obtain a
Snowflake from an ID you got from the Discord client, you can use one of its several static factory (
of()) methods. The most convenient for this case is either
of(String). There is no difference between these methods. For example, if my ID is
84766711735136256 I can do:
Snowflake.of(84766711735136256L). Take note of the
L at the end of the latter example. This is a long literal and could be required to tell Java that the number is a
"I want to get a channel, role, guild, etc..."
"...when handling an event."
If you have an
Event and want to get an entity related to that event, more than likely there is a path to get to that info from the event. For example,
MessageCreateEvent directly has
getGuild() and the channel can be obtained through the related message like
"...outside of an event handler."
If you don't have an
Event or there isn't a way to get the entity you want from the event, you can use the fact that all entities have a unique ID associated to them called a
Snowflake. You can obtain this ID in the Discord client and pass it to one of the many
getXById(Snowflake) methods of
"I can't send a message to a
Channel, what gives?"
Discord4J uses a hierarchy of types to separate the behavior of all of the different types of channels in Discord. Consider the fact that a method for sending messages doesn't make much sense for a
VoiceChannel. Here is an overview of this hierarchy (with some of the internal details removed):
In general, Discord4J uses the least specific type of channel it can. For example,
Message#getChannel() gets you a
MessageChannel which could be any of
PrivateChannel. So, what should you do if you know you have one of the more specific types? Cast! This can be done in Reactor with either the
cast(Class) operator. So, to get a channel which you know is a
TextChannel you could do
"What the heck is a Mono?"
Discord4J uses a library called Reactor to facilitate reactive programming in Java. Both the concepts and the library itself can be intimidating at first. As this is a very broad topic, we have split off this documentation into other pages and defer to Reactor docs. Please see our Reactive (Reactor) Tutorial and the awesome Reactor Reference Guide.
If you're trying to build a Minecraft plugin with Discord4J, you'll have a conflict with the netty version both provide. Discord4J uses newer netty methods that don't exist in Minecraft's version, therefore you must use a technique called "Shading" to make your plugin ship the latest netty.
This can be achieved through Maven or Gradle