[Add] BotGlobal BotGlobal在初始化时将检查Redis连通性;

[Change] RandomRankingArtworksSender 支持外部设置groupId, 以使用群组配置;
[Change] BotCommandProcess, BotAdminCommandProcess 适配RandomRankingArtworksSender更改;
This commit is contained in:
LamGC 2020-06-08 09:24:17 +08:00
parent 188309509b
commit 683a38bc17
4 changed files with 67 additions and 16 deletions

View File

@ -271,6 +271,7 @@ public class BotAdminCommandProcess {
AutoSender sender = new RandomRankingArtworksSender( AutoSender sender = new RandomRankingArtworksSender(
MessageSenderBuilder.getMessageSender(MessageSource.Group, id), MessageSenderBuilder.getMessageSender(MessageSource.Group, id),
id,
rankingStart, rankingStart,
rankingEnd, rankingEnd,
rankingMode, rankingContentType, rankingMode, rankingContentType,

View File

@ -14,8 +14,6 @@ import net.lamgc.cgj.pixiv.PixivDownload;
import net.lamgc.cgj.pixiv.PixivDownload.PageQuality; import net.lamgc.cgj.pixiv.PixivDownload.PageQuality;
import net.lamgc.cgj.pixiv.PixivSearchBuilder; import net.lamgc.cgj.pixiv.PixivSearchBuilder;
import net.lamgc.cgj.pixiv.PixivURL; import net.lamgc.cgj.pixiv.PixivURL;
import net.lamgc.cgj.pixiv.PixivURL.RankingContentType;
import net.lamgc.cgj.pixiv.PixivURL.RankingMode;
import net.lamgc.utils.base.runner.Argument; import net.lamgc.utils.base.runner.Argument;
import net.lamgc.utils.base.runner.Command; import net.lamgc.utils.base.runner.Command;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -257,13 +255,34 @@ public class BotCommandProcess {
* 随机获取一副作品 * 随机获取一副作品
*/ */
@Command(commandName = "random") @Command(commandName = "random")
public static String randomImage() { public static String randomImage(
@Argument(name = "$fromGroup") long fromGroup,
@Argument(force = false, name = "mode", defaultValue = "DAILY") String contentMode,
@Argument(force = false, name = "type", defaultValue = "ILLUST") String contentType) {
PixivURL.RankingMode mode;
try {
String rankingModeValue = contentMode.toUpperCase();
mode = PixivURL.RankingMode.valueOf(rankingModeValue.startsWith("MODE_") ?
rankingModeValue : "MODE_" + rankingModeValue);
} catch (IllegalArgumentException e) {
log.warn("无效的RankingMode值: {}", contentMode);
return "参数无效, 请查看帮助信息";
}
PixivURL.RankingContentType type;
try {
String contentTypeValue = contentType.toUpperCase();
type = PixivURL.RankingContentType.valueOf(
contentTypeValue.startsWith("TYPE_") ? contentTypeValue : "TYPE_" + contentTypeValue);
} catch (IllegalArgumentException e) {
log.warn("无效的RankingContentType值: {}", contentType);
return "参数无效, 请查看帮助信息";
}
BufferMessageEvent event = new BufferMessageEvent(); BufferMessageEvent event = new BufferMessageEvent();
RandomRankingArtworksSender artworksSender = RandomRankingArtworksSender artworksSender =
new RandomRankingArtworksSender(event, 1, 200, new RandomRankingArtworksSender(event, fromGroup, 1, 200, mode, type,
RankingMode.MODE_MALE, PageQuality.ORIGINAL);
RankingContentType.TYPE_ALL,
PageQuality.ORIGINAL);
artworksSender.send(); artworksSender.send();
return event.getBufferMessage(); return event.getBufferMessage();
} }

View File

@ -19,6 +19,7 @@ import java.util.Random;
public class RandomRankingArtworksSender extends AutoSender { public class RandomRankingArtworksSender extends AutoSender {
private final Logger log; private final Logger log;
private final long groupId;
private final int rankingStart; private final int rankingStart;
private final int rankingStop; private final int rankingStop;
private final PixivURL.RankingMode mode; private final PixivURL.RankingMode mode;
@ -35,8 +36,37 @@ public class RandomRankingArtworksSender extends AutoSender {
* @param quality 图片质量, 详见{@link PixivDownload.PageQuality} * @param quality 图片质量, 详见{@link PixivDownload.PageQuality}
* @throws IndexOutOfBoundsException rankingStart > rankingStop时抛出 * @throws IndexOutOfBoundsException rankingStart > rankingStop时抛出
*/ */
public RandomRankingArtworksSender(MessageSender messageSender, int rankingStart, int rankingStop, PixivURL.RankingMode mode, PixivURL.RankingContentType contentType, PixivDownload.PageQuality quality) { public RandomRankingArtworksSender(
MessageSender messageSender,
int rankingStart,
int rankingStop,
PixivURL.RankingMode mode,
PixivURL.RankingContentType contentType,
PixivDownload.PageQuality quality) {
this(messageSender, 0, rankingStart, rankingStop, mode, contentType, quality);
}
/**
* 构造一个推荐作品发送器
* @param messageSender 消息发送器
* @param groupId 群组Id, 如果发送目标为群组, 则可设置群组Id, 以使用群组配置.
* @param rankingStart 排行榜开始范围(从1开始, 名次)如传入0或负数则为默认值默认为1
* @param rankingStop 排名榜结束范围(包括该名次)如传入0或负数则为默认值默认为150
* @param mode 排行榜模式
* @param contentType 排行榜内容类型
* @param quality 图片质量, 详见{@link PixivDownload.PageQuality}
* @throws IndexOutOfBoundsException rankingStart > rankingStop时抛出
*/
public RandomRankingArtworksSender(
MessageSender messageSender,
long groupId,
int rankingStart,
int rankingStop,
PixivURL.RankingMode mode,
PixivURL.RankingContentType contentType,
PixivDownload.PageQuality quality) {
super(messageSender); super(messageSender);
this.groupId = groupId;
this.mode = mode; this.mode = mode;
this.contentType = contentType; this.contentType = contentType;
log = LoggerFactory.getLogger(this.toString()); log = LoggerFactory.getLogger(this.toString());
@ -78,7 +108,7 @@ public class RandomRankingArtworksSender extends AutoSender {
JsonObject rankingInfo = rankingList.get(0); JsonObject rankingInfo = rankingList.get(0);
int illustId = rankingInfo.get("illust_id").getAsInt(); int illustId = rankingInfo.get("illust_id").getAsInt();
if(BotCommandProcess.isNoSafe(illustId, if(BotCommandProcess.isNoSafe(illustId,
SettingProperties.getProperties(SettingProperties.GLOBAL), false)) { SettingProperties.getProperties(groupId), false)) {
log.warn("作品为r18作品, 取消本次发送."); log.warn("作品为r18作品, 取消本次发送.");
return; return;
} else if(BotCommandProcess.isReported(illustId)) { } else if(BotCommandProcess.isReported(illustId)) {

View File

@ -8,7 +8,9 @@ import org.apache.http.HttpHost;
import org.apache.http.client.CookieStore; import org.apache.http.client.CookieStore;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -56,6 +58,12 @@ public final class BotGlobal {
this.redisServer = new JedisPool( this.redisServer = new JedisPool(
getRedisUri().getHost(), getRedisUri().getHost(),
getRedisUri().getPort() == -1 ? 6379 : getRedisUri().getPort()); getRedisUri().getPort() == -1 ? 6379 : getRedisUri().getPort());
try (Jedis jedis = this.redisServer.getResource()) {
log.warn("Redis连接状态(Ping): {}", jedis.ping().equalsIgnoreCase("pong"));
} catch(JedisConnectionException e) {
log.warn("Redis连接失败, 将会影响到后续功能运行.", e);
}
String dataStoreDirPath = System.getProperty("cgj.botDataDir"); String dataStoreDirPath = System.getProperty("cgj.botDataDir");
this.dataStoreDir = new File((!dataStoreDirPath.endsWith("/") || !dataStoreDirPath.endsWith("\\")) ? this.dataStoreDir = new File((!dataStoreDirPath.endsWith("/") || !dataStoreDirPath.endsWith("\\")) ?
dataStoreDirPath + System.getProperty("file.separator") : dataStoreDirPath); dataStoreDirPath + System.getProperty("file.separator") : dataStoreDirPath);
@ -95,13 +103,6 @@ public final class BotGlobal {
return proxy; return proxy;
} }
public CookieStore getCookieStore() {
if(pixivDownload == null) {
throw new IllegalStateException("CookieStore needs to be set before PixivDownload can be obtained");
}
return cookieStore;
}
public void setCookieStore(CookieStore cookieStore) { public void setCookieStore(CookieStore cookieStore) {
if(this.cookieStore != null) { if(this.cookieStore != null) {
throw new IllegalStateException("CookieStore set"); throw new IllegalStateException("CookieStore set");