Skip to content

Commit

Permalink
Improve code style in HentaiBot class
Browse files Browse the repository at this point in the history
  • Loading branch information
rojikaru committed Oct 16, 2024
1 parent 9302d27 commit d7a31b4
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 139 deletions.
195 changes: 56 additions & 139 deletions src/main/java/org/hentaibot/HentaiBot.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.hentaibot;

import org.apache.log4j.Logger;
import org.hentaibot.constants.Constants;
import org.hentaibot.dtos.Rule34Dto;
import org.hentaibot.dtos.WaifuDto;
import org.hentaibot.network.AnimeWaifuQueries;
import org.hentaibot.network.Client;
import org.hentaibot.network.ClientSFW;
import org.hentaibot.network.HentaiQueries;
import org.hentaibot.network.waifu.AnimeQueries;
import org.hentaibot.network.rule34.Rule34ApiClient;
import org.hentaibot.network.rule34.HentaiQueries;
import org.hentaibot.network.waifu.WaifuApiClient;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendAnimation;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
Expand All @@ -22,156 +23,75 @@
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Random;

public class HentaiBot extends TelegramLongPollingBot {
private static final Random random = new Random();
private static final List<String> picExtensions =
Arrays.asList(".jpg", ".jpeg", ".png", ".webp", ".avif", ".jfif");

private static final List<String> tags =
Arrays.asList("ass","anal","cum","creampie","hentai","masturbation",
"public","orgy","elf","yuri","pussy","glasses","blowjob",
"handjob","footjob","boobs","thighs","ahe_gao","uniform","gangbang","tentacles","genshin_impact", "naruto", "bleach");
private static final List<String> tags_artist =
Arrays.asList("shexyo", "theobrobine", "cutesexyrobutts", "sakimichan", "krabby_(artist)", "lexaiduer", "miraihikari",
"LumiNyu", "lime_(purple_haze", "nepcill", "sciamano240", "neoartcore", "dandon_fuga", "zaphn", "flou",
"prywinko", "alexander_dinh", "kittew", "supullim", "tofuubear", "azto_dio", "derpixon", "kinkymation", "ggc", "afrobull");

private static final Logger logger = Logger.getLogger(HentaiBot.class.getName());

private static final HentaiQueries hentaiClient = Client.getNsfwClient();
private static final AnimeWaifuQueries animeClient = ClientSFW.getSfwClient();
private static final HentaiQueries hentaiClient = Rule34ApiClient.getNsfwClient();
private static final AnimeQueries animeClient = WaifuApiClient.getSfwClient();

private final String botName;

public HentaiBot(String botName, String botToken) {
super(botToken);
this.botName = botName;
}

// received message
@Override
public String getBotUsername() {
return botName;
}

@Override
public void onUpdateReceived(Update update) {
String chatId = update.getMessage().getChatId().toString();
if (!update.hasMessage() || !update.getMessage().hasText()) {
respondWithText(chatId, "I don't understand you. " +
"For list of all supported commands, try /help");
respondWithText(chatId, Constants.MESSAGES.get("UNRECOGNIZED_INPUT"));
return;
}

String userMsg = update.getMessage().getText(), respText;
String userMsg = update.getMessage().getText();

switch (userMsg) {
case "/start" -> respText = """
Hi! I'm HentaiBot. I can send you some spicy anime pics.
Use me if you're 18+.
For list of all supported commands, try /help
""";
case "/help" -> respText = """
List of commands available for now:
/help
/hentai
/anime
/help_tags
""";
case "/help_tags" -> respText = """
List of tags available for now:
/tags_anal
/tags_cum
/tags_creampie
/tags_hentai
/tags_masturbation
/tags_public
/tags_orgy
/tags_elf
/tags_yuri
/tags_pussy
/tags_glasses
/tags_blowjob
/tags_handjob
/tags_footjob
/tags_boobs
/tags_thighs
/tags_ahe_gao
/tags_uniform
/tags_gangbang
/tags_tentacles
/tags_genshin_impact
/tags_naruto
/tags_bleach
/tags_shexyo
/tags_theobrobine
/tags_cutesexyrobutts
/tags_sakimichan
/tags_krabby_(artist)
/tags_lexaiduer
/tags_miraihikari
/tags_LumiNyu
/tags_lime_(purple_haze)
/tags_nepcill
/tags_sciamano240
/tags_neoartcore
/tags_dandon_fuga
/tags_zaphn
/tags_flou
/tags_prywinko
/tags_alexander_dinh
/tags_kittew
/tags_supullim
/tags_tofuubear
/tags_azto_dio
/tags_derpixon
/tags_kinkymation
/tags_ggc
/tags_afrobull
""";
case "/anime", "/sfw" -> {
respText = null;
respondSfw(chatId);
}
case "/hentai", "/nsfw" -> {
respText = null;
respondNsfw(chatId, "female", 200000);
}
default ->
{
if (userMsg.startsWith("/tags_"))
{
respText = null;
if(tags.contains(userMsg.substring(6)))
{
respondNsfw(chatId, userMsg.substring(6) , 50000);
}
else if(tags_artist.contains(userMsg.substring(6)))
{
respondNsfw(chatId, userMsg.substring(6), 500);
}
else
{
respText = "Invalid command. For list of all supported commands, try /help_tags";
}
}
else
{
respText = "Invalid command. For list of all supported commands, try /help";
}

}
}

respondWithText(chatId, respText);
if (userMsg.startsWith("/tags_"))
respondOnTags(chatId, userMsg);
else
respondOnCommands(chatId, userMsg);
}

@Override
public String getBotUsername() {
return botName;
public void respondOnTags(String chatId, String userMsg) {
if (!userMsg.startsWith("/tags_")) return;

String tag = userMsg.substring(6);
Optional<Integer> randomUpperBound =
Constants.TAGS.contains(tag)
? Optional.of(50000)
: Constants.ARTISTS.contains(tag)
? Optional.of(500)
: Optional.empty();

if (randomUpperBound.isPresent())
respondNsfw(chatId, tag, randomUpperBound.get());
else
respondWithText(chatId, Constants.MESSAGES.get("UNRECOGNIZED_INPUT"));
}

public void respondNsfw(String chatId, String tag, int rand_number) {
int pageId = random.nextInt(1, rand_number);
public void respondOnCommands(String chatId, String userMsg) {
switch (userMsg) {
case "/start" -> respondWithText(chatId, Constants.MESSAGES.get("START"));
case "/help" -> respondWithText(chatId, Constants.MESSAGES.get("HELP"));
case "/help_tags" -> respondWithText(chatId, Constants.MESSAGES.get("HELP_TAGS"));
case "/anime", "/sfw" -> respondSfw(chatId);
case "/hentai", "/nsfw" -> respondNsfw(chatId, "female", 200000);
default -> respondWithText(chatId, Constants.MESSAGES.get("UNRECOGNIZED_INPUT"));
}
}

public void respondNsfw(String chatId, String tag, int randomUpperBound) {
int pageId = random.nextInt(1, randomUpperBound);
try {
hentaiClient
.getNsfw(tag, pageId)
Expand Down Expand Up @@ -201,31 +121,29 @@ public void onFailure(Call<List<Rule34Dto>> call, Throwable throwable) {
}
}
);


} catch (Exception ex) {
logger.error(ex.getMessage());
}
}

public void respondSfw(String chatId) {
final String[] categories = new String[]{"waifu", "neko", "shinobu", "megumin", "hug", "awoo", "kiss", "lick", "smug", "blush", "smile", "wave", "nom", "glomp", "slap", "kill", "happy", "wink", "poke", "dance", "cringe"};
String urlCategory = categories[random.nextInt(0, categories.length)];
int categoryIndex = random.nextInt(Constants.SFW_CATEGORIES.size());
String category = Constants.SFW_CATEGORIES.get(categoryIndex);

animeClient
.getSFW(urlCategory)
.getSFW(category)
.enqueue(
new Callback<>() {
@Override
public void onResponse(Call<WaifuDto> call, Response<WaifuDto> response) {
var picSDto = response.body();
logger.info("\nResponse:\n" + picSDto.getUrl() + "\n");

if (picSDto == null) {
var picDto = response.body();
if (picDto == null) {
onFailure(call, new IOException("Response body is null"));
return;
}

var path = picSDto.getUrl();
var path = picDto.getUrl();
logger.info("\nResponse:\n" + path + "\n");
respondWithContent(chatId, path);
}

Expand All @@ -236,7 +154,6 @@ public void onFailure(Call<WaifuDto> call, Throwable throwable) {
}
}
);

}

public void respondWithContent(String chatId, @NotNull String pathToSource) {
Expand All @@ -247,7 +164,7 @@ public void respondWithContent(String chatId, @NotNull String pathToSource) {

if (pathToSource.endsWith(".gif"))
respondWithGif(chatId, file);
else if (picExtensions.stream().anyMatch(pathToSource::endsWith))
else if (Constants.PICTURE_EXTENSIONS.stream().anyMatch(pathToSource::endsWith))
respondWithPicture(chatId, file);
else respondWithVideo(chatId, file);
} catch (IOException ex) {
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/org/hentaibot/constants/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.hentaibot.constants;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Constants {
public static final List<String> TAGS = Arrays.asList(
"ass", "anal", "cum", "creampie", "hentai", "masturbation",
"public", "orgy", "elf", "yuri", "pussy", "glasses", "blowjob",
"handjob", "footjob", "boobs", "thighs", "ahe_gao", "uniform", "gangbang", "tentacles", "genshin_impact", "naruto", "bleach"
);

public static final List<String> ARTISTS = Arrays.asList(
"shexyo", "theobrobine", "cutesexyrobutts", "sakimichan", "krabby_(artist)", "lexaiduer", "miraihikari",
"LumiNyu", "lime_(purple_haze", "nepcill", "sciamano240", "neoartcore", "dandon_fuga", "zaphn", "flou",
"prywinko", "alexander_dinh", "kittew", "supullim", "tofuubear", "azto_dio", "derpixon", "kinkymation", "ggc", "afrobull"
);

public static final List<String> SFW_CATEGORIES = Arrays.asList(
"waifu", "neko", "shinobu", "megumin", "hug", "awoo", "kiss", "lick", "smug", "blush", "smile", "wave", "nom", "glomp", "slap", "kill", "happy", "wink", "poke", "dance", "cringe"
);

public static final List<String> PICTURE_EXTENSIONS =
Arrays.asList(".jpg", ".jpeg", ".png", ".webp", ".avif", ".jfif");

public static final Map<String, String> MESSAGES = Map.of(
"UNRECOGNIZED_INPUT", "I don't understand you. For list of all supported commands, try /help",
"START", """
Hi! I'm HentaiBot. I can send you some spicy anime pics.
Use me if you're 18+.
For list of all supported commands, try /help
""",
"HELP", """
List of commands available for now:
/help
/hentai
/anime
/help_tags
""",
"HELP_TAGS", """
List of tags available for now:
/tags_anal
/tags_cum
/tags_creampie
/tags_hentai
/tags_masturbation
/tags_public
/tags_orgy
/tags_elf
/tags_yuri
/tags_pussy
/tags_glasses
/tags_blowjob
/tags_handjob
/tags_footjob
/tags_boobs
/tags_thighs
/tags_ahe_gao
/tags_uniform
/tags_gangbang
/tags_tentacles
/tags_genshin_impact
/tags_naruto
/tags_bleach
/tags_shexyo
/tags_theobrobine
/tags_cutesexyrobutts
/tags_sakimichan
/tags_krabby_(artist)
/tags_lexaiduer
/tags_miraihikari
/tags_LumiNyu
/tags_lime_(purple_haze)
/tags_nepcill
/tags_sciamano240
/tags_neoartcore
/tags_dandon_fuga
/tags_zaphn
/tags_flou
/tags_prywinko
/tags_alexander_dinh
/tags_kittew
/tags_supullim
/tags_tofuubear
/tags_azto_dio
/tags_derpixon
/tags_kinkymation
/tags_ggc
/tags_afrobull
""",
"INVALID_COMMAND", "Invalid command. For list of all supported commands, try /help",
"INVALID_TAG", "Invalid tag. For list of all supported tags, try /help_tags",
"NO_PICS", "No pics found for this tag. Try another one."
);
}

0 comments on commit d7a31b4

Please sign in to comment.