ImageListener no longer uploads images when in DEV mode

This commit is contained in:
Salmonllama 2020-09-06 15:05:17 -04:00
parent fc8f46358b
commit 18353617f0

View File

@ -11,22 +11,26 @@ import dev.salmonllama.fsbot.database.controllers.GalleryController;
import dev.salmonllama.fsbot.database.controllers.OutfitController;
import dev.salmonllama.fsbot.database.models.Outfit;
import dev.salmonllama.fsbot.endpoints.imgur.ImgurAPIConnection;
import org.javacord.api.entity.channel.ServerTextChannel;
import org.javacord.api.entity.message.MessageAttachment;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import org.javacord.api.event.message.MessageCreateEvent;
import org.javacord.api.listener.message.MessageCreateListener;
import org.javacord.api.util.logging.ExceptionLogger;
import java.util.UUID;
public class ImageListener implements MessageCreateListener {
@Override
public void onMessageCreate(MessageCreateEvent event) { // TODO: This needs immediate help
public void onMessageCreate(MessageCreateEvent event) {
// Check for valid source -> DONE -> WORKING
// Check for gallery channel presence -> DONE -> WORKING
// Check for images (attached files and links from approved sources) -> DONE -> WORKING (approved links to be added later)
// Upload the image(s) to imgur -> DONE -> WORKING
// Store the image in the database -> DONE -> WORKING
// Send confirmation && log event -> IN PROGRESS (waiting for logger upgrade)
// Check for production environment to avoid uploading dev images to Imgur -> DONE -> WORKING
if (!event.getMessageAuthor().isRegularUser()) {
// Ignore anything that is a webhook or a bot message
@ -40,61 +44,89 @@ public class ImageListener implements MessageCreateListener {
if (exists) {
// Check the message for images
if (event.getMessageAttachments().stream().anyMatch(MessageAttachment::isImage)) {
// Upload the image(s) to Imgur, store in database, log the stored images.
ImgurAPIConnection imgur = new ImgurAPIConnection();
event.getMessageAttachments().stream().filter(MessageAttachment::isImage).forEach(image -> {
// Check the ENVIRONMENT env-var. If PROD -> Upload to imgur and store. If not, just store
// Upload the image(s) to Imgur, store in database, log the stored images.
event.getMessageAttachments()
.stream()
.filter(MessageAttachment::isImage)
.forEach(image -> {
// Upload to Imgur
imgur.uploadImage(image.getUrl().toString()).thenAccept(upload -> {
// Store in the database
Outfit.OutfitBuilder outfitBuilder = new Outfit.OutfitBuilder()
.setId(upload.getId())
.setMeta(event.getMessageContent())
.setLink(upload.getLink())
.setSubmitter(event.getMessageAuthor().getIdAsString())
.setDeleteHash(upload.getDeleteHash());
GalleryController.getTag(channel.getIdAsString()).thenAccept(tag -> {
outfitBuilder.setTag(tag);
Outfit outfit = outfitBuilder.build();
OutfitController.insert(outfit).thenAcceptAsync((Void) -> {
// Log the outfit
event.getApi().getServerTextChannelById(BotConfig.OUTFIT_LOG).ifPresentOrElse(chnl -> {
EmbedBuilder response = new EmbedBuilder()
.setTitle("Outfit Added")
.setAuthor(event.getMessageAuthor())
.setThumbnail(outfit.getLink())
.setFooter(String.format("%s | %s", outfit.getTag(), outfit.getId()))
.setUrl(outfit.getLink())
.addField("Uploaded:", outfit.getCreated().toString());
if (!outfit.getMeta().equals("")) {
response.addField("Meta:", outfit.getMeta());
}
chnl.sendMessage(response);
// Add the reaction to the original message
GalleryController.getEmoji(channel.getIdAsString()).thenAcceptAsync(emoji -> {
event.getMessage().addReaction(EmojiParser.parseToUnicode(emoji));
}).exceptionally(ExceptionLogger.get());
}, () -> {
// Fallback error message to me
event.getApi().getUserById(BotConfig.BOT_OWNER).thenAcceptAsync(user -> {
user.sendMessage("Could not find OUTFIT LOG");
});
});
});
});
}).exceptionally(ExceptionLogger.get());
});
if (System.getenv("ENVIRONMENT") != null) {
// Upload the image(s) to Imgur, store in database, log the stored images.
System.out.println("PROD environment, uploading");
uploadAndStore(event, channel, image);
} else {
// Store the image(s) in database, log the stored images.
System.out.println("DEV environment, not uploading");
store(event, channel, image);
}
});
}
}
}).exceptionally(ExceptionLogger.get());
});
}
}
private void uploadAndStore(MessageCreateEvent event, ServerTextChannel channel, MessageAttachment image) {
ImgurAPIConnection imgur = new ImgurAPIConnection();
// Upload to Imgur
imgur.uploadImage(image.getUrl().toString()).thenAccept(upload -> {
// Store in the database
Outfit.OutfitBuilder outfitBuilder = new Outfit.OutfitBuilder()
.setId(upload.getId())
.setMeta(event.getMessageContent())
.setLink(upload.getLink())
.setSubmitter(event.getMessageAuthor().getIdAsString())
.setDeleteHash(upload.getDeleteHash());
storeAndLog(event, channel, outfitBuilder);
}).exceptionally(ExceptionLogger.get());
}
private void store(MessageCreateEvent event, ServerTextChannel channel, MessageAttachment image) {
// Store in the database
Outfit.OutfitBuilder outfitBuilder = new Outfit.OutfitBuilder()
.setId(UUID.randomUUID().toString())
.setMeta(event.getMessageContent())
.setLink("DUMMY-LINK")
.setSubmitter(event.getMessageAuthor().getIdAsString())
.setDeleteHash("DUMMY-DELETE-HASH");
storeAndLog(event, channel, outfitBuilder);
}
private void storeAndLog(MessageCreateEvent event, ServerTextChannel channel, Outfit.OutfitBuilder outfitBuilder) {
GalleryController.getTag(channel.getIdAsString()).thenAccept(tag -> {
outfitBuilder.setTag(tag);
Outfit outfit = outfitBuilder.build();
OutfitController.insert(outfit).thenAcceptAsync((Void) -> {
// Log the outfit
event.getApi().getServerTextChannelById(BotConfig.OUTFIT_LOG).ifPresentOrElse(chnl -> {
EmbedBuilder response = new EmbedBuilder()
.setTitle("Outfit Added")
.setAuthor(event.getMessageAuthor())
.setThumbnail(outfit.getLink())
.setFooter(String.format("%s | %s", outfit.getTag(), outfit.getId()))
.setUrl(outfit.getLink())
.addField("Uploaded:", outfit.getCreated().toString());
if (!outfit.getMeta().equals("")) {
response.addField("Meta:", outfit.getMeta());
}
chnl.sendMessage(response);
// Add the reaction to the original message
GalleryController.getEmoji(channel.getIdAsString()).thenAcceptAsync(
emoji -> event.getMessage().addReaction(EmojiParser.parseToUnicode(emoji))
).exceptionally(ExceptionLogger.get());
}, () -> {
// Fallback error message to me
event.getApi().getUserById(BotConfig.BOT_OWNER).thenAcceptAsync(
user -> user.sendMessage("Could not find OUTFIT LOG")
);
});
});
});
}
}