This repository was archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathImageboardUtils.java
310 lines (270 loc) · 13.3 KB
/
ImageboardUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright (C) 2016 Kodehawa
*
* Mantaro is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Mantaro is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Mantaro. If not, see http://www.gnu.org/licenses/
*
*/
package net.kodehawa.mantarobot.commands.image;
import com.google.common.collect.ImmutableMap;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.kodehawa.lib.imageboards.DefaultImageBoards;
import net.kodehawa.lib.imageboards.ImageBoard;
import net.kodehawa.lib.imageboards.entities.BoardImage;
import net.kodehawa.lib.imageboards.entities.Rating;
import net.kodehawa.mantarobot.commands.currency.TextChannelGround;
import net.kodehawa.mantarobot.commands.currency.item.ItemReference;
import net.kodehawa.mantarobot.commands.currency.profile.Badge;
import net.kodehawa.mantarobot.core.command.slash.SlashContext;
import net.kodehawa.mantarobot.db.entities.MongoGuild;
import net.kodehawa.mantarobot.utils.Utils;
import net.kodehawa.mantarobot.utils.commands.EmoteReference;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;
public class ImageboardUtils {
private static final Random r = new Random();
private static final Map<ImageBoard<?>, Integer> maxQuerySize = ImmutableMap.of(
DefaultImageBoards.KONACHAN, 5,
DefaultImageBoards.YANDERE, 5,
DefaultImageBoards.DANBOORU, 1,
DefaultImageBoards.SAFEBOORU, 1
);
private static final Map<ImageBoard<?>, Boolean> imageboardUsesRating = ImmutableMap.of(
DefaultImageBoards.SAFEBOORU, false,
DefaultImageBoards.RULE34, false,
DefaultImageBoards.E621, false
);
public static void getImage(ImageBoard<?> api, ImageRequestType type, boolean nsfwOnly, String imageboard, String rating, String tags, String excludeTags, SlashContext ctx) {
Rating ratingEnum = Rating.SAFE;
if (!nsfwOnly) {
ratingEnum = lookupRating(rating);
if (ratingEnum == null) {
// Try with short name
ratingEnum = lookupShortRating(rating);
}
// If it's *still* null...
if (ratingEnum == null) {
ratingEnum = Rating.SAFE;
}
}
if (!nsfwCheck(ctx, nsfwOnly, false, ratingEnum)) {
ctx.reply("commands.imageboard.non_nsfw_channel", EmoteReference.ERROR);
return;
}
final var dbGuild = ctx.getDBGuild();
if ((ratingEnum == Rating.EXPLICIT || ratingEnum == Rating.QUESTIONABLE || nsfwOnly) && dbGuild.isDisableExplicit()) {
ctx.reply("commands.imageboard.disabled_explicit", EmoteReference.ERROR);
return;
}
if (!Optional.ofNullable(imageboardUsesRating.get(api)).orElse(true)) {
ratingEnum = null;
}
List<String> excludeList = excludeTags.isEmpty() ? new ArrayList<>(1) :
new ArrayList<>(Arrays.asList(excludeTags.split("\\s+"))).stream().map(s -> "-" + s).toList();
List<String> list = tags.isEmpty() ? new ArrayList<>() : new ArrayList<>(Arrays.asList(tags.split("\\s+")));
list.addAll(excludeList);
var limit = Optional.ofNullable(maxQuerySize.get(api)).orElse(10);
if (list.size() > limit) {
ctx.reply("commands.imageboard.too_many_tags", EmoteReference.ERROR, imageboard, limit);
return;
}
final var blackListedImageTags = dbGuild.getBlackListedImageTags();
if (list.stream().anyMatch(blackListedImageTags::contains)) {
ctx.reply("commands.imageboard.blacklisted_tag", EmoteReference.ERROR);
return;
}
try {
if (type == ImageRequestType.TAGS) {
api.search(list, ratingEnum).async(
requestedImages -> sendImage0(ctx, requestedImages, imageboard, blackListedImageTags),
failure -> ctx.sendLocalized("commands.imageboard.error_tag", EmoteReference.ERROR)
);
} else if (type == ImageRequestType.RANDOM) {
api.search(ratingEnum).async(
requestedImages -> sendImage0(ctx, requestedImages, imageboard, blackListedImageTags),
failure -> ctx.sendLocalized("commands.imageboard.error_random", EmoteReference.ERROR)
);
}
} catch (Exception e) {
e.printStackTrace();
ctx.reply("commands.imageboard.error_general", EmoteReference.ERROR);
}
}
private static <T extends BoardImage> void sendImage0(SlashContext ctx, List<T> images, String imageboard, Set<String> blacklisted) {
var filter = filterImages(images, ctx);
if (filter == null) {
return;
}
final var image = filter.get(r.nextInt(filter.size()));
if (image.getTags().stream().anyMatch(blacklisted::contains)) {
ctx.reply("commands.imageboard.blacklisted_tag", EmoteReference.ERROR);
return;
}
sendImage(ctx, imageboard, image, ctx.getDBGuild());
}
private static <T extends BoardImage> List<T> filterImages(List<T> images, SlashContext ctx) {
if (images == null) {
ctx.reply("commands.imageboard.null_image_notice", EmoteReference.ERROR);
return null;
}
final var filter = images.stream()
// This is a pain and a half.
.filter(img -> !img.isPending())
// Somehow Danbooru and e621 are returning null images when a image is deleted?
.filter(img -> img.getURL() != null)
// There should be no need for searches to contain loli content anyway, if it's gonna get locked away.
// This is more of a quality-of-life improvement, don't make them search again if random happened
// to pick undesirable lewd content.
// This also gets away with the need to re-roll, unless they looked up a prohibited tag.
.filter(img -> !containsExcludedTags(img.getTags()))
// Safe images can have undesirable tags too
// Say, stuff that isn't so safe.
.filter(img -> img.getRating() != Rating.SAFE || !containsSafeExcludedTags(img.getTags()))
.collect(Collectors.toList());
if (filter.isEmpty()) {
ctx.reply("commands.imageboard.no_images", EmoteReference.SAD);
return null;
}
return filter;
}
private static void sendImage(SlashContext ctx, String imageboard, BoardImage image, MongoGuild dbGuild) {
final var tags = image.getTags();
final var blackListedImageTags = dbGuild.getBlackListedImageTags();
// This is the last line of defense. It should filter *all* minor tags from all sort of images on
// the method that calls this.
if (containsExcludedTags(tags) && image.getRating() != Rating.SAFE) {
ctx.reply("commands.imageboard.loli_content_disallow", EmoteReference.WARNING);
return;
}
if (tags.stream().anyMatch(blackListedImageTags::contains)) {
ctx.reply("commands.imageboard.blacklisted_tag", EmoteReference.ERROR);
return;
}
// Format the tags output so it's actually human-readable.
final var imageTags = String.join(", ", tags);
imageEmbed(
ctx, image.getURL(), String.valueOf(image.getWidth()),
String.valueOf(image.getHeight()), imageTags, image.getRating(), imageboard
);
if (image.getRating().equals(Rating.EXPLICIT) && r.nextBoolean()) {
var player = ctx.getPlayer();
if (player.addBadgeIfAbsent(Badge.LEWDIE)) {
player.updateAllChanged();
}
// Drop a lewd magazine.
TextChannelGround.of(ctx.getChannel()).dropItemWithChance(ItemReference.LEWD_MAGAZINE, 4);
}
}
public static boolean nsfwCheck(SlashContext ctx, boolean nsfwImageboard, boolean sendMessage, Rating rating) {
if (ctx.isChannelNSFW()) {
return true;
}
var finalRating = rating == null ? Rating.SAFE : rating;
var isSafe = finalRating.equals(Rating.SAFE) && !nsfwImageboard;
if (!isSafe && sendMessage) {
ctx.reply("commands.imageboard.non_nsfw_channel", EmoteReference.ERROR);
}
return isSafe;
}
// List of tags to exclude from *safe* searches
// This isn't exactly safe wdym
private final static List<String> excludedSafeTags = List.of(
"underwear", "bikini", "ass", "wet", "see_through",
// Not quite a bad tag, just filter it out for R34
"video"
);
private static boolean containsSafeExcludedTags(List<String> tags) {
return tags.stream().anyMatch(excludedSafeTags::contains);
}
// The list of tags to exclude from searches.
private final static List<String> excludedTags = List.of(
// minor tags
"loli", "shota", "lolicon", "shotacon", "child", "underage", "young", "younger",
"under_age", "cub",
// questionable whether this one leads to minor images or not, but
// sometimes they're tagged like this and not with any of the tags above
"flat_chest",
// tagme means it hasn't been tagged yet, so it's very unsafe to show
// you know what the other one means
"tagme",
// why
"bestiality", "zoophilia",
// very nsfl tags
"dismemberment", "death", "decapitation", "guro", "eye_socket", "necrophilia",
"rape", "gangrape", "gore", "gross", "bruise", "asphyxiation", "scat",
"strangling", "torture", "vore", "digestion", "stomach_(organ)", "inside_creature",
// Not quite a bad tag, just filter it out for R34
"video"
);
private static boolean containsExcludedTags(List<String> tags) {
return tags.stream().anyMatch(excludedTags::contains);
}
private static void imageEmbed(SlashContext ctx, String url, String width, String height,
String tags, Rating rating, String imageboard) {
var languageContext = ctx.getLanguageContext();
var finalTags = (tags == null ? "None" : tags);
if (finalTags.length() > 1016) {
finalTags = finalTags.substring(0, 1016) + "(...)";
}
var builder = new EmbedBuilder()
.setAuthor(languageContext.get("commands.imageboard.found_image"), url, ctx.getAuthor().getEffectiveAvatarUrl())
.setImage(url)
.setColor(Color.PINK)
.addField(EmoteReference.ZAP.toHeaderString() + languageContext.get("commands.imageboard.source"),
Utils.capitalize(imageboard), false
)
.addField(EmoteReference.DIAMOND.toHeaderString() + languageContext.get("commands.imageboard.rating"),
Utils.capitalize(rating.getLongName()), true
)
.addField(EmoteReference.WIDTH.toHeaderString() + languageContext.get("commands.imageboard.width"),
width + " px", true
)
.addField(EmoteReference.HEIGHT.toHeaderString() + languageContext.get("commands.imageboard.height"),
height + " px", true
)
.addField(EmoteReference.PENCIL.toHeaderString() + languageContext.get("commands.imageboard.tags"),
"`" + finalTags + "`", false
)
.setFooter(languageContext.get("commands.imageboard.load_notice") +
(imageboard.equals("rule34") ? " " + languageContext.get("commands.imageboard.rule34_notice") : ""),
null
);
ctx.send(builder.build(), ActionRow.of(Button.link(url, languageContext.get("commands.imageboard.open_in_browser_link"))));
}
// This is so random is a valid rating.
private static Rating lookupRating(String rating) {
if (rating.equalsIgnoreCase("random")) {
var values = Rating.values();
return values[r.nextInt(values.length)];
} else {
return Rating.lookupFromString(rating);
}
}
// This is so random (R) is a valid rating.
private static Rating lookupShortRating(String shortRating) {
if (shortRating.equalsIgnoreCase("r")) {
var values = Rating.values();
return values[r.nextInt(values.length)];
} else {
return Rating.lookupFromStringShort(shortRating);
}
}
}