Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle disabled intent in setEnabledIntents #2767

Merged
merged 3 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/main/java/net/dv8tion/jda/api/JDABuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ public JDABuilder setDisabledIntents(@Nullable Collection<GatewayIntent> intents
{
this.intents = GatewayIntent.ALL_INTENTS;
if (intents != null)
this.intents &= ~GatewayIntent.getRaw(intents);
this.intents = 1 | (GatewayIntent.ALL_INTENTS & ~GatewayIntent.getRaw(intents));
return this;
}

Expand Down Expand Up @@ -1646,7 +1646,7 @@ public JDABuilder setEnabledIntents(@Nonnull GatewayIntent intent, @Nonnull Gate
Checks.notNull(intent, "Intents");
Checks.noneNull(intents, "Intents");
EnumSet<GatewayIntent> set = EnumSet.of(intent, intents);
return setDisabledIntents(EnumSet.complementOf(set));
return setEnabledIntents(set);
}

/**
Expand All @@ -1673,11 +1673,9 @@ public JDABuilder setEnabledIntents(@Nonnull GatewayIntent intent, @Nonnull Gate
public JDABuilder setEnabledIntents(@Nullable Collection<GatewayIntent> intents)
{
if (intents == null || intents.isEmpty())
setDisabledIntents(EnumSet.allOf(GatewayIntent.class));
else if (intents instanceof EnumSet)
setDisabledIntents(EnumSet.complementOf((EnumSet<GatewayIntent>) intents));
this.intents = 1;
else
setDisabledIntents(EnumSet.complementOf(EnumSet.copyOf(intents)));
this.intents = 1 | GatewayIntent.getRaw(intents);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2018,7 +2018,7 @@ public DefaultShardManagerBuilder setDisabledIntents(@Nullable Collection<Gatewa
{
this.intents = GatewayIntent.ALL_INTENTS;
if (intents != null)
this.intents &= ~GatewayIntent.getRaw(intents);
this.intents = 1 | ~GatewayIntent.getRaw(intents);
return this;
}

Expand Down Expand Up @@ -2110,7 +2110,7 @@ public DefaultShardManagerBuilder setEnabledIntents(@Nonnull GatewayIntent inten
Checks.notNull(intent, "Intent");
Checks.noneNull(intents, "Intent");
EnumSet<GatewayIntent> set = EnumSet.of(intent, intents);
return setDisabledIntents(EnumSet.complementOf(set));
return setEnabledIntents(set);
}

/**
Expand All @@ -2137,11 +2137,9 @@ public DefaultShardManagerBuilder setEnabledIntents(@Nonnull GatewayIntent inten
public DefaultShardManagerBuilder setEnabledIntents(@Nullable Collection<GatewayIntent> intents)
{
if (intents == null || intents.isEmpty())
setDisabledIntents(EnumSet.allOf(GatewayIntent.class));
else if (intents instanceof EnumSet)
setDisabledIntents(EnumSet.complementOf((EnumSet<GatewayIntent>) intents));
this.intents = 1;
else
setDisabledIntents(EnumSet.complementOf(EnumSet.copyOf(intents)));
this.intents = 1 | GatewayIntent.getRaw(intents);
return this;
}

Expand Down
47 changes: 41 additions & 6 deletions src/test/java/net/dv8tion/jda/test/JDABuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ void testRequiredIntentForCacheFlagMissing(CacheFlag cacheFlag)
}
}


@ParameterizedTest
@EnumSource(CacheFlag.class)
void testRequiredIntentForCacheFlagEnabled(CacheFlag cacheFlag)
Expand All @@ -113,6 +112,14 @@ void testRequiredIntentForCacheFlagEnabled(CacheFlag cacheFlag)
builder.enableCache(cacheFlag);

assertThatNoException().isThrownBy(builder::checkIntents);

builder = new TestJDABuilder(0);
builder.applyIntents();
if (requiredIntent != null)
builder.setEnabledIntents(requiredIntent);
builder.enableCache(cacheFlag);

assertThatNoException().isThrownBy(builder::checkIntents);
}

@Test
Expand Down Expand Up @@ -141,19 +148,40 @@ void testChunkingWithMissingIntent()
assertThat(logs).contains("Member chunking is disabled due to missing GUILD_MEMBERS intent.");
}

@Test
@EnumSource
@ParameterizedTest
@SuppressWarnings("deprecation")
void testDeprecatedIntentDoesNotDisableCache()
void testDeprecatedIntentDoesNotDisableCache(IntentTestCase testCase)
{
TestJDABuilder builder = new TestJDABuilder(GatewayIntent.GUILD_EMOJIS_AND_STICKERS.getRawValue());
builder.applyIntents();
TestJDABuilder builder;

switch (testCase)
{
case PASSED_TO_FACTORY:
builder = new TestJDABuilder(GatewayIntent.GUILD_EMOJIS_AND_STICKERS.getRawValue());
builder.applyIntents();
break;
case PASSED_TO_RELATIVE:
builder = new TestJDABuilder(0);
builder.applyLight();
builder.enableIntents(GatewayIntent.GUILD_EMOJIS_AND_STICKERS);
builder.enableCache(CacheFlag.EMOJI, CacheFlag.STICKER);
break;
case PASSED_TO_SETTER:
builder = new TestJDABuilder(0);
builder.applyLight();
builder.setEnabledIntents(GatewayIntent.GUILD_EMOJIS_AND_STICKERS);
builder.enableCache(CacheFlag.EMOJI, CacheFlag.STICKER);
break;
default:
throw new AssertionError("Unexpected test case " + testCase);
}

List<String> logs = captureLogging(() ->
assertThatNoException().isThrownBy(builder::checkIntents)
);

assertThat(logs)
.isNotEmpty()
.noneMatch(log -> log.contains("CacheFlag." + CacheFlag.EMOJI))
.noneMatch(log -> log.contains("CacheFlag." + CacheFlag.STICKER));
}
Expand Down Expand Up @@ -208,4 +236,11 @@ public void checkIntents()
super.checkIntents();
}
}

enum IntentTestCase
{
PASSED_TO_FACTORY,
PASSED_TO_RELATIVE,
PASSED_TO_SETTER;
}
}