JDA icon indicating copy to clipboard operation
JDA copied to clipboard

Guild object on MessageReceivedEvent shouldn't be Nonnull

Open tancred423 opened this issue 5 years ago • 3 comments

General Troubleshooting

  • [x] I have checked for similar issues.
  • [x] I have updated to the latest JDA version.
  • [x] I have checked the branches or the maintainers' PRs for upcoming bug fixes.

Bug Report

The guild object of the MessageReceivedEvent is set to Nonnull, even though it could be a private message. Same story for the guild object in the message object of the MessageReceivedEvent. If that occurs, you will receive an exception.

Expected Behavior

I think it should be nullable as the Discord API message object states guild_id to be nullable.

Code Example or Reproduction Steps

class MessageReceivedListener : ListenerAdapter() {
    override fun onMessageReceived(@Nonnull event: MessageReceivedEvent) {
        println(event.guild.id)
    }
}

Then DM the bot.

Code for JDABuilder or DefaultShardManagerBuilder Used

N/A

Exception or Error

java.lang.IllegalStateException: This message event did not happen in a text channel

tancred423 avatar Jun 13 '21 09:06 tancred423

getGuild never returns null. It instead will throw an exception if the event did not happen in a text channel as documented. So the @Nonnull annotation is correct.

gpluscb avatar Jun 13 '21 11:06 gpluscb

Use MessageReceivedEvent#isFromGuild to avoid this exception

arynxd avatar Jun 13 '21 12:06 arynxd

This is not a bug.

MinnDevelopment avatar Jun 13 '21 13:06 MinnDevelopment