每次调用都会检查目录是否存在, 如不存在则会抛出异常
+ * @return 返回File对象 + * @throws RuntimeException 当目录创建失败时将包装{@link IOException}异常并抛出. + */ private static File getImageStoreDir() { - if(!imageStoreDir.exists() && !imageStoreDir.mkdirs()) { - log.warn("酷Q图片缓存目录失效!(Path: {} )", imageStoreDir.getAbsolutePath()); - throw new RuntimeException(new IOException("文件夹创建失败!")); + if(!imageStoreDir.exists() && !Files.isSymbolicLink(imageStoreDir.toPath())) { + if(!imageStoreDir.mkdirs()) { + log.warn("酷Q图片缓存目录失效!(Path: {} )", imageStoreDir.getAbsolutePath()); + throw new RuntimeException(new IOException("文件夹创建失败!")); + } } return imageStoreDir; } diff --git a/src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java b/src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java index 0f35faf..1f02550 100644 --- a/src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java +++ b/src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java @@ -27,8 +27,9 @@ public class RankingUpdateTimer { Calendar cal = Calendar.getInstance(); cal.setTime(firstRunDate == null ? new Date() : firstRunDate); LocalDate currentLocalDate = LocalDate.now(); - if(cal.get(Calendar.DAY_OF_YEAR) <= currentLocalDate.getDayOfYear() && - cal.get(Calendar.HOUR_OF_DAY) >= 11 && cal.get(Calendar.MINUTE) >= 30) { + if(cal.get(Calendar.DAY_OF_YEAR) < currentLocalDate.getDayOfYear() || ( + cal.get(Calendar.DAY_OF_YEAR) == currentLocalDate.getDayOfYear() && + (cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE) >= 690))) { cal.set(Calendar.DAY_OF_YEAR, currentLocalDate.getDayOfYear() + 1); } cal.set(Calendar.HOUR_OF_DAY, 11); @@ -36,7 +37,7 @@ public class RankingUpdateTimer { cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); - long delay = cal.getTime().getTime() - (System.currentTimeMillis()); + long delay = cal.getTime().getTime() - System.currentTimeMillis(); log.warn("已设置排行榜定时更新, 首次运行时间: {} ({}min)", cal.getTime(), delay / 1000 / 60); timer.schedule(new TimerTask() { @Override diff --git a/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java b/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java index 26b7583..2533d0c 100644 --- a/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java +++ b/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java @@ -215,12 +215,17 @@ public class BotEventHandler implements EventHandler { } catch(ParameterNoFoundException e) { result = "命令缺少参数: " + e.getParameterName(); } catch(DeveloperRunnerException e) { - if (!(e.getCause() instanceof InterruptedException)) { - log.error("执行命令时发生异常", e); - result = "色图姬在执行命令时遇到了一个错误!"; - } else { + Throwable cause = e.getCause(); + if (cause instanceof InterruptedException) { log.error("命令执行超时, 终止执行."); result = "色图姬发现这个命令的处理时间太久了!所以打断了这个命令。"; + } else if(cause instanceof NoSuchElementException && cause.getMessage().startsWith("No work found: ")) { + String message = cause.getMessage(); + log.error("指定作品不存在.(Id: {})", message.substring(message.lastIndexOf(": ") + 2)); + result = "色图姬找不到这个作品!"; + } else { + log.error("执行命令时发生异常", e); + result = "色图姬在执行命令时遇到了一个错误!"; } } long processTime = System.currentTimeMillis() - time; diff --git a/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java b/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java index 21ebe55..8846d5a 100644 --- a/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java +++ b/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java @@ -542,8 +542,9 @@ public class PixivDownload { * } * * @throws IOException 当请求发生异常, 或接口返回错误信息时抛出. + * @throws NoSuchElementException 当该作品不存在时抛出异常 */ - public JsonObject getIllustInfoByIllustId(int illustId) throws IOException { + public JsonObject getIllustInfoByIllustId(int illustId) throws IOException, NoSuchElementException { HttpGet request = createHttpGetRequest(PixivURL.getPixivIllustInfoAPI(illustId)); HttpResponse response = httpClient.execute(request); String responseStr = EntityUtils.toString(response.getEntity()); @@ -558,7 +559,7 @@ public class PixivDownload { if(illustsArray.size() == 1) { return illustsArray.get(0).getAsJsonObject(); } else { - return null; + throw new NoSuchElementException("No work found: " + illustId); } }