From f4733ab9bc5be659622bd1579d42617944cf21c5 Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Thu, 9 Dec 2021 02:50:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DLinux=E4=B8=8B=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E4=BD=BF=E7=94=A8=E6=B5=8F=E8=A7=88=E5=99=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=9D=83?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=97=A0=E6=B3=95=E8=AE=BF=E9=97=AE=E5=85=AC?= =?UTF-8?q?=E5=BC=80=E6=A8=A1=E5=9D=97=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 8 ++-- .../qqbot/Listeners/QQMessageListener.java | 6 +-- .../qqbot/MessageEvent/AdminMessage.java | 31 ++++++++++------ .../com/yutou/qqbot/QQBotApplication.java | 2 +- .../java/com/yutou/qqbot/QQBotController.java | 36 ++++++++++++++++++ .../java/com/yutou/qqbot/QQBotManager.java | 2 + .../java/com/yutou/qqbot/QQNumberManager.java | 4 ++ .../qqbot/models/Animal/TurnipProphet.java | 37 +++++++++++-------- .../yutou/qqbot/models/Commands/Bangumi.java | 9 ++++- .../qqbot/models/Commands/System/Restart.java | 1 - .../java/com/yutou/qqbot/models/Model.java | 10 +++-- .../java/com/yutou/qqbot/utlis/WebClient.java | 14 +++---- 12 files changed, 113 insertions(+), 47 deletions(-) create mode 100644 src/main/java/com/yutou/qqbot/QQBotController.java diff --git a/pom.xml b/pom.xml index 98c6b75..94543ff 100644 --- a/pom.xml +++ b/pom.xml @@ -90,8 +90,10 @@ selenium-java 3.141.59 - - + + org.springframework.boot + spring-boot-starter-web + @@ -159,7 +161,7 @@ - X:\servier\tools\ + X:\servier\qqbot\ diff --git a/src/main/java/com/yutou/qqbot/Listeners/QQMessageListener.java b/src/main/java/com/yutou/qqbot/Listeners/QQMessageListener.java index 4e80e24..86d0104 100644 --- a/src/main/java/com/yutou/qqbot/Listeners/QQMessageListener.java +++ b/src/main/java/com/yutou/qqbot/Listeners/QQMessageListener.java @@ -37,13 +37,13 @@ public class QQMessageListener extends SimpleListenerHost { new AdminMessage(qqNumber, event.getMessage().contentToString()); } for (Class model : Model.classList) { - if (QQNumberManager.getManager().isUseModel(qqNumber, model)) { + if (QQNumberManager.getManager().isUseModel(qqNumber, model)||!isGroup) { try { Model useModel = (Model) model.getDeclaredConstructor().newInstance(); if (!useModel.isUserPublic()) { if (!QQNumberManager.getManager().isExistsPower(event.getSource().getFromId(), useModel.getUsePowers())) { - Log.i("用户没有权限",event.getSource().getFromId()); - break; + Log.i("用户没有权限",event.getSource().getFromId()+" > "+model.getSimpleName()); + continue; } } useModel.onMessage(qqNumber, event, isGroup); diff --git a/src/main/java/com/yutou/qqbot/MessageEvent/AdminMessage.java b/src/main/java/com/yutou/qqbot/MessageEvent/AdminMessage.java index 4908de5..6cf68a6 100644 --- a/src/main/java/com/yutou/qqbot/MessageEvent/AdminMessage.java +++ b/src/main/java/com/yutou/qqbot/MessageEvent/AdminMessage.java @@ -3,7 +3,7 @@ package com.yutou.qqbot.MessageEvent; import com.yutou.qqbot.QQBotManager; import com.yutou.qqbot.QQNumberManager; import com.yutou.qqbot.models.Model; -import com.yutou.qqbot.utlis.RedisTools; +import com.yutou.qqbot.utlis.AppTools; import com.yutou.qqbot.utlis.StringUtils; import java.util.List; @@ -18,19 +18,18 @@ public class AdminMessage extends Message { private static final String MODEL_SHOW = "!查看模块"; private static final String MODEL_DEL = "!删除模块"; + public static final String SYSTEM_RESTART_QQ="!重启qq"; + public static final String SYSTEM_RESTART_TOOLS="!重启服务"; + + private static final String SHOW="!列表"; private static final String HELP="!help"; } public AdminMessage(Long qq, String msg) { sendQQ = qq; - String msgSrc = msg; String[] msgGroup = msg.replace("!", "!").split(" "); - System.err.println(Model.classList.size()); - for (Class aClass : Model.classList) { - System.err.println(aClass.getName()); - } switch (msgGroup[0]) { case QQCommands.HELP -> QQBotManager.getInstance().sendMessage(qq,Model.getCommands(QQCommands.class).toString()); case QQCommands.POWER_ADD -> { @@ -115,12 +114,10 @@ public class AdminMessage extends Message { StringBuilder builder=new StringBuilder(); for (Long user : users) { builder.append(user).append(":\n"); - if(QQNumberManager.getManager().isGroup(user)){ - List list=QQNumberManager.getManager().getUseModel(user); - builder.append("模块(").append(list.size()).append("):\n"); - for (String model : list) { - builder.append(model).append("\n"); - } + List modelPower=QQNumberManager.getManager().getUseModel(user); + builder.append("模块(").append(modelPower.size()).append("):\n"); + for (String model : modelPower) { + builder.append(model).append("\n"); } List list=QQNumberManager.getManager().getPower(user); builder.append("权限(").append(list.size()).append("):\n"); @@ -131,6 +128,16 @@ public class AdminMessage extends Message { } QQBotManager.getInstance().sendMessage(qq,builder.toString()); } + case QQCommands.SYSTEM_RESTART_QQ -> { + QQBotManager.getInstance().sendMessage(qq,"正在重启机器人"); + System.out.println("结束进程"); + AppTools.exec("cd /home/yutou/public/servier/qqbot && ./start.sh",null,true,false); + } + case QQCommands.SYSTEM_RESTART_TOOLS ->{ + QQBotManager.getInstance().sendMessage(qq,"正在重启服务"); + System.out.println("结束进程"); + AppTools.exec("cd /home/yutou/public/servier/tools && ./start.sh",null,true,false); + } } } diff --git a/src/main/java/com/yutou/qqbot/QQBotApplication.java b/src/main/java/com/yutou/qqbot/QQBotApplication.java index 2dd31e6..ce00662 100644 --- a/src/main/java/com/yutou/qqbot/QQBotApplication.java +++ b/src/main/java/com/yutou/qqbot/QQBotApplication.java @@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class QQBotApplication { - public static final String version="QQBot v.1.0"; + public static final String version="QQBot v.1.0.1"; public static void main(String[] args) { SpringApplication.run(QQBotApplication.class, args); RedisTools.initRedisPoolSub(); diff --git a/src/main/java/com/yutou/qqbot/QQBotController.java b/src/main/java/com/yutou/qqbot/QQBotController.java new file mode 100644 index 0000000..f79a4e5 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/QQBotController.java @@ -0,0 +1,36 @@ +package com.yutou.qqbot; + +import com.yutou.qqbot.utlis.AppTools; +import com.yutou.qqbot.utlis.RedisTools; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; + +@Controller +public class QQBotController { + @ResponseBody + @RequestMapping("/tools/openpc.do") + public String open_pc(HttpServletRequest request, String type) { + if ("nas".equals(type)) { + try { + Process process = Runtime.getRuntime().exec("wakeonlan 00:D8:61:6F:02:2F"); + RedisTools.processOut(process.getInputStream()); + RedisTools.processOut(process.getErrorStream()); + process.destroy(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return "ok"; + } + + @ResponseBody + @RequestMapping("/tools/server.do") + public String sendServerManager(String title, String msg) { + AppTools.sendServer(title, msg); + return "ok"; + } +} diff --git a/src/main/java/com/yutou/qqbot/QQBotManager.java b/src/main/java/com/yutou/qqbot/QQBotManager.java index 1323ce3..2d6fb45 100644 --- a/src/main/java/com/yutou/qqbot/QQBotManager.java +++ b/src/main/java/com/yutou/qqbot/QQBotManager.java @@ -21,6 +21,8 @@ import java.util.Objects; public class QQBotManager { + public static Long defGroup=891655174L; + static { Model.classList.add(QQSetu.class); Model.classList.add(Audio.class); diff --git a/src/main/java/com/yutou/qqbot/QQNumberManager.java b/src/main/java/com/yutou/qqbot/QQNumberManager.java index 14486aa..8cd8b75 100644 --- a/src/main/java/com/yutou/qqbot/QQNumberManager.java +++ b/src/main/java/com/yutou/qqbot/QQNumberManager.java @@ -38,7 +38,11 @@ public class QQNumberManager { jedis.select(RedisTools.QQBOT_USER); Set set=jedis.keys("*"); for (String s : set) { + try { list.add(Long.parseLong(s)); + }catch (Exception ignored){ + + } } jedis.close(); return list; diff --git a/src/main/java/com/yutou/qqbot/models/Animal/TurnipProphet.java b/src/main/java/com/yutou/qqbot/models/Animal/TurnipProphet.java index 0bd3427..78098e7 100644 --- a/src/main/java/com/yutou/qqbot/models/Animal/TurnipProphet.java +++ b/src/main/java/com/yutou/qqbot/models/Animal/TurnipProphet.java @@ -88,9 +88,8 @@ public class TurnipProphet extends Model { json = JSONObject.parseObject(data); array = json.getJSONArray("turnip"); } - System.err.println(getDay()); - if(array.getInteger(0)==-1&&getDay()!=0){ - QQBotManager.getInstance().sendMessage(qq,"没有周日买入信息,本周不收录 :("); + if (array.getInteger(0) == -1 && getDay() != 0) { + QQBotManager.getInstance().sendMessage(qq, "没有周日买入信息,本周不收录 :("); return; } if (json.containsKey("old_pattern")) { @@ -131,18 +130,25 @@ public class TurnipProphet extends Model { prices = prices.replace("-1", ""); json.put("prices", prices); try { - if(pattern!=null) { - switch (pattern){ - case "波动型" -> pattern="0"; - case "大幅上涨(三期型)"-> pattern="1"; - case "递减型" -> pattern="2"; - case "小幅上涨(四期型)" -> pattern="3"; + if (pattern != null) { + switch (pattern) { + case "波动型" -> pattern = "0"; + case "大幅上涨(三期型)" -> pattern = "1"; + case "递减型" -> pattern = "2"; + case "小幅上涨(四期型)" -> pattern = "3"; } } - QQBotManager.getInstance().sendMessage(qq,"已记录,正在预测本周走势..."); + QQBotManager.getInstance().sendMessage(qq, "已记录,正在预测本周走势..."); Map map = WebClient.getInstance().openTurnip(prices, pattern); - + if (map == null) { + String url = String.format("https://turnipprophet.io?prices=%s%s", + prices, + pattern == null ? "" : "&pattern=" + pattern + ); + QQBotManager.getInstance().sendMessage(qq, "没有结果,请检查数据是否有误。 \n网页预览:" + url); + return; + } JSONObject pr = JSONObject.parseObject(map.get(TurnipData.MODEL)); JSONArray prArray = pr.getJSONArray(TurnipData.MODEL); @@ -179,10 +185,11 @@ public class TurnipProphet extends Model { out.append("祝好运 :)"); json.put("pattern", prArray.getJSONObject(0).getString(TurnipData.MODEL)); RedisTools.set(redisKey, json.toJSONString()); - Log.i("TurnipProphet",out.toString()); - QQBotManager.getInstance().sendMessage(qq,out.toString()); + Log.i("TurnipProphet", out.toString()); + QQBotManager.getInstance().sendMessage(qq, out.toString()); } catch (Exception e) { - setData(money, qq); + setData(money, qq); + e.printStackTrace(); } @@ -195,7 +202,7 @@ public class TurnipProphet extends Model { } private int getDay() { - return Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-1; + return Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1; } public static void main(String[] args) throws Exception { diff --git a/src/main/java/com/yutou/qqbot/models/Commands/Bangumi.java b/src/main/java/com/yutou/qqbot/models/Commands/Bangumi.java index adf3016..d0c902b 100644 --- a/src/main/java/com/yutou/qqbot/models/Commands/Bangumi.java +++ b/src/main/java/com/yutou/qqbot/models/Commands/Bangumi.java @@ -23,7 +23,6 @@ public class Bangumi extends Model { super.onMessage(qq, event, isGroup); if(!isGroupPower){ - System.err.println(qq+" 没有权限"); return; } switch (msg){ @@ -123,4 +122,12 @@ public class Bangumi extends Model { QQGroupCommands.QQ_BANGUMI_TODAY }; } + + @Override + public void onTime(String time) { + super.onTime(time); + switch (time) { + case "08:00", "20:00" -> QQBotManager.getInstance().sendMessage(QQBotManager.defGroup,BangumiTools.reportToDayBangumi()); + } + } } diff --git a/src/main/java/com/yutou/qqbot/models/Commands/System/Restart.java b/src/main/java/com/yutou/qqbot/models/Commands/System/Restart.java index 84f0d92..a8a82c6 100644 --- a/src/main/java/com/yutou/qqbot/models/Commands/System/Restart.java +++ b/src/main/java/com/yutou/qqbot/models/Commands/System/Restart.java @@ -23,7 +23,6 @@ public class Restart extends Model { super.onMessage(qq, event, isGroup); if(msg.equals(QQGroupCommands.QQ_SYSTEM_RESTART)){ QQBotManager.getInstance().sendMessage(qq,"正在重启服务"); - System.out.println("结束进程"); AppTools.exec("cd /home/yutou/public/servier/tools && ./start.sh",null,true,false); } } diff --git a/src/main/java/com/yutou/qqbot/models/Model.java b/src/main/java/com/yutou/qqbot/models/Model.java index d8752da..0556390 100644 --- a/src/main/java/com/yutou/qqbot/models/Model.java +++ b/src/main/java/com/yutou/qqbot/models/Model.java @@ -55,10 +55,12 @@ public abstract class Model implements ModelInterface { public void onMessage(Long qq, MessageEvent event, boolean isGroup) { msg = event.getMessage().contentToString(); msg = msg.replace("!", "!"); - GroupMessageEvent groupEvent = (GroupMessageEvent) event; - group = groupEvent.getGroup().getId(); - if (QQNumberManager.getManager().isExistsPower(group, msg.split(" ")[0])) { - isGroupPower = true; + if(isGroup) { + GroupMessageEvent groupEvent = (GroupMessageEvent) event; + group = groupEvent.getGroup().getId(); + if (QQNumberManager.getManager().isExistsPower(group, msg.split(" ")[0])) { + isGroupPower = true; + } } } diff --git a/src/main/java/com/yutou/qqbot/utlis/WebClient.java b/src/main/java/com/yutou/qqbot/utlis/WebClient.java index ce7c315..795dc78 100644 --- a/src/main/java/com/yutou/qqbot/utlis/WebClient.java +++ b/src/main/java/com/yutou/qqbot/utlis/WebClient.java @@ -8,10 +8,7 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; -import org.openqa.selenium.support.ui.ExpectedConditions; -import org.openqa.selenium.support.ui.WebDriverWait; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -31,8 +28,8 @@ public class WebClient { private WebClient() { System.setProperty("webdriver.chrome.driver", - "E:\\GoogleChromeDriver\\chromedriver.exe"); - java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF); + ConfigTools.load(ConfigTools.CONFIG,"chrome",String.class)); + // java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF); } public Map openTurnip(String prices, String pattern) throws Exception { @@ -47,11 +44,13 @@ public class WebClient { webDriver.get(url); webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); Thread.sleep(500); - WebElement element = webDriver.findElement(By.id("output")); List list = element.findElements(By.tagName("tr")); JSONObject pr = new JSONObject(); JSONArray array = new JSONArray(); + if(list.size()==0){ + return null; + } for (WebElement webElement : list) { String[] tmp; @@ -104,7 +103,8 @@ public class WebClient { ChromeOptions options = new ChromeOptions(); options.addArguments("disable-infobars"); options.addArguments("blink-settings=imagesEnabled=false"); - options.addArguments("headless"); + options.addArguments("--headless"); + options.addArguments("--no-sandbox"); return options; }