Handle attachment permissions in mirroring

This commit is contained in:
Vankka 2024-12-27 00:53:19 +02:00
parent fc86a2bc90
commit 05aedf2067
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
2 changed files with 29 additions and 21 deletions

View File

@ -188,19 +188,42 @@ public class DiscordMessageMirroringModule extends AbstractModule<DiscordSRV> {
}
channelIdsHandled.add(channelId);
GuildMessageChannel channel = (GuildMessageChannel) mirrorChannel.getAsJDAMessageChannel();
MirroringConfig config = target.config;
MirroringConfig.AttachmentConfig attachmentConfig = config.attachments;
int attachmentMaxSize = attachmentConfig.maximumSizeKb * 1000;
boolean embedAttachments = attachmentConfig.embedAttachments;
boolean attachAttachments = attachmentMaxSize >= 0;
boolean hasAttachments = !attachments.isEmpty() && (embedAttachments || attachAttachments);
String missingPermissions = DiscordPermissionUtil.missingPermissionsString(
channel,
Permission.VIEW_CHANNEL,
Permission.MANAGE_WEBHOOKS,
embedAttachments ? Permission.MESSAGE_EMBED_LINKS : null,
attachAttachments ? Permission.MESSAGE_ATTACH_FILES : null
);
if (missingPermissions != null) {
logger().error("Failed to mirror message to " + describeChannel(mirrorChannel) + ": " + missingPermissions);
continue;
}
SendableDiscordMessage.Builder messageBuilder = convert(event.getMessage(), mirrorChannel, config);
if (!attachmentEmbed.getFields().isEmpty() && attachmentConfig.embedAttachments) {
if (messageBuilder.isEmpty() && !hasAttachments) {
logger().debug("Nothing to mirror to " + mirrorChannel + ", skipping");
return;
}
if (embedAttachments && !attachmentEmbed.getFields().isEmpty()) {
messageBuilder.addEmbed(attachmentEmbed.build());
}
int maxSize = attachmentConfig.maximumSizeKb * 1000;
List<InputStream> streams = new ArrayList<>();
if (!attachments.isEmpty() && maxSize >= 0) {
if (attachAttachments) {
attachments.forEach((attachment, bytes) -> {
if (bytes != null && (maxSize == 0 || attachment.sizeBytes() <= maxSize)) {
if (bytes != null && (attachmentMaxSize == 0 || attachment.sizeBytes() <= attachmentMaxSize)) {
InputStream stream = new ByteArrayInputStream(bytes);
streams.add(stream);
messageBuilder.addAttachment(stream, attachment.fileName());
@ -208,23 +231,6 @@ public class DiscordMessageMirroringModule extends AbstractModule<DiscordSRV> {
});
}
if (messageBuilder.isEmpty()) {
logger().debug("Nothing to mirror to " + mirrorChannel + ", skipping");
for (InputStream stream : streams) {
try {
stream.close();
} catch (IOException ignored) {}
}
return;
}
GuildMessageChannel channel = (GuildMessageChannel) mirrorChannel.getAsJDAMessageChannel();
String missingPermissions = DiscordPermissionUtil.missingPermissionsString(channel, Permission.VIEW_CHANNEL, Permission.MANAGE_WEBHOOKS);
if (missingPermissions != null) {
logger().error("Failed to mirror message to " + describeChannel(mirrorChannel) + ": " + missingPermissions);
continue;
}
CompletableFuture<MirroredMessage> future =
mirrorChannel.sendMessage(messageBuilder.build())
.thenApply(msg -> new MirroredMessage(msg, config));

View File

@ -51,6 +51,7 @@ public final class DiscordPermissionUtil {
}
EnumSet<Permission> missingPermissions = EnumSet.noneOf(Permission.class);
for (Permission permission : permissions) {
if (permission == null) continue;
if (!channel.getGuild().getSelfMember().hasPermission(channel, permission)) {
missingPermissions.add(permission);
}
@ -70,6 +71,7 @@ public final class DiscordPermissionUtil {
public static EnumSet<Permission> getMissingPermissions(Guild guild, Collection<Permission> permissions) {
EnumSet<Permission> missingPermissions = EnumSet.noneOf(Permission.class);
for (Permission permission : permissions) {
if (permission == null) continue;
if (!guild.getSelfMember().hasPermission(permission)) {
missingPermissions.add(permission);
}