mirror of
https://github.com/DiscordSRV/Ascension.git
synced 2025-01-07 19:17:52 +01:00
Handle attachment permissions in mirroring
This commit is contained in:
parent
fc86a2bc90
commit
05aedf2067
@ -188,19 +188,42 @@ public class DiscordMessageMirroringModule extends AbstractModule<DiscordSRV> {
|
|||||||
}
|
}
|
||||||
channelIdsHandled.add(channelId);
|
channelIdsHandled.add(channelId);
|
||||||
|
|
||||||
|
GuildMessageChannel channel = (GuildMessageChannel) mirrorChannel.getAsJDAMessageChannel();
|
||||||
|
|
||||||
MirroringConfig config = target.config;
|
MirroringConfig config = target.config;
|
||||||
MirroringConfig.AttachmentConfig attachmentConfig = config.attachments;
|
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);
|
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());
|
messageBuilder.addEmbed(attachmentEmbed.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
int maxSize = attachmentConfig.maximumSizeKb * 1000;
|
|
||||||
List<InputStream> streams = new ArrayList<>();
|
List<InputStream> streams = new ArrayList<>();
|
||||||
if (!attachments.isEmpty() && maxSize >= 0) {
|
if (attachAttachments) {
|
||||||
attachments.forEach((attachment, bytes) -> {
|
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);
|
InputStream stream = new ByteArrayInputStream(bytes);
|
||||||
streams.add(stream);
|
streams.add(stream);
|
||||||
messageBuilder.addAttachment(stream, attachment.fileName());
|
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 =
|
CompletableFuture<MirroredMessage> future =
|
||||||
mirrorChannel.sendMessage(messageBuilder.build())
|
mirrorChannel.sendMessage(messageBuilder.build())
|
||||||
.thenApply(msg -> new MirroredMessage(msg, config));
|
.thenApply(msg -> new MirroredMessage(msg, config));
|
||||||
|
@ -51,6 +51,7 @@ public final class DiscordPermissionUtil {
|
|||||||
}
|
}
|
||||||
EnumSet<Permission> missingPermissions = EnumSet.noneOf(Permission.class);
|
EnumSet<Permission> missingPermissions = EnumSet.noneOf(Permission.class);
|
||||||
for (Permission permission : permissions) {
|
for (Permission permission : permissions) {
|
||||||
|
if (permission == null) continue;
|
||||||
if (!channel.getGuild().getSelfMember().hasPermission(channel, permission)) {
|
if (!channel.getGuild().getSelfMember().hasPermission(channel, permission)) {
|
||||||
missingPermissions.add(permission);
|
missingPermissions.add(permission);
|
||||||
}
|
}
|
||||||
@ -70,6 +71,7 @@ public final class DiscordPermissionUtil {
|
|||||||
public static EnumSet<Permission> getMissingPermissions(Guild guild, Collection<Permission> permissions) {
|
public static EnumSet<Permission> getMissingPermissions(Guild guild, Collection<Permission> permissions) {
|
||||||
EnumSet<Permission> missingPermissions = EnumSet.noneOf(Permission.class);
|
EnumSet<Permission> missingPermissions = EnumSet.noneOf(Permission.class);
|
||||||
for (Permission permission : permissions) {
|
for (Permission permission : permissions) {
|
||||||
|
if (permission == null) continue;
|
||||||
if (!guild.getSelfMember().hasPermission(permission)) {
|
if (!guild.getSelfMember().hasPermission(permission)) {
|
||||||
missingPermissions.add(permission);
|
missingPermissions.add(permission);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user