Skip to content

Commit

Permalink
Merge branch 'main' into ban-sync-role
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinty1 authored Feb 3, 2025
2 parents 87a16e0 + 785aafb commit db2bc19
Show file tree
Hide file tree
Showing 68 changed files with 1,145 additions and 501 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ jobs:
run: ./gradlew build --no-daemon
env:
DISCORDSRV_AUTOTEST_BOT_TOKEN: ${{ secrets.DISCORDSRV_AUTOTEST_BOT_TOKEN }}
DISCORDSRV_AUTOTEST_CHANNEL_ID: ${{ secrets.DISCORDSRV_AUTOTEST_CHANNEL_ID }}
DISCORDSRV_AUTOTEST_TEXT_ID: ${{ secrets.DISCORDSRV_AUTOTEST_TEXT_ID }}
DISCORDSRV_AUTOTEST_NEWS_ID: ${{ secrets.DISCORDSRV_AUTOTEST_NEWS_ID }}
DISCORDSRV_AUTOTEST_FORUM_ID: ${{ secrets.DISCORDSRV_AUTOTEST_FORUM_ID }}
DISCORDSRV_AUTOTEST_VOICE_ID: ${{ secrets.DISCORDSRV_AUTOTEST_VOICE_ID }}
DISCORDSRV_AUTOTEST_MEDIA_ID: ${{ secrets.DISCORDSRV_AUTOTEST_MEDIA_ID }}
DISCORDSRV_AUTOTEST_STAGE_ID: ${{ secrets.DISCORDSRV_AUTOTEST_STAGE_ID }}
- name: Upload Build Artifacts
uses: actions/upload-artifact@v4
with:
Expand Down
8 changes: 8 additions & 0 deletions api/src/main/java/com/discordsrv/api/discord/DiscordAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ public interface DiscordAPI {
@Nullable
DiscordForumChannel getForumChannelById(long id);

/**
* Gets a Discord media channel by id, the provided entity should not be stored for long periods of time.
* @param id the id for the text channel
* @return the media channel
*/
@Nullable
DiscordMediaChannel getMediaChannelById(long id);

/**
* Gets a Discord voice channel by id, the provided entity should be stored for long periods of time.
* @param id the id for the voice channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,4 @@ public interface DiscordConnectionDetails {
*/
boolean requestCacheFlag(@NotNull DiscordCacheFlag cacheFlag, @NotNull DiscordCacheFlag... cacheFlags);

/**
* Requests that the provided {@link DiscordMemberCachePolicy}s be passed to the Discord connection.
*
* @param memberCachePolicy the first member cache policy
* @param memberCachePolicies more member cache policies
* @return {@code true} if the Discord connection is yet to be created and the intent will become active once it is
*/
boolean requestMemberCachePolicy(@NotNull DiscordMemberCachePolicy memberCachePolicy, @NotNull DiscordMemberCachePolicy... memberCachePolicies);

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum DiscordChannelType implements JDAEntity<ChannelType> {
GROUP(ChannelType.GROUP),
CATEGORY(ChannelType.CATEGORY),
FORUM(ChannelType.FORUM),
MEDIA(ChannelType.MEDIA),
NEWS(ChannelType.NEWS),
STAGE(ChannelType.STAGE),
GUILD_NEWS_THREAD(ChannelType.GUILD_NEWS_THREAD),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,14 @@
* SOFTWARE.
*/

package com.discordsrv.api.discord.connection.details;
package com.discordsrv.api.discord.entity.channel;

import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.api.discord.entity.guild.DiscordGuildMember;
import com.discordsrv.api.profile.IProfile;
import com.discordsrv.api.discord.entity.JDAEntity;
import com.discordsrv.api.discord.entity.message.SendableDiscordMessage;
import net.dv8tion.jda.api.entities.channel.concrete.MediaChannel;

/**
* Represents a Discord member caching policy, a function which dictates if a given {@link DiscordGuildMember} should be cached.
*/
@FunctionalInterface
public interface DiscordMemberCachePolicy {

DiscordMemberCachePolicy ALL = member -> true;
DiscordMemberCachePolicy LINKED = member -> DiscordSRVApi.optional()
.map(api -> api.profileManager().getProfile(member.getUser().getId()))
.map(IProfile::isLinked).orElse(false);
DiscordMemberCachePolicy VOICE = member -> member.asJDA().getVoiceState() != null;
DiscordMemberCachePolicy OWNER = DiscordGuildMember::isOwner;
import java.util.concurrent.CompletableFuture;

boolean isCached(DiscordGuildMember member);
public interface DiscordMediaChannel extends DiscordChannel, DiscordThreadContainer, JDAEntity<MediaChannel> {
CompletableFuture<DiscordThreadChannel> createPost(String name, SendableDiscordMessage message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jetbrains.annotations.Unmodifiable;

import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

/**
Expand Down Expand Up @@ -141,11 +142,19 @@ public interface ReceivedDiscordMessage extends Snowflake {
*/
@Nullable
@Placeholder(value = "server", relookup = "server")
default DiscordGuild getGuild() {
DiscordTextChannel textChannel = getTextChannel();
DiscordGuild getGuild();

return textChannel != null ? textChannel.getGuild() : null;
}
/**
* Gets the users that were mentioned in this message.
* @return a set of unique users that were mentioned
*/
Set<DiscordUser> getMentionedUsers();

/**
* Gets the {@link DiscordGuild} members that were mentioned in this message.
* @return a set of unique members that were mentioned
*/
Set<DiscordGuildMember> getMentionedMembers();

/**
* Deletes this message.
Expand Down
11 changes: 0 additions & 11 deletions api/src/main/java/com/discordsrv/api/module/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.api.discord.connection.details.DiscordCacheFlag;
import com.discordsrv.api.discord.connection.details.DiscordGatewayIntent;
import com.discordsrv.api.discord.connection.details.DiscordMemberCachePolicy;
import com.discordsrv.api.eventbus.EventListener;
import com.discordsrv.api.eventbus.Subscribe;
import com.discordsrv.api.events.discord.message.AbstractDiscordMessageEvent;
Expand Down Expand Up @@ -120,16 +119,6 @@ default Collection<DiscordCacheFlag> requiredCacheFlags() {
return Collections.emptyList();
}

/**
* Provides a {@link Collection} of {@link DiscordMemberCachePolicy DiscordMemberCachePolicies} that are required for this {@link Module},
* if a policy other than {@link DiscordMemberCachePolicy#OWNER} or {@link DiscordMemberCachePolicy#VOICE} is provided the {@link DiscordGatewayIntent#GUILD_MEMBERS} intent will be required automatically.
* @return the collection of member caching policies required by this module at the time this method is called
*/
@NotNull
default Collection<DiscordMemberCachePolicy> requiredMemberCachingPolicies() {
return Collections.emptyList();
}

/**
* Returns the priority of this Module given the lookup type.
* @param type the type being looked up this could be an interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerAdvancementDoneEvent;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Method;
import java.util.Arrays;
Expand Down Expand Up @@ -65,11 +66,11 @@ public BukkitLegacyAdvancementListener(BukkitDiscordSRV discordSRV) {

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerAdvancementDone(PlayerAdvancementDoneEvent event) {
handleEvent(event);
handleEventWithErrorHandling(event);
}

@Override
protected void handleEvent(PlayerAdvancementDoneEvent event, Void __) {
protected void handleEvent(@NotNull PlayerAdvancementDoneEvent event, Void __) {
if (nms == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerAdvancementDoneEvent;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

Expand All @@ -49,11 +50,11 @@ public PaperAdvancementListener(BukkitDiscordSRV discordSRV) {

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerAdvancementDone(PlayerAdvancementDoneEvent event) {
handleEvent(event);
handleEventWithErrorHandling(event);
}

@Override
protected void handleEvent(PlayerAdvancementDoneEvent event, Void __) {
protected void handleEvent(@NotNull PlayerAdvancementDoneEvent event, Void __) {
Boolean gameRuleValue = event.getPlayer().getWorld().getGameRuleValue(GameRule.ANNOUNCE_ADVANCEMENTS);
if (Objects.equals(gameRuleValue, false)) {
logger().trace("Skipping displaying advancement, disabled by gamerule");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.papermc.paper.event.player.AsyncChatEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.jetbrains.annotations.NotNull;

public class PaperChatListener extends AbstractBukkitListener<AsyncChatEvent> {

Expand All @@ -42,11 +43,11 @@ public PaperChatListener(BukkitDiscordSRV discordSRV) {

@EventHandler(priority = EventPriority.MONITOR)
public void onAsyncChat(AsyncChatEvent event) {
handleEvent(event);
handleEventWithErrorHandling(event);
}

@Override
protected void handleEvent(AsyncChatEvent event, Void __) {
protected void handleEvent(@NotNull AsyncChatEvent event, Void __) {
MinecraftComponent component = MESSAGE_HANDLE.getAPI(event);

IPlayer player = discordSRV.playerProvider().player(event.getPlayer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@
import com.discordsrv.api.events.message.render.GameChatRenderEvent;
import com.discordsrv.bukkit.BukkitDiscordSRV;
import com.discordsrv.bukkit.component.PaperComponentHandle;
import com.discordsrv.bukkit.debug.EventObserver;
import com.discordsrv.common.abstraction.player.IPlayer;
import com.discordsrv.common.core.logging.NamedLogger;
import com.discordsrv.common.feature.channel.global.GlobalChannel;
import io.papermc.paper.event.player.AsyncChatDecorateEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

@SuppressWarnings("UnstableApiUsage") // Understood
@ApiStatus.AvailableSince("Paper 1.19.1")
public class PaperChatRenderListener extends AbstractBukkitListener<AsyncChatDecorateEvent> {

private static final PaperComponentHandle.Get<AsyncChatDecorateEvent> GET_RESULT_HANDLE
Expand All @@ -44,11 +48,11 @@ public PaperChatRenderListener(BukkitDiscordSRV discordSRV) {

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onAsyncChatDecorate(AsyncChatDecorateEvent event) {
handleEvent(event);
handleEventWithErrorHandling(event);
}

@Override
protected void handleEvent(AsyncChatDecorateEvent event, Void __) {
protected void handleEvent(@NotNull AsyncChatDecorateEvent event, Void __) {
Player bukkitPlayer = event.player();
if (bukkitPlayer == null) {
return;
Expand All @@ -72,7 +76,10 @@ protected void handleEvent(AsyncChatDecorateEvent event, Void __) {
}
}

// Not important
private EventObserver<AsyncChatDecorateEvent, Boolean> observer;

@Override
protected void observeEvents(boolean enable) {}
protected void observeEvents(boolean enable) {
observer = observeEvent(observer, AsyncChatDecorateEvent.class, AsyncChatDecorateEvent::isCancelled, enable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.jetbrains.annotations.NotNull;

public class PaperDeathListener extends AbstractBukkitListener<PlayerDeathEvent> {

Expand All @@ -40,11 +41,11 @@ public PaperDeathListener(BukkitDiscordSRV discordSRV) {

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerDeath(PlayerDeathEvent event) {
handleEvent(event);
handleEventWithErrorHandling(event);
}

@Override
protected void handleEvent(PlayerDeathEvent event, Void __) {
protected void handleEvent(@NotNull PlayerDeathEvent event, Void __) {
MinecraftComponent message = MESSAGE_HANDLE.getAPI(event);

DiscordSRVPlayer player = discordSRV.playerProvider().player(event.getEntity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerJoinEvent;
import org.jetbrains.annotations.NotNull;

public class PaperJoinListener extends AbstractBukkitListener<PlayerJoinEvent> {

Expand All @@ -40,11 +41,11 @@ public PaperJoinListener(BukkitDiscordSRV discordSRV) {

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event) {
handleEvent(event);
handleEventWithErrorHandling(event);
}

@Override
protected void handleEvent(PlayerJoinEvent event, Void __) {
protected void handleEvent(@NotNull PlayerJoinEvent event, Void __) {
MinecraftComponent message = MESSAGE_HANDLE.getAPI(event);
boolean firstJoin = !event.getPlayer().hasPlayedBefore();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.discordsrv.bukkit.listener;

import com.discordsrv.api.component.MinecraftComponent;
import com.discordsrv.api.events.message.render.GameChatRenderEvent;
import com.discordsrv.bukkit.BukkitDiscordSRV;
import com.discordsrv.bukkit.component.PaperComponentHandle;
import com.discordsrv.common.abstraction.player.IPlayer;
import com.discordsrv.common.core.logging.NamedLogger;
import com.discordsrv.common.feature.channel.global.GlobalChannel;
import io.papermc.paper.event.player.AsyncChatEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.jetbrains.annotations.NotNull;

public class PaperLegacyChatRenderListener extends AbstractBukkitListener<AsyncChatEvent> {

private static final PaperComponentHandle.Get<AsyncChatEvent> GET_MESSAGE_HANDLE
= PaperComponentHandle.get(AsyncChatEvent.class, "message");
private static final PaperComponentHandle.Set<AsyncChatEvent> SET_MESSAGE_HANDLE
= PaperComponentHandle.set(AsyncChatEvent.class, "message");

public PaperLegacyChatRenderListener(BukkitDiscordSRV discordSRV) {
super(discordSRV, new NamedLogger(discordSRV, "CHAT_RENDER_LISTENER"));
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onAsyncChat(AsyncChatEvent event) {
handleEventWithErrorHandling(event);
}

@Override
protected void handleEvent(@NotNull AsyncChatEvent event, Void __) {
Player bukkitPlayer = event.getPlayer();

IPlayer player = discordSRV.playerProvider().player(bukkitPlayer);
MinecraftComponent message = GET_MESSAGE_HANDLE.getAPI(event);

GameChatRenderEvent annotateEvent = new GameChatRenderEvent(
event,
player,
new GlobalChannel(discordSRV),
message
);

discordSRV.eventBus().publish(annotateEvent);

MinecraftComponent annotatedMessage = annotateEvent.getAnnotatedMessage();
if (annotatedMessage != null) {
SET_MESSAGE_HANDLE.call(event, annotatedMessage);
}
}

// Already observed via normal chat listener
@Override
protected void observeEvents(boolean enable) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.NotNull;

public class PaperQuitListener extends AbstractBukkitListener<PlayerQuitEvent> {

Expand All @@ -40,11 +41,11 @@ public PaperQuitListener(BukkitDiscordSRV discordSRV) {

@EventHandler(priority = EventPriority.HIGH)
public void onPlayerQuit(PlayerQuitEvent event) {
handleEvent(event);
handleEventWithErrorHandling(event);
}

@Override
protected void handleEvent(PlayerQuitEvent event, Void __) {
protected void handleEvent(@NotNull PlayerQuitEvent event, Void __) {
MinecraftComponent message = MESSAGE_HANDLE.getAPI(event);

DiscordSRVPlayer player = discordSRV.playerProvider().player(event.getPlayer());
Expand Down
Loading

0 comments on commit db2bc19

Please sign in to comment.