Skip to content

Commit

Permalink
Handle newly_created gracefully (#2254)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Oct 4, 2022
1 parent dab2a86 commit 312f97a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Expand Up @@ -17,14 +17,16 @@
package net.dv8tion.jda.api.events.thread;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;

import javax.annotation.Nonnull;

/**
* This event is dispatched when a {@link ThreadChannel} that JDA didn't previously have access to (due to permissions) is now visible.
*
* For example, if the bot is given the {@link net.dv8tion.jda.api.Permission#ADMINISTRATOR} permission, any thread channels that the bot could not previously see would be "revealed".
* <p>For example, if the bot is given the {@link Permission#ADMINISTRATOR} permission, any thread channels that the bot could not previously see would be "revealed".
* This event is also used when the bot is added to a {@link ThreadChannel#isPublic() private} thread channel, revealing it through membership.
*
* @see ThreadHiddenEvent
*/
Expand Down
Expand Up @@ -18,6 +18,7 @@

import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.events.channel.ChannelCreateEvent;
import net.dv8tion.jda.api.events.thread.ThreadRevealedEvent;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.entities.EntityBuilder;
Expand All @@ -38,8 +39,16 @@ protected Long handleInternally(DataObject content)

try
{
//Prevent possible duplicate events or unexpected situations of threads being revealed
// after already being known due to permissions
if (api.getThreadChannelById(content.getUnsignedLong("id")) != null)
return null;
ThreadChannel thread = api.getEntityBuilder().createThreadChannel(content, guildId);
api.handleEvent(new ChannelCreateEvent(api, responseNumber, thread));

if (content.getBoolean("newly_created"))
api.handleEvent(new ChannelCreateEvent(api, responseNumber, thread));
else
api.handleEvent(new ThreadRevealedEvent(api, responseNumber, thread));
}
catch (IllegalArgumentException ex)
{
Expand Down

0 comments on commit 312f97a

Please sign in to comment.