修改部分QQ发送消息的代码

This commit is contained in:
Yutousama 2022-01-16 12:51:26 +08:00
parent 11534b04f7
commit 6d9ba12cf1
7 changed files with 48 additions and 312 deletions

View File

@ -30,20 +30,7 @@ public class QQBot {
if(StringUtils.isEmpty(imgUrl)){ if(StringUtils.isEmpty(imgUrl)){
ret= QQBotManager.getInstance().sendMessage(msg); ret= QQBotManager.getInstance().sendMessage(msg);
}else{ }else{
Tools.download(imgUrl, new DownloadInterface() { ret=QQBotManager.getInstance().sendMessage(imgUrl,msg);
@Override
public void onDownload(File file) {
super.onDownload(file);
QQBotManager.getInstance().sendMessage(file,msg);
}
@Override
public void onError(Exception e) {
super.onError(e);
QQBotManager.getInstance().sendMessage(imgUrl+"\n"+msg);
}
});
ret="图片下载中";
} }
json.put("code",0); json.put("code",0);
json.put("msg",ret); json.put("msg",ret);
@ -66,7 +53,7 @@ public class QQBot {
String ret; String ret;
try { try {
String path = Tools.createFile("qq_image",file, file.getName()); String path = Tools.createFile("qq_image",file, file.getName());
ret=QQBotManager.getInstance().sendMessage(new File(path),msg); ret=QQBotManager.getInstance().sendMessage(path,msg);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
ret=e.getLocalizedMessage(); ret=e.getLocalizedMessage();

View File

@ -12,12 +12,12 @@ import org.springframework.context.annotation.Import;
@Import(BTDownloadManager.class) @Import(BTDownloadManager.class)
@SpringBootApplication @SpringBootApplication
public class NasApplication { public class NasApplication {
public static final String version="1.2.5"; public static final String version="1.2.6";
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(NasApplication.class, args); SpringApplication.run(NasApplication.class, args);
AppData.defaultMusicPath = (String) ConfigTools.load(ConfigTools.CONFIG, "musicDir"); AppData.defaultMusicPath = (String) ConfigTools.load(ConfigTools.CONFIG, "musicDir");
RedisTools.initRedisPoolSub(); RedisTools.initRedisPoolSub();
QQBotManager.getInstance(); QQBotManager.getInstance().sendMessage("NasManager已上线:"+version);
} }
} }

View File

@ -1,206 +0,0 @@
package com.yutou.nas.other;
import com.alibaba.fastjson.JSONObject;
import com.yutou.nas.utils.Log;
import com.yutou.nas.utils.QQBotManager;
import com.yutou.nas.utils.RedisTools;
import com.yutou.nas.utils.Tools;
import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.Image;
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;
public class QQSetu {
public static void printTodaySetu() {
String redisKey=Tools.getToDayTime() + "_setu";
com.yutou.nas.utils.Log.i("今日涩图 redisKey = " + redisKey);
String js = RedisTools.get(redisKey, 1);
if (js != null) {
JSONObject json = JSONObject.parseObject(js);
if(json.containsKey("isPrint")&&json.getBoolean("isPrint")){
return;
}
Map<String,Float> groupAverage=new HashMap<>();
Map<String,String> groupImage=new HashMap<>();
JSONObject setu=null;
for (String id : json.keySet()) {
String group=json.getJSONObject(id).getJSONObject("info").getLong("group")+"";
if(groupAverage.containsKey(group)){
if(groupAverage.get(group)<=json.getJSONObject(id).getFloat("average")){
groupAverage.put(group,json.getJSONObject(id).getFloat("average"));
groupImage.put(group,id);
}
}else{
groupAverage.put(group,json.getJSONObject(id).getFloat("average"));
groupImage.put(group,id);
}
}
for (String id : groupImage.keySet()) {
setu=json.getJSONObject(groupImage.get(id));
if(setu!=null){
json.put("isPrint",true);
RedisTools.set(1,redisKey,json.toJSONString());
JSONObject info=setu.getJSONObject("info");
JSONObject score=setu.getJSONObject("score");
MessageChainBuilder builder = new MessageChainBuilder();
builder.append(Image.fromId(info.getString("id")));
builder.append("本日最佳涩图由").append(new At(info.getLong("sourQQ"))).append("提供\n");
builder.append("获得分数 ").append(String.valueOf(setu.getFloat("average"))).append("\n");
builder.append("共有 ").append(String.valueOf(score.getIntValue("userNumber"))).append(" 人参与投票");
QQBotManager.getInstance().sendMessage(info.getLong("group"),builder);
Log.i("今日涩图:"+builder.toString());
}
}
}else {
Log.i("今日没有涩图");
}
}
private boolean isSetu(Image image) {
String url = Image.queryUrl(image);
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
int length = connection.getContentLength();
connection.disconnect();
if (length > 50000) {
return true;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
return false;
}
private void setuBuilder(Image image, GroupMessageEvent event) {
if (!isSetu(image)) {
return;
}
if (RedisTools.get(event.getGroup().getId()+"setu") != null) {
printSetu(event.getGroup().getId());
}
setuScore.clear();
JSONObject json = new JSONObject();
json.put("id", image.getImageId());
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);
if (timer != null) {
timer.cancel();
timer = null;
}
startTimer(event.getGroup().getId());
}
private void startTimer(long group) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(!setuScore.isEmpty()){
printSetu(group);
}
timer = null;
}
}, 5 * 60 * 1000);
}
private void printSetu(long group) {
JSONObject jt = JSONObject.parseObject(RedisTools.get(group+"setu"));
String id = jt.getString("id");
float average = 0;
float max = 0;
float min = 10;
float length = 0;
String maxName = "";
String minName = "";
if(setuScore.size()<=1){
return;
}
for (String name : setuScore.keySet()) {
length += setuScore.get(name);
average += setuScore.get(name);
if (setuScore.get(name) > max) {
max = setuScore.get(name);
maxName = name.split("\\|")[0];
}
if (setuScore.get(name) < min) {
min = setuScore.get(name);
minName = name.split("\\|")[0];
}
}
JSONObject score = new JSONObject();
score.put("max", max);
score.put("min", min);
score.put("sum", length);
score.put("maxName", maxName);
score.put("minName", minName);
score.put("userNumber", setuScore.size());
average = average / setuScore.size();
String builder = "涩图评分:" + average +"\n "+
"其中最高分由:" + maxName + " 给与:" + max +"\n "+
"其中最低分由:" + minName + " 给与:" + min;
QQBotManager.getInstance().sendMessage(group, builder);
String st = RedisTools.get(Tools.getToDayTime() + "_setu", 1);
JSONObject json;
if (st == null) {
json = new JSONObject();
} else {
json = JSONObject.parseObject(st);
}
if (!json.containsKey(id)) {
JSONObject item;
if (json.containsKey("item")) {
item = json.getJSONObject("item");
} else {
item = new JSONObject();
}
item.put("score", score);
item.put("info", jt);
item.put("average", average);
json.put(id, item);
RedisTools.set(1, Tools.getToDayTime() + "_setu", json.toJSONString());
}
RedisTools.remove(group+"setu",0);
setuScore.clear();
}
public void setu(String msg, GroupMessageEvent event) {
if (msg.trim().equals("[图片]")) {
Image image = (Image) event.getMessage().stream().filter(Image.class::isInstance).findFirst().orElse(null);
if (image != null) {
setuBuilder(image, event);
return;
}
}
try {
if(msg.trim().length()>3){
return;
}
float i = Float.parseFloat(msg.trim());
if (i > 0 && i <= 10) {
String name = event.getSenderName();
String qq=event.getSender().getId()+"";
if (!setuScore.containsKey(name)) {
setuScore.put(name+"|"+qq, i);
}
}
} catch (Exception ignored) {
}
}
private Timer timer;
private static Map<String, Float> setuScore = new HashMap<>();
}

View File

@ -1,7 +1,6 @@
package com.yutou.nas.utils; package com.yutou.nas.utils;
import com.yutou.nas.Services.impl.MusicToolsServiceImpl; import com.yutou.nas.Services.impl.MusicToolsServiceImpl;
import com.yutou.nas.other.QQSetu;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -68,7 +67,6 @@ public class ApplicationInit implements ApplicationRunner {
QQBotManager.getInstance().reportToDayBangumi(); QQBotManager.getInstance().reportToDayBangumi();
break; break;
case "23:59": case "23:59":
QQSetu.printTodaySetu();
break; break;
} }

View File

@ -15,7 +15,10 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class BTDownloadManager implements ApplicationContextAware { public class BTDownloadManager implements ApplicationContextAware {
@ -97,8 +100,7 @@ public class BTDownloadManager implements ApplicationContextAware {
array.add(item.getString("title")); array.add(item.getString("title"));
RedisTools.set(bangumiItem.getTitle(), array.toJSONString()); RedisTools.set(bangumiItem.getTitle(), array.toJSONString());
if (item.containsKey("thumbnail")) { if (item.containsKey("thumbnail")) {
File file = HttpTools.syncDownload(item.getString("thumbnail"), bangumiItem.getTitle() + ".jpg"); onSend(item.getString("thumbnail"), item);
onSend(file, item);
} else { } else {
onSend(null, item); onSend(null, item);
} }
@ -114,13 +116,13 @@ public class BTDownloadManager implements ApplicationContextAware {
} }
} }
private void onSend(File file, JSONObject item) { private void onSend(String fileUrl, JSONObject item) {
String text = "启动下载器\n已提交到下载:" + item.getString("title"); String text = "启动下载器\n已提交到下载:" + item.getString("title");
Log.i(text); Log.i(text);
if (file == null) { if (fileUrl == null) {
QQBotManager.getInstance().sendMessage(text); QQBotManager.getInstance().sendMessage(text);
} else { } else {
QQBotManager.getInstance().sendMessage(file, text); QQBotManager.getInstance().sendMessage(fileUrl, text);
} }
} }
@ -163,15 +165,7 @@ public class BTDownloadManager implements ApplicationContextAware {
} }
public static void main(String[] args) { public static void main(String[] args) {
AnimationData data = new AnimationData();
BangumiItem item = new BangumiItem();
item.setAuthor(data.nameToValue("lleeopen", true) + "");
item.setCategories(data.nameToValue("動畫", false) + "");
item.setTitle("無職轉生~到了異世界就拿出真本事~");
item.setTitlekey("无职转生");
String url = new BTDownloadManager().getRSSUrl(item);
com.yutou.nas.utils.Log.i(url);
} }
@Override @Override

View File

@ -1,10 +1,10 @@
package com.yutou.nas.utils; package com.yutou.nas.utils;
import com.alibaba.fastjson.JSONObject;
import com.yutou.nas.NasApplication; import com.yutou.nas.NasApplication;
import com.yutou.nas.bangumi.BangumiTools; import com.yutou.nas.bangumi.BangumiTools;
import com.yutou.nas.interfaces.DownloadInterface; import com.yutou.nas.interfaces.DownloadInterface;
import com.yutou.nas.other.QQAudio; import com.yutou.nas.other.QQAudio;
import com.yutou.nas.other.QQSetu;
import net.mamoe.mirai.Bot; import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactory; import net.mamoe.mirai.BotFactory;
import net.mamoe.mirai.event.GlobalEventChannel; import net.mamoe.mirai.event.GlobalEventChannel;
@ -13,15 +13,12 @@ import net.mamoe.mirai.message.data.Image;
import net.mamoe.mirai.message.data.MessageChainBuilder; import net.mamoe.mirai.message.data.MessageChainBuilder;
import net.mamoe.mirai.utils.BotConfiguration; import net.mamoe.mirai.utils.BotConfiguration;
import net.mamoe.mirai.utils.ExternalResource; import net.mamoe.mirai.utils.ExternalResource;
import com.yutou.nas.utils.StringUtils;
import java.io.File; import java.io.File;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -48,7 +45,7 @@ public class QQBotManager {
private final static String QQ_TOOLS_IDEA_FILE = "!idea>"; private final static String QQ_TOOLS_IDEA_FILE = "!idea>";
private final static String QQ_TOOLS_IDEA_URL = "!idea_url"; private final static String QQ_TOOLS_IDEA_URL = "!idea_url";
public final static String QQ_BANGUMI_INFO="!保存动画信息>"; public final static String QQ_BANGUMI_INFO = "!保存动画信息>";
} }
private static QQBotManager botManager = null; private static QQBotManager botManager = null;
@ -99,7 +96,7 @@ public class QQBotManager {
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
String str = sendMessage("姬妻酱上线拉~☆Daze~ 当前版本:"+NasApplication.version); String str = sendMessage("姬妻酱上线拉~☆Daze~ 当前版本:" + NasApplication.version);
com.yutou.nas.utils.Log.i(str); com.yutou.nas.utils.Log.i(str);
} }
@ -138,25 +135,21 @@ public class QQBotManager {
} }
public String sendMessage(String text) { public String sendMessage(String text) {
if (bot != null&&!StringUtils.isEmpty(text)) { JSONObject json = new JSONObject();
try { json.put("qq", 891655174);
return Objects.requireNonNull(bot.getGroup(qqGroup)).sendMessage(text).toString(); json.put("message",text);
} catch (Exception e) { Map<String, String> header = new HashMap<>();
e.printStackTrace(); header.put("content-type", "application/json");
} return HttpTools.http_post("http://192.168.31.88:802/qq/send.do", json.toString().getBytes(StandardCharsets.UTF_8), 1, header);
}
return getNotLoginQQ();
} }
public String sendMessage(Long group, String text) { public String sendMessage(Long group, String text) {
if (bot != null) { JSONObject json = new JSONObject();
try { json.put("qq", group);
return Objects.requireNonNull(bot.getGroup(group)).sendMessage(text).toString(); json.put("message",text);
} catch (Exception e) { Map<String, String> header = new HashMap<>();
e.printStackTrace(); header.put("content-type", "application/json");
} return HttpTools.http_post("http://192.168.31.88:802/qq/send.do", json.toString().getBytes(StandardCharsets.UTF_8), 1, header);
}
return getNotLoginQQ();
} }
public void sendMessage(Long group, MessageChainBuilder builder) { public void sendMessage(Long group, MessageChainBuilder builder) {
@ -166,22 +159,14 @@ public class QQBotManager {
} }
public String sendMessage(File imageFile, String text) { public String sendMessage(String imageUrl, String message) {
try { JSONObject json = new JSONObject();
if (bot != null) { json.put("qq", 891655174);
Image image = getImage(imageFile); json.put("message",message);
MessageChainBuilder builder = new MessageChainBuilder(); json.put("image", imageUrl);
if (image != null) { Map<String, String> header = new HashMap<>();
builder.append(image); header.put("content-type", "application/json");
} return HttpTools.http_post("http://192.168.31.88:802/qq/send.do", json.toString().getBytes(StandardCharsets.UTF_8), 1, header);
builder.append(text);
return Objects.requireNonNull(bot.getGroup(qqGroup)).sendMessage(builder.asMessageChain()).toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return getNotLoginQQ();
} }
public String sendMessage(List<File> imgs, String text) { public String sendMessage(List<File> imgs, String text) {
@ -219,20 +204,13 @@ public class QQBotManager {
@Override @Override
public void accept(GroupMessageEvent event) { public void accept(GroupMessageEvent event) {
String msg = event.getMessage().contentToString(); 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) { private void myGroup(String msg) {
StringBuilder builder; StringBuilder builder;
String msgSrc=msg; String msgSrc = msg;
msg = msg.replace("", "!").toLowerCase(); msg = msg.replace("", "!").toLowerCase();
Log.i("QQBot","command = "+msg); Log.i("QQBot", "command = " + msg);
switch (msg) { switch (msg) {
case QQCommands.QQ_OPEN_PC: case QQCommands.QQ_OPEN_PC:
RedisTools.Consumer.system("openPC", null); RedisTools.Consumer.system("openPC", null);
@ -269,7 +247,7 @@ public class QQBotManager {
case QQCommands.QQ_SYSTEM_RESTART: case QQCommands.QQ_SYSTEM_RESTART:
getInstance().sendMessage("正在重启服务"); getInstance().sendMessage("正在重启服务");
System.out.println("结束进程"); System.out.println("结束进程");
AppTools.exec("cd /home/yutou/public/servier/tools && ./start.sh",null,true,false); AppTools.exec("cd /home/yutou/public/servier/tools && ./start.sh", null, true, false);
break; break;
case QQCommands.QQ_HELP: case QQCommands.QQ_HELP:
builder = new StringBuilder(); builder = new StringBuilder();
@ -285,13 +263,13 @@ public class QQBotManager {
break; break;
case QQCommands.QQ_TOOLS_IDEA: case QQCommands.QQ_TOOLS_IDEA:
builder = new StringBuilder(); builder = new StringBuilder();
if(StringUtils.isEmpty(RedisTools.get("ideaUrl"))){ if (StringUtils.isEmpty(RedisTools.get("ideaUrl"))) {
builder.append("暂未设置IDEA激活码下载地址"); builder.append("暂未设置IDEA激活码下载地址");
}else { } else {
for (String name : IdeaTools.getIdeaList(RedisTools.get("ideaUrl"))) { for (String name : IdeaTools.getIdeaList(RedisTools.get("ideaUrl"))) {
builder.append(QQCommands.QQ_TOOLS_IDEA_FILE).append(name).append("\n"); builder.append(QQCommands.QQ_TOOLS_IDEA_FILE).append(name).append("\n");
} }
if(builder.toString().trim().length()==0){ if (builder.toString().trim().length() == 0) {
builder.append("激活码文件中未包含txt文件"); builder.append("激活码文件中未包含txt文件");
} }
} }
@ -322,11 +300,11 @@ public class QQBotManager {
} else if (msg.startsWith(QQCommands.QQ_AUDIO)) { } else if (msg.startsWith(QQCommands.QQ_AUDIO)) {
QQAudio.playText(msg.replace(QQCommands.QQ_AUDIO, "")); QQAudio.playText(msg.replace(QQCommands.QQ_AUDIO, ""));
}else if(msg.startsWith(QQCommands.QQ_TOOLS_IDEA_FILE)){ } else if (msg.startsWith(QQCommands.QQ_TOOLS_IDEA_FILE)) {
getInstance().sendMessage(IdeaTools.getIdea(msgSrc.replace(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)){ } else if (msg.startsWith(QQCommands.QQ_TOOLS_IDEA_URL)) {
RedisTools.set("ideaUrl",msg.replace(QQCommands.QQ_TOOLS_IDEA_URL,"").trim()); RedisTools.set("ideaUrl", msg.replace(QQCommands.QQ_TOOLS_IDEA_URL, "").trim());
getInstance().sendMessage("已设定下载地址:"+RedisTools.get("ideaUrl")); getInstance().sendMessage("已设定下载地址:" + RedisTools.get("ideaUrl"));
} }
} }
} }

View File

@ -111,23 +111,8 @@ public class Tools {
if (img == null) { if (img == null) {
QQBotManager.getInstance().sendMessage(title + "\n" + msg); QQBotManager.getInstance().sendMessage(title + "\n" + msg);
} else { } else {
String finalMsg = msg; QQBotManager.getInstance().sendMessage(img, title + "\n" + msg);
String finalImg = img;
if (QQBotManager.getInstance().isLogin()) {
download(img, new DownloadInterface() {
@Override
public void onDownload(File file) {
super.onDownload(file);
QQBotManager.getInstance().sendMessage(file, title + "\n" + finalMsg);
}
@Override
public void onError(Exception e) {
super.onError(e);
QQBotManager.getInstance().sendMessage(title + "\n" + finalMsg + "\n" + finalImg);
}
});
}
} }
} else if (!StringUtils.isEmpty(UpdateIp.nas_ip)) { } else if (!StringUtils.isEmpty(UpdateIp.nas_ip)) {
String img = null; String img = null;