diff --git a/src/main/java/com/yutou/qqbot/Annotations/UseModel.java b/src/main/java/com/yutou/qqbot/Annotations/UseModel.java new file mode 100644 index 0000000..e72a92b --- /dev/null +++ b/src/main/java/com/yutou/qqbot/Annotations/UseModel.java @@ -0,0 +1,11 @@ +package com.yutou.qqbot.Annotations; + +import java.lang.annotation.*; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface UseModel { + + String value() default ""; +} diff --git a/src/main/java/com/yutou/qqbot/Listeners/QQMessageListener.java b/src/main/java/com/yutou/qqbot/Listeners/QQMessageListener.java new file mode 100644 index 0000000..39a53f4 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/Listeners/QQMessageListener.java @@ -0,0 +1,62 @@ +package com.yutou.qqbot.Listeners; + +import com.yutou.qqbot.MessageEvent.AdminMessage; +import com.yutou.qqbot.QQNumberManager; +import com.yutou.qqbot.models.Model; +import com.yutou.qqbot.utlis.Log; +import kotlin.coroutines.CoroutineContext; +import net.mamoe.mirai.event.EventHandler; +import net.mamoe.mirai.event.ListeningStatus; +import net.mamoe.mirai.event.SimpleListenerHost; +import net.mamoe.mirai.event.events.GroupMessageEvent; +import net.mamoe.mirai.event.events.MessageEvent; +import org.jetbrains.annotations.NotNull; + +public class QQMessageListener extends SimpleListenerHost { + + + @Override + public void handleException(@NotNull CoroutineContext context, @NotNull Throwable exception) { + // super.handleException(context, exception); + exception.printStackTrace(); + } + + @EventHandler + public ListeningStatus onMessage(MessageEvent event) { + long qqNumber; + boolean isGroup; + if (event instanceof GroupMessageEvent) { + qqNumber = ((GroupMessageEvent) event).getGroup().getId(); + isGroup = true; + } else { + isGroup = false; + qqNumber = event.getSource().getFromId(); + } + QQNumberManager.getManager().addNumber(qqNumber, isGroup); + if (event.getSource().getFromId() == 583819556) { + new AdminMessage(qqNumber, event.getMessage().contentToString()); + } + System.out.println("isGroup = " + isGroup); + for (Class model : Model.classList) { + if (QQNumberManager.getManager().isUseModel(qqNumber, model)) { + 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; + } + } + useModel.onMessage(qqNumber, event, isGroup); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + return ListeningStatus.LISTENING; // 表示继续监听事件 + + } + + +} diff --git a/src/main/java/com/yutou/qqbot/MessageEvent/AdminMessage.java b/src/main/java/com/yutou/qqbot/MessageEvent/AdminMessage.java new file mode 100644 index 0000000..e2730a1 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/MessageEvent/AdminMessage.java @@ -0,0 +1,180 @@ +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.StringUtils; + +import java.util.List; + +public class AdminMessage extends Message { + + public static class QQCommands { + private static final String POWER_ADD = "!新增权限"; + private static final String POWER_SHOW = "!查看权限"; + private static final String POWER_DEL = "!删除权限"; + private static final String MODEL_ADD = "!新增模块"; + private static final String MODEL_SHOW = "!查看模块"; + private static final String MODEL_DEL = "!删除模块"; + } + + 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()); + } + String[] commands; + switch (msgGroup[0]) { + case QQCommands.POWER_ADD -> { + if (msgGroup.length == 1) { + StringBuilder builder = Model.getCommands(Model.QQGroupCommands.class); + builder.append("\n"); + builder.append(Model.getCommands(Model.QQFromCommands.class)); + QQBotManager.getInstance().sendMessage(qq, "使用方法:" + QQCommands.POWER_ADD + " [qqnumber] 权限" + "\n" + "可设置以下权限,用&分割"); + QQBotManager.getInstance().sendMessage(qq, builder.toString()); + } else { + if (msgGroup.length <= 2) { + addPower(msgGroup[1], null); + } else { + addPower(msgGroup[1], msgGroup[2]); + } + } + } + case QQCommands.POWER_SHOW -> { + if (msgGroup.length == 1) { + QQBotManager.getInstance().sendMessage(qq, "使用方法:" + QQCommands.POWER_SHOW + " [qqnumber]"); + } else { + List list = QQNumberManager.getManager().getPower(Long.parseLong(msgGroup[1])); + StringBuilder builder = new StringBuilder(); + for (String power : list) { + builder.append(power).append("\n"); + } + QQBotManager.getInstance().sendMessage(qq, Long.parseLong(msgGroup[1]) + " 拥有以下权限:" + builder); + } + } + case QQCommands.POWER_DEL -> { + if (msgGroup.length == 1) { + QQBotManager.getInstance().sendMessage(qq, "使用方法:" + QQCommands.POWER_DEL + " [qqnumber] 权限 用&分割"); + } else { + if (msgGroup.length <= 2) { + delPower(msgGroup[1], null); + } else { + delPower(msgGroup[1], msgGroup[2]); + } + } + } + case QQCommands.MODEL_ADD -> { + if (msgGroup.length == 1) { + StringBuilder builder = new StringBuilder(); + for (Class aClass : Model.classList) { + builder.append(aClass.getName()).append("\n"); + } + QQBotManager.getInstance().sendMessage(qq, "使用方法:" + QQCommands.MODEL_ADD + " [qqnumber] 模块" + "\n" + "可设置以下模块,用&分割"); + QQBotManager.getInstance().sendMessage(qq, builder.toString()); + } else { + if (msgGroup.length <= 2) { + addModel(msgGroup[1], null); + } else { + addModel(msgGroup[1], msgGroup[2]); + } + } + } + case QQCommands.MODEL_SHOW -> { + if (msgGroup.length == 1) { + QQBotManager.getInstance().sendMessage(qq, "使用方法:" + QQCommands.MODEL_SHOW + " [qqnumber]"); + } else { + List list = QQNumberManager.getManager().getUseModel(Long.parseLong(msgGroup[1])); + StringBuilder builder = new StringBuilder(); + for (String power : list) { + builder.append(power).append("\n"); + } + QQBotManager.getInstance().sendMessage(qq, Long.parseLong(msgGroup[1]) + " 拥有以下模块:" + builder); + } + } + case QQCommands.MODEL_DEL -> { + if (msgGroup.length == 1) { + QQBotManager.getInstance().sendMessage(qq, "使用方法:" + QQCommands.MODEL_DEL + " [qqnumber] 模块 用&分割"); + } else { + if (msgGroup.length <= 2) { + delModel(msgGroup[1], null); + } else { + delModel(msgGroup[1], msgGroup[2]); + } + } + } + } + } + + private void addPower(String qq, String msg) { + boolean flag = false; + if (msg == null) { + StringBuilder builder = Model.getCommands(Model.QQGroupCommands.class); + builder.append("\n"); + builder.append(Model.getCommands(Model.QQFromCommands.class)); + for (String power : builder.toString().split("\n")) { + if (!StringUtils.isEmpty(power)) { + flag = QQNumberManager.getManager().addPower(Long.parseLong(qq), power); + } + } + } else { + for (String power : msg.split("&")) { + flag = QQNumberManager.getManager().addPower(Long.parseLong(qq), power); + } + } + QQBotManager.getInstance().sendMessage(sendQQ, flag ? "新增成功" : "新增失败"); + } + + private void delPower(String qq, String msg) { + if (msg == null) { + List list = QQNumberManager.getManager().getPower(Long.parseLong(qq)); + for (String power : list) { + QQNumberManager.getManager().delPower(Long.parseLong(qq), power); + } + } else { + for (String power : msg.split("&")) { + QQNumberManager.getManager().delPower(Long.parseLong(qq), power); + } + } + } + + private void addModel(String qq, String model) { + boolean flag = false; + try { + if (model == null) { + for (Class power : Model.classList) { + flag = QQNumberManager.getManager().addUseModel(Long.parseLong(qq), power); + } + } else { + for (String power : model.split("&")) { + flag = QQNumberManager.getManager().addUseModel(Long.parseLong(qq), Class.forName(power)); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + QQBotManager.getInstance().sendMessage(sendQQ, flag ? "新增成功" : "新增失败"); + } + + private void delModel(String qq, String model) { + try { + if (model == null) { + List list = QQNumberManager.getManager().getUseModel(Long.parseLong(qq)); + for (String power : list) { + QQNumberManager.getManager().delUseModel(Long.parseLong(qq), Class.forName(power)); + } + } else { + for (String power : model.split("&")) { + QQNumberManager.getManager().delUseModel(Long.parseLong(qq), Class.forName(power)); + } + } + }catch (Exception e){ + e.printStackTrace(); + } + + } +} diff --git a/src/main/java/com/yutou/qqbot/MessageEvent/Message.java b/src/main/java/com/yutou/qqbot/MessageEvent/Message.java new file mode 100644 index 0000000..5180754 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/MessageEvent/Message.java @@ -0,0 +1,7 @@ +package com.yutou.qqbot.MessageEvent; + +public class Message { + + + public long sendQQ=891655174L; +} diff --git a/src/main/java/com/yutou/qqbot/QQBotManager.java b/src/main/java/com/yutou/qqbot/QQBotManager.java index 711ea4d..d0a6f72 100644 --- a/src/main/java/com/yutou/qqbot/QQBotManager.java +++ b/src/main/java/com/yutou/qqbot/QQBotManager.java @@ -1,54 +1,39 @@ package com.yutou.qqbot; -import com.yutou.qqbot.interfaces.DownloadInterface; -import com.yutou.qqbot.models.audio.QQAudio; +import com.yutou.qqbot.Listeners.QQMessageListener; +import com.yutou.qqbot.models.Commands.Bangumi; +import com.yutou.qqbot.models.Commands.System.*; +import com.yutou.qqbot.models.Model; import com.yutou.qqbot.models.setu.QQSetu; import com.yutou.qqbot.utlis.*; import net.mamoe.mirai.Bot; import net.mamoe.mirai.BotFactory; import net.mamoe.mirai.event.GlobalEventChannel; -import net.mamoe.mirai.event.events.GroupMessageEvent; import net.mamoe.mirai.message.data.Image; import net.mamoe.mirai.message.data.MessageChainBuilder; import net.mamoe.mirai.utils.BotConfiguration; import net.mamoe.mirai.utils.ExternalResource; import java.io.File; -import java.lang.reflect.Field; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.Objects; -import java.util.function.Consumer; -import java.util.regex.Matcher; -import java.util.regex.Pattern; public class QQBotManager { - - public static class QQCommands { - private final static String QQ_HELP = "!help"; - private final static String QQ_SYSTEM_RESTART = "!restart"; - private final static String QQ_UPDATE_IP = "!更新ip"; - private final static String QQ_GET_IP = "!ip"; - private final static String QQ_OPEN_PC = "!开机"; - private final static String QQ_GET_VERSION = "!version"; - private final static String QQ_CMD = "!cmd"; - private final static String QQ_BANGUMI_TODAY = "!今日动画"; - private final static String QQ_BANGUMI_LIST = "!新番"; - private final static String QQ_BANGUMI_SUB = "!查动画"; - private final static String QQ_AUDIO = "!语音"; - private final static String QQ_AUDIO_OPEN_LAMP = "!开灯"; - private final static String QQ_AUDIO_OPEN_AIR = "!开空调"; - private final static String QQ_BT_RELOAD = "!刷bt"; - private final static String QQ_TOOLS_IDEA = "!idea"; - private final static String QQ_TOOLS_IDEA_FILE = "!idea>"; - private final static String QQ_TOOLS_IDEA_URL = "!idea_url"; - - public final static String QQ_BANGUMI_INFO="!保存动画信息>"; + static { + Model.classList.add(QQSetu.class); + Model.classList.add(Audio.class); + Model.classList.add(BtFlash.class); + Model.classList.add(Cmd.class); + Model.classList.add(Help.class); + Model.classList.add(IP.class); + Model.classList.add(OpenPC.class); + Model.classList.add(Restart.class); + Model.classList.add(ToolsIdea.class); + Model.classList.add(UpdateIP.class); + Model.classList.add(Version.class); + Model.classList.add(Bangumi.class); } - private static QQBotManager botManager = null; private Bot bot; private static final long qqGroup = 891655174L; @@ -82,7 +67,8 @@ public class QQBotManager { } }); //Events.registerEvents(bot, new MessageListener()); - GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessageEvent.class, new MessageListener()); + GlobalEventChannel.INSTANCE.registerListenerHost(new QQMessageListener()); + // GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessageEvent.class, new MessageListener()); bot.login(); new Thread(new Runnable() { @Override @@ -115,9 +101,13 @@ public class QQBotManager { return isLogin; } - private Image getImage(File file) { + private Image getImage(File file,Long qq) { if (bot != null) { - return Objects.requireNonNull(bot.getGroup(qqGroup)).uploadImage(ExternalResource.create(file)); + if(QQNumberManager.getManager().isGroup(qq)) { + return Objects.requireNonNull(bot.getGroup(qq)).uploadImage(ExternalResource.create(file)); + }else{ + return Objects.requireNonNull(bot.getFriend(qq)).uploadImage(ExternalResource.create(file)); + } } return null; } @@ -126,9 +116,6 @@ public class QQBotManager { return "没有登录QQ"; } - public void reportToDayBangumi() { - getInstance().sendMessage(BangumiTools.reportToDayBangumi()); - } public String sendMessage(String text) { if (bot != null&&!StringUtils.isEmpty(text)) { @@ -144,7 +131,11 @@ public class QQBotManager { public String sendMessage(Long group, String text) { if (bot != null) { try { - return Objects.requireNonNull(bot.getGroup(group)).sendMessage(text).toString(); + if(QQNumberManager.getManager().isGroup(group)) { + return Objects.requireNonNull(bot.getGroup(group)).sendMessage(text).toString(); + }else{ + return Objects.requireNonNull(bot.getFriend(group)).sendMessage(text).toString(); + } } catch (Exception e) { e.printStackTrace(); } @@ -154,21 +145,28 @@ public class QQBotManager { public void sendMessage(Long group, MessageChainBuilder builder) { if (bot != null) { - Objects.requireNonNull(bot.getGroup(group)).sendMessage(builder.asMessageChain()); + if(QQNumberManager.getManager().isGroup(group)) { + Objects.requireNonNull(bot.getGroup(group)).sendMessage(builder.asMessageChain()); + }else{ + Objects.requireNonNull(bot.getFriend(group)).sendMessage(builder.asMessageChain()); + } } } - - public String sendMessage(File imageFile, String text) { + public String sendMessage(File imageFile,Long qq,String text){ try { if (bot != null) { - Image image = getImage(imageFile); + Image image = getImage(imageFile,qq); MessageChainBuilder builder = new MessageChainBuilder(); if (image != null) { builder.append(image); } builder.append(text); - return Objects.requireNonNull(bot.getGroup(qqGroup)).sendMessage(builder.asMessageChain()).toString(); + if(QQNumberManager.getManager().isGroup(qq)) { + return Objects.requireNonNull(bot.getGroup(qq)).sendMessage(builder.asMessageChain()).toString(); + }else{ + return Objects.requireNonNull(bot.getFriend(qq)).sendMessage(builder.asMessageChain()).toString(); + } } } catch (Exception e) { e.printStackTrace(); @@ -176,198 +174,40 @@ public class QQBotManager { return getNotLoginQQ(); } + public String sendMessage(File imageFile, String text) { + return sendMessage(imageFile,qqGroup,text); + } - public String sendMessage(List imgs, String text) { + public String sendMessage(List imgs,Long qq, String text) { if (bot != null) { MessageChainBuilder builder = new MessageChainBuilder(); for (File img : imgs) { - builder.append(Objects.requireNonNull(getImage(img))); + builder.append(Objects.requireNonNull(getImage(img,qq))); } builder.append(text); - return Objects.requireNonNull(bot.getGroup(qqGroup)).sendMessage(builder.asMessageChain()).toString(); + if(QQNumberManager.getManager().isGroup(qq)) { + return Objects.requireNonNull(bot.getGroup(qq)).sendMessage(builder.asMessageChain()).toString(); + }else{ + return Objects.requireNonNull(bot.getFriend(qq)).sendMessage(builder.asMessageChain()).toString(); + } } return getNotLoginQQ(); } - public static List getImages(String str) { - List list = new ArrayList<>(); - String regex = ""; - Pattern pattern = Pattern.compile(regex); - Matcher matcher = pattern.matcher(str); - while (matcher.find()) { - list.add(matcher.group().replace("", "") - .trim()); - } - return list; - } + public static void main(String[] args) { getInstance(); } - private static class MessageListener implements Consumer { - - - @Override - public void accept(GroupMessageEvent event) { - String msg = event.getMessage().contentToString(); - switch (event.getGroup().getId() + "") { - case qqGroup + "": - myGroup(msg); - case "570197155": - new QQSetu().setu(msg, event); - - } - } - - private void myGroup(String msg) { - StringBuilder builder; - String msgSrc=msg; - msg = msg.replace("!", "!").toLowerCase(); - Log.i("QQBot","command = "+msg); - switch (msg) { - case QQCommands.QQ_OPEN_PC: - RedisTools.Consumer.system("openPC", null); - String time = new SimpleDateFormat("HH").format(new Date()); - if (Integer.parseInt(time) < 18) { - break; - } - case QQCommands.QQ_AUDIO_OPEN_LAMP: - QQAudio.playText("小爱同学,开灯"); - break; - case QQCommands.QQ_AUDIO_OPEN_AIR: - QQAudio.playText("小爱同学,开空调"); - break; - case QQCommands.QQ_UPDATE_IP: - RedisTools.Consumer.system("updateIP", null); - break; - case QQCommands.QQ_GET_IP: - RedisTools.Consumer.bot("getip"); - break; - case QQCommands.QQ_GET_VERSION: - sendVersion(); - break; - case QQCommands.QQ_BANGUMI_TODAY: - RedisTools.remove("reportToDayBangumi"); - QQBotManager.getInstance().sendMessage(BangumiTools.reportToDayBangumi()); - break; - case QQCommands.QQ_BANGUMI_LIST: - getInstance().sendMessage("获取中..."); - getInstance().sendMessage(BangumiTools.reportBangumiList()); - break; - case QQCommands.QQ_BT_RELOAD: - HttpTools.get("http://192.168.31.88:8000/bt/down.do"); - break; - case QQCommands.QQ_SYSTEM_RESTART: - getInstance().sendMessage("正在重启服务"); - System.out.println("结束进程"); - AppTools.exec("cd /home/yutou/public/servier/tools && ./start.sh",null,true,false); - break; - case QQCommands.QQ_HELP: - builder = new StringBuilder(); - for (Field field : QQCommands.class.getDeclaredFields()) { - try { - field.setAccessible(true); - builder.append(field.get(null)).append("\n"); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - getInstance().sendMessage(builder.toString()); - break; - case QQCommands.QQ_TOOLS_IDEA: - builder = new StringBuilder(); - if(StringUtils.isEmpty(RedisTools.get("ideaUrl"))){ - builder.append("暂未设置IDEA激活码下载地址"); - }else { - for (String name : IdeaTools.getIdeaList(RedisTools.get("ideaUrl"))) { - builder.append(QQCommands.QQ_TOOLS_IDEA_FILE).append(name).append("\n"); - } - if(builder.toString().trim().length()==0){ - builder.append("激活码文件中未包含txt文件"); - } - } - getInstance().sendMessage(builder.toString()); - break; - default: - if (msg.startsWith(QQCommands.QQ_CMD)) { - RedisTools.Consumer.system("cmd", msg.replace(QQCommands.QQ_CMD, "")); - } else if (msg.startsWith(QQCommands.QQ_BANGUMI_SUB)) { - List infos = null; - try { - int id = Integer.parseInt(msg.replace(QQCommands.QQ_BANGUMI_SUB, "").trim()); - infos = BangumiTools.reportBangumiInfo(id); - } catch (Exception e) { - String key = msg.replace(QQCommands.QQ_BANGUMI_SUB, "").trim(); - infos = BangumiTools.reportSearchBangumi(key); - } - for (String info : infos) { - List imgs = new ArrayList<>(); - if (info.contains("")) { - imgs = getImages(info); - for (String img : imgs) { - info = info.replace("", ""); - } - } - sendImagesMsg(imgs, info); - } - - } else if (msg.startsWith(QQCommands.QQ_AUDIO)) { - QQAudio.playText(msg.replace(QQCommands.QQ_AUDIO, "")); - }else if(msg.startsWith(QQCommands.QQ_TOOLS_IDEA_FILE)){ - getInstance().sendMessage(IdeaTools.getIdea(msgSrc.replace(QQCommands.QQ_TOOLS_IDEA_FILE,""))); - }else if(msg.startsWith(QQCommands.QQ_TOOLS_IDEA_URL)){ - RedisTools.set("ideaUrl",msg.replace(QQCommands.QQ_TOOLS_IDEA_URL,"").trim()); - getInstance().sendMessage("已设定下载地址:"+RedisTools.get("ideaUrl")); - } - } - } - - - private List files; - private int index = 0; - - private void sendImagesMsg(List imgs, String text) { - files = new ArrayList<>(); - index = 0; - if (imgs.size() == 0) { - getInstance().sendMessage(text); - return; - } - for (String img : imgs) { - HttpTools.download(img,null, new DownloadInterface() { - @Override - public void onDownload(File file) { - super.onDownload(file); - files.add(file); - send(imgs.size(), text); - } - - @Override - public void onError(Exception e) { - super.onError(e); - index++; - send(imgs.size(), text); - } - }); - } - } - - private void send(int size, String text) { - if ((files.size() + index) == size) { - String str = getInstance().sendMessage(files, text); - Log.i("str = " + str); - } - } - - private void sendVersion() { - String localVersion = QQBotApplication.version; - String serverVersion = HttpTools.get("http://tools.yutou233.cn:8000/public/version.do?token=zIrsh9TUZP2lfRW753PannG49E7VJvor"); - String msg = "本地版本:" + localVersion + "\n" + "服务器版本:" + serverVersion; - QQBotManager.getInstance().sendMessage(msg); - AppTools.sendServer("服务版本查询", msg); - } - + public void sendVersion() { + String localVersion = QQBotApplication.version; + String serverVersion = HttpTools.get("http://tools.yutou233.cn:8000/public/version.do?token=zIrsh9TUZP2lfRW753PannG49E7VJvor"); + String msg = "本地版本:" + localVersion + "\n" + "服务器版本:" + serverVersion; + QQBotManager.getInstance().sendMessage(msg); + AppTools.sendServer("服务版本查询", msg); } + + + } diff --git a/src/main/java/com/yutou/qqbot/QQNumberManager.java b/src/main/java/com/yutou/qqbot/QQNumberManager.java new file mode 100644 index 0000000..c94da30 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/QQNumberManager.java @@ -0,0 +1,140 @@ +package com.yutou.qqbot; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.yutou.qqbot.utlis.Log; +import com.yutou.qqbot.utlis.RedisTools; +import redis.clients.jedis.Jedis; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +public class QQNumberManager { + private static QQNumberManager manager; + private QQNumberManager(){ + + } + + public static QQNumberManager getManager() { + if(manager==null) { + manager=new QQNumberManager(); + } + return manager; + } + public void addNumber(Long qq,boolean isGroup){ + if(RedisTools.exists(qq,null)){ + return; + } + JSONObject json=new JSONObject(); + json.put("group",isGroup); + json.put("power",new JSONArray()); + json.put("model",new JSONArray()); + RedisTools.set(qq,json.toJSONString()); + } + public List getNumber(){ + List list =new ArrayList<>(); + Jedis jedis=RedisTools.getRedis(); + jedis.select(3); + Set set=jedis.keys("*"); + for (String s : set) { + list.add(Long.parseLong(s)); + } + jedis.close(); + return list; + } + public boolean addPower(Long qq, String power){ + if(RedisTools.exists(qq,null)){ + JSONObject json=JSONObject.parseObject(RedisTools.get(qq)); + JSONArray array=json.getJSONArray("power"); + array.add(power); + json.put("power",array); + return RedisTools.set(qq,json.toJSONString()); + } + return false; + } + public List getPower(Long qq){ + List list=new ArrayList<>(); + if(RedisTools.exists(qq,null)){ + JSONObject json=JSONObject.parseObject(RedisTools.get(qq)); + JSONArray array=json.getJSONArray("power"); + for (Object power : array) { + list.add((String) power); + } + return list; + } + return list; + } + public List getUseModel(long qq) { + List list=new ArrayList<>(); + if(RedisTools.exists(qq,null)){ + JSONObject json=JSONObject.parseObject(RedisTools.get(qq)); + JSONArray array=json.getJSONArray("model"); + for (Object power : array) { + list.add((String) power); + } + return list; + } + return list; + } + public boolean delPower(Long qq, String power){ + if(RedisTools.exists(qq,null)){ + JSONObject json=JSONObject.parseObject(RedisTools.get(qq)); + JSONArray array=json.getJSONArray("power"); + array.remove(power); + json.put("power",array); + return RedisTools.set(qq,json.toJSONString()); + } + return false; + } + public boolean addUseModel(Long qq,Class modelClass){ + if(RedisTools.exists(qq,null)){ + JSONObject json=JSONObject.parseObject(RedisTools.get(qq)); + JSONArray array=json.getJSONArray("model"); + array.add(modelClass.getName()); + json.put("model",array); + return RedisTools.set(qq,json.toJSONString()); + } + return false; + } + public boolean delUseModel(Long qq,Class modelClass){ + if(RedisTools.exists(qq,null)){ + JSONObject json=JSONObject.parseObject(RedisTools.get(qq)); + JSONArray array=json.getJSONArray("model"); + array.remove(modelClass.getName()); + json.put("model",array); + return RedisTools.set(qq,json.toJSONString()); + } + return false; + } + public boolean isExistsPower(Long qq, String... power){ + if(RedisTools.exists(qq,null)){ + JSONObject json=JSONObject.parseObject(RedisTools.get(qq)); + JSONArray array=json.getJSONArray("power"); + for (String key : power) { + if(!array.contains(key)){ + return false; + } + } + return true; + } + return false; + } + public boolean isGroup(Long qq){ + if(RedisTools.exists(qq,null)){ + JSONObject json=JSONObject.parseObject(RedisTools.get(qq)); + return json.getBoolean("group"); + } + return false; + } + public boolean isUseModel(Long qq,Class modelClass){ + if(RedisTools.exists(qq,null)){ + JSONObject json=JSONObject.parseObject(RedisTools.get(qq)); + JSONArray array=json.getJSONArray("model"); + return array.contains(modelClass.getName()); + } + return false; + } + + +} \ No newline at end of file diff --git a/src/main/java/com/yutou/qqbot/interfaces/ModelInterface.java b/src/main/java/com/yutou/qqbot/interfaces/ModelInterface.java new file mode 100644 index 0000000..d44a9e6 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/interfaces/ModelInterface.java @@ -0,0 +1,6 @@ +package com.yutou.qqbot.interfaces; + +public interface ModelInterface { + boolean isUserPublic(); + String[] getUsePowers(); +} diff --git a/src/main/java/com/yutou/qqbot/models/Animal/TurnipProphet.java b/src/main/java/com/yutou/qqbot/models/Animal/TurnipProphet.java new file mode 100644 index 0000000..59578cb --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Animal/TurnipProphet.java @@ -0,0 +1,15 @@ +package com.yutou.qqbot.models.Animal; + +import com.yutou.qqbot.models.Model; + +public class TurnipProphet extends Model { + @Override + public boolean isUserPublic() { + return false; + } + + @Override + public String[] getUsePowers() { + return new String[0]; + } +} diff --git a/src/main/java/com/yutou/qqbot/models/Commands/Bangumi.java b/src/main/java/com/yutou/qqbot/models/Commands/Bangumi.java new file mode 100644 index 0000000..adf3016 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Commands/Bangumi.java @@ -0,0 +1,126 @@ +package com.yutou.qqbot.models.Commands; + +import com.yutou.qqbot.QQBotManager; +import com.yutou.qqbot.interfaces.DownloadInterface; +import com.yutou.qqbot.models.Model; +import com.yutou.qqbot.utlis.BangumiTools; +import com.yutou.qqbot.utlis.HttpTools; +import com.yutou.qqbot.utlis.Log; +import com.yutou.qqbot.utlis.RedisTools; +import net.mamoe.mirai.event.events.MessageEvent; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Bangumi extends Model { + + + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup) { + super.onMessage(qq, event, isGroup); + + if(!isGroupPower){ + System.err.println(qq+" 没有权限"); + return; + } + switch (msg){ + case QQGroupCommands.QQ_BANGUMI_TODAY -> { + RedisTools.remove("reportToDayBangumi"); + QQBotManager.getInstance().sendMessage(qq, BangumiTools.reportToDayBangumi()); + } + case QQGroupCommands.QQ_BANGUMI_LIST -> { + QQBotManager.getInstance().sendMessage(qq,"获取中..."); + QQBotManager.getInstance().sendMessage(qq,BangumiTools.reportBangumiList()); + }default -> { + if(msg.startsWith(QQGroupCommands.QQ_BANGUMI_SUB)){ + subBanGumi(qq,msg); + } + } + } + + } + private void subBanGumi(long qq,String msg){ + List infos = null; + try { + int id = Integer.parseInt(msg.replace(QQGroupCommands.QQ_BANGUMI_SUB, "").trim()); + infos = BangumiTools.reportBangumiInfo(id); + } catch (Exception e) { + String key = msg.replace(QQGroupCommands.QQ_BANGUMI_SUB, "").trim(); + infos = BangumiTools.reportSearchBangumi(key); + } + for (String info : infos) { + List imgs = new ArrayList<>(); + if (info.contains("")) { + imgs = getImages(info); + for (String img : imgs) { + info = info.replace("", ""); + } + } + sendImagesMsg(imgs,qq, info); + } + } + public static List getImages(String str) { + List list = new ArrayList<>(); + String regex = ""; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(str); + while (matcher.find()) { + list.add(matcher.group().replace("", "") + .trim()); + } + return list; + } + private List files; + private int index = 0; + + private void sendImagesMsg(List imgs,Long qq, String text) { + files = new ArrayList<>(); + index = 0; + if (imgs.size() == 0) { + QQBotManager.getInstance().sendMessage(qq,text); + return; + } + for (String img : imgs) { + HttpTools.download(img,null, new DownloadInterface() { + @Override + public void onDownload(File file) { + super.onDownload(file); + files.add(file); + send(imgs.size(),qq, text); + } + + @Override + public void onError(Exception e) { + super.onError(e); + index++; + send(imgs.size(),qq, text); + } + }); + } + } + + private void send(int size,Long qq, String text) { + if ((files.size() + index) == size) { + String str = QQBotManager.getInstance().sendMessage(files,qq, text); + Log.i("str = " + str); + } + } + + @Override + public boolean isUserPublic() { + return true; + } + + @Override + public String[] getUsePowers() { + return new String[]{ + QQGroupCommands.QQ_BANGUMI_SUB, + QQGroupCommands.QQ_BANGUMI_LIST, + QQGroupCommands.QQ_BANGUMI_TODAY + }; + } +} diff --git a/src/main/java/com/yutou/qqbot/models/Commands/System/Audio.java b/src/main/java/com/yutou/qqbot/models/Commands/System/Audio.java new file mode 100644 index 0000000..ebc3f78 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Commands/System/Audio.java @@ -0,0 +1,36 @@ +package com.yutou.qqbot.models.Commands.System; + +import com.yutou.qqbot.models.Model; +import com.yutou.qqbot.models.audio.QQAudio; +import net.mamoe.mirai.event.events.MessageEvent; + +public class Audio extends Model { + + @Override + public boolean isUserPublic() { + return false; + } + + @Override + public String[] getUsePowers() { + return new String[]{ + QQGroupCommands.QQ_AUDIO, + QQGroupCommands.QQ_AUDIO_OPEN_LAMP, + QQGroupCommands.QQ_AUDIO_OPEN_AIR + }; + } + + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup) { + super.onMessage(qq, event, isGroup); + switch (msg){ + case QQGroupCommands.QQ_AUDIO_OPEN_LAMP -> QQAudio.playText("小爱同学,开灯"); + case QQGroupCommands.QQ_AUDIO_OPEN_AIR -> QQAudio.playText("小爱同学,开空调"); + default -> { + if(msg.startsWith(QQGroupCommands.QQ_AUDIO)){ + QQAudio.playText(msg.replace(QQGroupCommands.QQ_AUDIO, "")); + } + } + } + } +} diff --git a/src/main/java/com/yutou/qqbot/models/Commands/System/BtFlash.java b/src/main/java/com/yutou/qqbot/models/Commands/System/BtFlash.java new file mode 100644 index 0000000..f360b7c --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Commands/System/BtFlash.java @@ -0,0 +1,29 @@ +package com.yutou.qqbot.models.Commands.System; + +import com.yutou.qqbot.QQBotManager; +import com.yutou.qqbot.models.Model; +import com.yutou.qqbot.utlis.HttpTools; +import net.mamoe.mirai.event.events.MessageEvent; + +public class BtFlash extends Model { + @Override + public boolean isUserPublic() { + return false; + } + + @Override + public String[] getUsePowers() { + return new String[]{ + QQGroupCommands.QQ_BT_RELOAD + }; + } + + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup) { + super.onMessage(qq, event, isGroup); + if(msg.equals(QQGroupCommands.QQ_BT_RELOAD)){ + HttpTools.get("http://192.168.31.88:8000/bt/down.do"); + QQBotManager.getInstance().sendMessage(qq,"已提交"); + } + } +} diff --git a/src/main/java/com/yutou/qqbot/models/Commands/System/Cmd.java b/src/main/java/com/yutou/qqbot/models/Commands/System/Cmd.java new file mode 100644 index 0000000..fe85859 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Commands/System/Cmd.java @@ -0,0 +1,28 @@ +package com.yutou.qqbot.models.Commands.System; + +import com.yutou.qqbot.models.Model; +import com.yutou.qqbot.utlis.RedisTools; +import net.mamoe.mirai.event.events.MessageEvent; + +public class Cmd extends Model { + @Override + public boolean isUserPublic() { + + return false; + } + + @Override + public String[] getUsePowers() { + return new String[]{ + QQGroupCommands.QQ_CMD + }; + } + + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup) { + super.onMessage(qq, event, isGroup); + if (msg.startsWith(QQGroupCommands.QQ_CMD)) { + RedisTools.Consumer.system("cmd", msg.replace(QQGroupCommands.QQ_CMD, "")); + } + } +} diff --git a/src/main/java/com/yutou/qqbot/models/Commands/System/Help.java b/src/main/java/com/yutou/qqbot/models/Commands/System/Help.java new file mode 100644 index 0000000..72e100d --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Commands/System/Help.java @@ -0,0 +1,26 @@ +package com.yutou.qqbot.models.Commands.System; + +import com.yutou.qqbot.QQBotManager; +import com.yutou.qqbot.models.Model; +import net.mamoe.mirai.event.events.MessageEvent; + +public class Help extends Model { + + @Override + public boolean isUserPublic() { + return true; + } + + @Override + public String[] getUsePowers() { + return new String[0]; + } + + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup) { + super.onMessage(qq, event, isGroup); + if(msg.equals(QQGroupCommands.QQ_HELP)){ + QQBotManager.getInstance().sendMessage(qq,getCommands(QQGroupCommands.class).toString()); + } + } +} diff --git a/src/main/java/com/yutou/qqbot/models/Commands/System/IP.java b/src/main/java/com/yutou/qqbot/models/Commands/System/IP.java new file mode 100644 index 0000000..adc20e0 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Commands/System/IP.java @@ -0,0 +1,27 @@ +package com.yutou.qqbot.models.Commands.System; + +import com.yutou.qqbot.models.Model; +import com.yutou.qqbot.utlis.RedisTools; +import net.mamoe.mirai.event.events.MessageEvent; + +public class IP extends Model { + @Override + public boolean isUserPublic() { + return false; + } + + @Override + public String[] getUsePowers() { + return new String[]{ + QQGroupCommands.QQ_GET_IP + }; + } + + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup) { + super.onMessage(qq, event, isGroup); + if(msg.equals(QQGroupCommands.QQ_GET_IP)){ + RedisTools.Consumer.bot("getip"); + } + } +} diff --git a/src/main/java/com/yutou/qqbot/models/Commands/System/OpenPC.java b/src/main/java/com/yutou/qqbot/models/Commands/System/OpenPC.java new file mode 100644 index 0000000..3f4c0ae --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Commands/System/OpenPC.java @@ -0,0 +1,30 @@ +package com.yutou.qqbot.models.Commands.System; + +import com.yutou.qqbot.models.Model; +import com.yutou.qqbot.utlis.RedisTools; +import net.mamoe.mirai.event.events.MessageEvent; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class OpenPC extends Model { + @Override + public boolean isUserPublic() { + return false; + } + + @Override + public String[] getUsePowers() { + return new String[]{ + QQGroupCommands.QQ_OPEN_PC + }; + } + + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup) { + super.onMessage(qq, event, isGroup); + if(msg.equals(QQGroupCommands.QQ_OPEN_PC)){ + RedisTools.Consumer.system("openPC", null); + } + } +} 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 new file mode 100644 index 0000000..84f0d92 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Commands/System/Restart.java @@ -0,0 +1,30 @@ +package com.yutou.qqbot.models.Commands.System; + +import com.yutou.qqbot.QQBotManager; +import com.yutou.qqbot.models.Model; +import com.yutou.qqbot.utlis.AppTools; +import net.mamoe.mirai.event.events.MessageEvent; + +public class Restart extends Model { + @Override + public boolean isUserPublic() { + return false; + } + + @Override + public String[] getUsePowers() { + return new String[]{ + QQGroupCommands.QQ_SYSTEM_RESTART + }; + } + + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup) { + 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/Commands/System/ToolsIdea.java b/src/main/java/com/yutou/qqbot/models/Commands/System/ToolsIdea.java new file mode 100644 index 0000000..bab7514 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Commands/System/ToolsIdea.java @@ -0,0 +1,50 @@ +package com.yutou.qqbot.models.Commands.System; + +import com.yutou.qqbot.QQBotManager; +import com.yutou.qqbot.models.Model; +import com.yutou.qqbot.utlis.IdeaTools; +import com.yutou.qqbot.utlis.RedisTools; +import com.yutou.qqbot.utlis.StringUtils; +import net.mamoe.mirai.event.events.MessageEvent; + +public class ToolsIdea extends Model { + @Override + public boolean isUserPublic() { + return false; + } + + @Override + public String[] getUsePowers() { + return new String[]{ + QQGroupCommands.QQ_TOOLS_IDEA, + QQGroupCommands.QQ_TOOLS_IDEA_FILE, + QQGroupCommands.QQ_TOOLS_IDEA + }; + } + + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup) { + super.onMessage(qq, event, isGroup); + if (QQGroupCommands.QQ_TOOLS_IDEA.equals(msg)) { + StringBuilder builder = new StringBuilder(); + if (StringUtils.isEmpty(RedisTools.get("ideaUrl"))) { + builder.append("暂未设置IDEA激活码下载地址"); + } else { + for (String name : IdeaTools.getIdeaList(RedisTools.get("ideaUrl"))) { + builder.append(QQGroupCommands.QQ_TOOLS_IDEA_FILE).append(name).append("\n"); + } + if (builder.toString().trim().length() == 0) { + builder.append("激活码文件中未包含txt文件"); + } + } + QQBotManager.getInstance().sendMessage(qq, builder.toString()); + } else { + if (msg.startsWith(QQGroupCommands.QQ_TOOLS_IDEA_URL)) { + RedisTools.set("ideaUrl", msg.replace(QQGroupCommands.QQ_TOOLS_IDEA_URL, "").trim()); + QQBotManager.getInstance().sendMessage(qq, "已设定下载地址:" + RedisTools.get("ideaUrl")); + } else if (msg.startsWith(QQGroupCommands.QQ_TOOLS_IDEA_FILE)) { + QQBotManager.getInstance().sendMessage(qq, IdeaTools.getIdea(event.getMessage().contentToString().replace(QQGroupCommands.QQ_TOOLS_IDEA_FILE, ""))); + } + } + } +} diff --git a/src/main/java/com/yutou/qqbot/models/Commands/System/UpdateIP.java b/src/main/java/com/yutou/qqbot/models/Commands/System/UpdateIP.java new file mode 100644 index 0000000..b396fc4 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Commands/System/UpdateIP.java @@ -0,0 +1,27 @@ +package com.yutou.qqbot.models.Commands.System; + +import com.yutou.qqbot.models.Model; +import com.yutou.qqbot.utlis.RedisTools; +import net.mamoe.mirai.event.events.MessageEvent; + +public class UpdateIP extends Model { + @Override + public boolean isUserPublic() { + return false; + } + + @Override + public String[] getUsePowers() { + return new String[]{ + QQGroupCommands.QQ_UPDATE_IP + }; + } + + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup) { + super.onMessage(qq, event, isGroup); + if(msg.equals(QQGroupCommands.QQ_UPDATE_IP)){ + RedisTools.Consumer.system("updateIP", null); + } + } +} diff --git a/src/main/java/com/yutou/qqbot/models/Commands/System/Version.java b/src/main/java/com/yutou/qqbot/models/Commands/System/Version.java new file mode 100644 index 0000000..1cffb7e --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Commands/System/Version.java @@ -0,0 +1,27 @@ +package com.yutou.qqbot.models.Commands.System; + +import com.yutou.qqbot.QQBotManager; +import com.yutou.qqbot.models.Model; +import net.mamoe.mirai.event.events.MessageEvent; + +public class Version extends Model { + @Override + public boolean isUserPublic() { + return false; + } + + @Override + public String[] getUsePowers() { + return new String[]{ + QQGroupCommands.QQ_GET_VERSION + }; + } + + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup) { + super.onMessage(qq, event, isGroup); + if(msg.equals(QQGroupCommands.QQ_GET_VERSION)){ + QQBotManager.getInstance().sendVersion(); + } + } +} diff --git a/src/main/java/com/yutou/qqbot/models/Model.java b/src/main/java/com/yutou/qqbot/models/Model.java new file mode 100644 index 0000000..7a7c961 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Model.java @@ -0,0 +1,81 @@ +package com.yutou.qqbot.models; + +import com.yutou.qqbot.QQNumberManager; +import com.yutou.qqbot.interfaces.ModelInterface; +import net.mamoe.mirai.event.events.GroupMessageEvent; +import net.mamoe.mirai.event.events.MessageEvent; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +public abstract class Model implements ModelInterface { + + public static class QQGroupCommands { + public final static String QQ_HELP = "!help"; + public final static String QQ_SYSTEM_RESTART = "!restart"; + public final static String QQ_UPDATE_IP = "!更新ip"; + public final static String QQ_GET_IP = "!ip"; + public final static String QQ_OPEN_PC = "!开机"; + public final static String QQ_GET_VERSION = "!version"; + public final static String QQ_CMD = "!cmd"; + public final static String QQ_BANGUMI_TODAY = "!今日动画"; + public final static String QQ_BANGUMI_LIST = "!新番"; + public final static String QQ_BANGUMI_SUB = "!查动画"; + public final static String QQ_AUDIO = "!语音"; + public final static String QQ_AUDIO_OPEN_LAMP = "!开灯"; + public final static String QQ_AUDIO_OPEN_AIR = "!开空调"; + public final static String QQ_BT_RELOAD = "!刷bt"; + public final static String QQ_TOOLS_IDEA = "!idea"; + public final static String QQ_TOOLS_IDEA_FILE = "!idea>"; + public final static String QQ_TOOLS_IDEA_URL = "!idea_url"; + + public final static String QQ_BANGUMI_INFO = "!保存动画信息>"; + + } + + public static class QQFromCommands { + public static final String ADD_POWER = "大头菜"; + } + + + public static List> classList = new ArrayList<>(); + long group; + + public Model() { + if (!classList.contains(getClass())) { + classList.add(getClass()); + } + + } + + public String msg; + protected boolean isGroupPower = false; + + 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; + } + } + + public void onTime(String time) { + } + + public static StringBuilder getCommands(Class commands) { + StringBuilder builder = new StringBuilder(); + for (Field field : commands.getDeclaredFields()) { + try { + field.setAccessible(true); + builder.append(field.get(null)).append("\n"); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + return builder; + } + +} diff --git a/src/main/java/com/yutou/qqbot/models/setu/QQSetu.java b/src/main/java/com/yutou/qqbot/models/setu/QQSetu.java index 8a49bab..3329c6c 100644 --- a/src/main/java/com/yutou/qqbot/models/setu/QQSetu.java +++ b/src/main/java/com/yutou/qqbot/models/setu/QQSetu.java @@ -3,11 +3,14 @@ package com.yutou.qqbot.models.setu; import com.alibaba.fastjson.JSONObject; +import com.yutou.qqbot.Annotations.UseModel; import com.yutou.qqbot.QQBotManager; +import com.yutou.qqbot.models.Model; import com.yutou.qqbot.utlis.AppTools; import com.yutou.qqbot.utlis.Log; import com.yutou.qqbot.utlis.RedisTools; import net.mamoe.mirai.event.events.GroupMessageEvent; +import net.mamoe.mirai.event.events.MessageEvent; import net.mamoe.mirai.message.data.At; import net.mamoe.mirai.message.data.Image; import net.mamoe.mirai.message.data.MessageChainBuilder; @@ -15,16 +18,22 @@ import net.mamoe.mirai.message.data.MessageChainBuilder; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; -import java.util.HashMap; -import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; +import java.text.SimpleDateFormat; +import java.util.*; -public class QQSetu { - public static void printTodaySetu() { +@UseModel +public class QQSetu extends Model { + + private int db_print =1;//统计结果 + private int db_user =3;//当次数据 + private long group; + private Timer timer; + private final Map setuScore = new HashMap<>(); + + public void printTodaySetu() { String redisKey= AppTools.getToDayTime() + "_setu"; Log.i("今日涩图 redisKey = " + redisKey); - String js = RedisTools.get(redisKey, 1); + String js = RedisTools.get(redisKey, db_print); if (js != null) { JSONObject json = JSONObject.parseObject(js); if(json.containsKey("isPrint")&&json.getBoolean("isPrint")){ @@ -49,7 +58,7 @@ public class QQSetu { setu=json.getJSONObject(groupImage.get(id)); if(setu!=null){ json.put("isPrint",true); - RedisTools.set(1,redisKey,json.toJSONString()); + RedisTools.set(db_print,redisKey,json.toJSONString()); JSONObject info=setu.getJSONObject("info"); JSONObject score=setu.getJSONObject("score"); MessageChainBuilder builder = new MessageChainBuilder(); @@ -87,7 +96,7 @@ public class QQSetu { if (!isSetu(image)) { return; } - if (RedisTools.get(event.getGroup().getId()+"setu") != null) { + if (RedisTools.get(event.getGroup().getId()+"setu", db_user) != null) { printSetu(event.getGroup().getId()); } setuScore.clear(); @@ -96,15 +105,15 @@ public class QQSetu { json.put("sourName", event.getSenderName()); json.put("sourQQ", event.getSender().getId()); json.put("group", event.getGroup().getId()); - RedisTools.set(event.getGroup().getId()+"setu", json.toJSONString(),6*60); + RedisTools.set(event.getGroup().getId()+"setu", json.toJSONString(),6*60, db_user); if (timer != null) { timer.cancel(); timer = null; } - startTimer(event.getGroup().getId()); + startTimer(); } - private void startTimer(long group) { + private void startTimer() { timer = new Timer(); timer.schedule(new TimerTask() { @Override @@ -114,11 +123,11 @@ public class QQSetu { } timer = null; } - }, 5 * 60 * 1000); + }, (long) (0.9 * 60 * 1000)); } private void printSetu(long group) { - JSONObject jt = JSONObject.parseObject(RedisTools.get(group+"setu")); + JSONObject jt = JSONObject.parseObject(RedisTools.get(group+"setu", db_user)); String id = jt.getString("id"); float average = 0; float max = 0; @@ -154,7 +163,7 @@ public class QQSetu { "其中最高分由:" + maxName + " 给与:" + max +"\n "+ "其中最低分由:" + minName + " 给与:" + min; QQBotManager.getInstance().sendMessage(group, builder); - String st = RedisTools.get(AppTools.getToDayTime() + "_setu", 1); + String st = RedisTools.get(AppTools.getToDayTime() + "_setu", db_print); JSONObject json; if (st == null) { json = new JSONObject(); @@ -173,16 +182,23 @@ public class QQSetu { item.put("info", jt); item.put("average", average); json.put(id, item); - RedisTools.set(1, AppTools.getToDayTime() + "_setu", json.toJSONString()); + RedisTools.set(db_print, AppTools.getToDayTime() + "_setu", json.toJSONString()); } - RedisTools.remove(group+"setu",0); + RedisTools.remove(group+"setu",db_user); setuScore.clear(); } - public void setu(String msg, GroupMessageEvent event) { - if (msg.trim().equals("[图片]")) { + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup){ + System.out.println("qq = " + qq + ", event = " + event.getMessage().contentToString() + ", isGroup = " + isGroup); + if(!isGroup){ + return; + } + group=qq; + String msg=event.getMessage().contentToString(); + if ("[图片]".equals(msg.trim())) { Image image = (Image) event.getMessage().stream().filter(Image.class::isInstance).findFirst().orElse(null); if (image != null) { - setuBuilder(image, event); + setuBuilder(image, (GroupMessageEvent) event); return; } } @@ -193,15 +209,30 @@ public class QQSetu { float i = Float.parseFloat(msg.trim()); if (i > 0 && i <= 10) { String name = event.getSenderName(); - String qq=event.getSender().getId()+""; + String sender=event.getSender().getId()+""; if (!setuScore.containsKey(name)) { - setuScore.put(name+"|"+qq, i); + setuScore.put(name+"|"+sender, i); } } } catch (Exception ignored) { } } - private Timer timer; - private static Map setuScore = new HashMap<>(); + @Override + public void onTime(String time) { + super.onTime(time); + if("23:59".equals(time)){ + printTodaySetu(); + } + } + + @Override + public boolean isUserPublic() { + return true; + } + + @Override + public String[] getUsePowers() { + return new String[0]; + } } diff --git a/src/main/java/com/yutou/qqbot/utlis/AppTools.java b/src/main/java/com/yutou/qqbot/utlis/AppTools.java index 6f8b763..7a421c1 100644 --- a/src/main/java/com/yutou/qqbot/utlis/AppTools.java +++ b/src/main/java/com/yutou/qqbot/utlis/AppTools.java @@ -13,10 +13,15 @@ import org.springframework.util.ObjectUtils; import java.io.*; import java.lang.annotation.Annotation; import java.lang.reflect.Method; +import java.net.JarURLConnection; +import java.net.URL; +import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.*; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; public class AppTools { diff --git a/src/main/java/com/yutou/qqbot/utlis/ApplicationInit.java b/src/main/java/com/yutou/qqbot/utlis/ApplicationInit.java new file mode 100644 index 0000000..2cc5d95 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/utlis/ApplicationInit.java @@ -0,0 +1,40 @@ +package com.yutou.qqbot.utlis; + +import com.yutou.qqbot.models.Model; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; + +@Component +public class ApplicationInit implements ApplicationRunner { + @Override + public void run(ApplicationArguments args) throws Exception { + new Timer().schedule(new TimerTask() { + private String oldTime = ""; + @Override + public void run() { + try { + String time = new SimpleDateFormat("HH:mm").format(new Date()); + if (time.equals(oldTime)) { + return; + } + for (Class model : Model.classList) { + try { + Model useModel= (Model) model.getDeclaredConstructor().newInstance(); + useModel.onTime(time); + } catch (Exception e) { + e.printStackTrace(); + } + } + }catch (Exception e){ + e.printStackTrace(); + } + } + },0, 35 * 1000); + } +} diff --git a/src/main/java/com/yutou/qqbot/utlis/RedisTools.java b/src/main/java/com/yutou/qqbot/utlis/RedisTools.java index 210381d..c6bc951 100644 --- a/src/main/java/com/yutou/qqbot/utlis/RedisTools.java +++ b/src/main/java/com/yutou/qqbot/utlis/RedisTools.java @@ -44,7 +44,7 @@ public class RedisTools { Jedis jedis = getRedis(); jedis.select(dbIndex); String ret = jedis.set(key, value); - Log.i("Redis set =" + ret); + Log.i("Redis set =" + ret,"key = "+key+" value = "+value); jedis.close(); } catch (Exception e) { // TODO: handle exception @@ -54,16 +54,15 @@ public class RedisTools { return true; } - public static boolean set(String key, String value) { - return set(0, key, value); - } - - public static boolean set(String key, String value, int timeout) { + public static boolean set (String key,String value, long timeout,int db){ try { if (isNotInstallRedis) { return false; } Jedis jedis = getRedis(); + if(db>-1){ + jedis.select(db); + } if (timeout == -1) { jedis.set(key, value); } else { @@ -78,6 +77,14 @@ public class RedisTools { return true; } + public static boolean set(Object key, String value) { + return set(3, key+"", value); + } + + public static boolean set(String key, String value, long timeout) { + return set(key, value, timeout,3); + } + public static String get(String key, int dbIndex) { String value = "-999"; if (isNotInstallRedis) { @@ -94,12 +101,12 @@ public class RedisTools { return value; } - public static String get(String key) { - return get(key, 0); + public static String get(Object key) { + return get(key+"", 3); } public static boolean remove(String key) { - return remove(key,0); + return remove(key,3); } public static void removeLoginState(String uid) { @@ -119,13 +126,21 @@ public class RedisTools { jedis.close(); return tmp; } - - public static boolean exists(String key, String value) { + public static boolean exists(Object key,String value){ + return exists(3,key,value); + } + public static boolean exists(int db,Object key, String value) { if (isNotInstallRedis) { return false; } Jedis jedis = getRedis(); - boolean flag = value.equals(jedis.get(key)); + jedis.select(db); + boolean flag=false; + if(value==null){ + flag= jedis.exists(key+""); + }else { + flag = value.equals(jedis.get(key+"")); + } jedis.close(); return flag; }