mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-30 06:37:36 +00:00
Merge branch 'mirai'
This commit is contained in:
commit
60e91987d1
2
pom.xml
2
pom.xml
@ -19,7 +19,7 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||||
<mirai.CoreVersion>0.39.4</mirai.CoreVersion>
|
<mirai.CoreVersion>1.0-RC2-1</mirai.CoreVersion>
|
||||||
<mirai.JaptVersion>1.1.1</mirai.JaptVersion>
|
<mirai.JaptVersion>1.1.1</mirai.JaptVersion>
|
||||||
<kotlin.version>1.3.71</kotlin.version>
|
<kotlin.version>1.3.71</kotlin.version>
|
||||||
<ktor.version>1.3.2</ktor.version>
|
<ktor.version>1.3.2</ktor.version>
|
||||||
|
@ -9,9 +9,7 @@ import net.mamoe.mirai.BotFactoryJvm;
|
|||||||
import net.mamoe.mirai.event.events.BotMuteEvent;
|
import net.mamoe.mirai.event.events.BotMuteEvent;
|
||||||
import net.mamoe.mirai.event.events.BotUnmuteEvent;
|
import net.mamoe.mirai.event.events.BotUnmuteEvent;
|
||||||
import net.mamoe.mirai.japt.Events;
|
import net.mamoe.mirai.japt.Events;
|
||||||
import net.mamoe.mirai.message.ContactMessage;
|
import net.mamoe.mirai.message.*;
|
||||||
import net.mamoe.mirai.message.FriendMessage;
|
|
||||||
import net.mamoe.mirai.message.GroupMessage;
|
|
||||||
import net.mamoe.mirai.utils.BotConfiguration;
|
import net.mamoe.mirai.utils.BotConfiguration;
|
||||||
import org.apache.commons.net.util.Base64;
|
import org.apache.commons.net.util.Base64;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -45,9 +43,11 @@ public class MiraiMain implements Closeable {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bot = BotFactoryJvm.newBot(Long.parseLong(botProperties.getProperty("bot.qq", "0")), Base64.decodeBase64(botProperties.getProperty("bot.password", "")), new BotConfiguration());
|
BotConfiguration configuration = new BotConfiguration();
|
||||||
Events.subscribeAlways(GroupMessage.class, this::executeMessageEvent);
|
configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PAD);
|
||||||
Events.subscribeAlways(FriendMessage.class, this::executeMessageEvent);
|
bot = BotFactoryJvm.newBot(Long.parseLong(botProperties.getProperty("bot.qq", "0")), Base64.decodeBase64(botProperties.getProperty("bot.password", "")), configuration);
|
||||||
|
Events.subscribeAlways(GroupMessageEvent.class, this::executeMessageEvent);
|
||||||
|
Events.subscribeAlways(FriendMessageEvent.class, this::executeMessageEvent);
|
||||||
Events.subscribeAlways(BotMuteEvent.class,
|
Events.subscribeAlways(BotMuteEvent.class,
|
||||||
event -> BotEventHandler.setMuteState(event.getGroup().getId(), true));
|
event -> BotEventHandler.setMuteState(event.getGroup().getId(), true));
|
||||||
Events.subscribeAlways(BotUnmuteEvent.class,
|
Events.subscribeAlways(BotUnmuteEvent.class,
|
||||||
@ -62,20 +62,27 @@ public class MiraiMain implements Closeable {
|
|||||||
* 处理消息事件
|
* 处理消息事件
|
||||||
* @param message 消息事件对象
|
* @param message 消息事件对象
|
||||||
*/
|
*/
|
||||||
private void executeMessageEvent(ContactMessage message) {
|
private void executeMessageEvent(MessageEvent message) {
|
||||||
if(message instanceof GroupMessage) {
|
if(message instanceof GroupMessageEvent) {
|
||||||
GroupMessage groupMessage = (GroupMessage) message;
|
GroupMessageEvent GroupMessageEvent = (GroupMessageEvent) message;
|
||||||
if(BotEventHandler.isMute(groupMessage.getGroup().getId(), true) == null) {
|
if(BotEventHandler.isMute(GroupMessageEvent.getGroup().getId(), true) == null) {
|
||||||
BotEventHandler.setMuteState(groupMessage.getGroup().getId(),
|
BotEventHandler.setMuteState(GroupMessageEvent.getGroup().getId(),
|
||||||
((GroupMessage) message).getGroup().getBotMuteRemaining() != 0);
|
((GroupMessageEvent) message).getGroup().getBotMuteRemaining() != 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BotEventHandler.executeMessageEvent(new MiraiMessageEvent(message));
|
BotEventHandler.executeMessageEvent(new MiraiMessageEvent(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭机器人
|
||||||
|
*/
|
||||||
public void close() {
|
public void close() {
|
||||||
|
if(bot == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
log.warn("正在关闭机器人...");
|
log.warn("正在关闭机器人...");
|
||||||
bot.close(null);
|
bot.close(null);
|
||||||
|
bot = null;
|
||||||
log.warn("机器人已关闭.");
|
log.warn("机器人已关闭.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,29 +1,27 @@
|
|||||||
package net.lamgc.cgj.bot.framework.mirai.message;
|
package net.lamgc.cgj.bot.framework.mirai.message;
|
||||||
|
|
||||||
import net.lamgc.cgj.bot.event.MessageEvent;
|
|
||||||
import net.lamgc.cgj.bot.message.MessageSender;
|
import net.lamgc.cgj.bot.message.MessageSender;
|
||||||
import net.lamgc.cgj.bot.message.MessageSource;
|
import net.lamgc.cgj.bot.message.MessageSource;
|
||||||
import net.mamoe.mirai.message.ContactMessage;
|
import net.mamoe.mirai.message.GroupMessageEvent;
|
||||||
import net.mamoe.mirai.message.GroupMessage;
|
import net.mamoe.mirai.message.MessageEvent;
|
||||||
import net.mamoe.mirai.message.data.MessageUtils;
|
import net.mamoe.mirai.message.data.MessageUtils;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class MiraiMessageEvent extends MessageEvent {
|
public class MiraiMessageEvent extends net.lamgc.cgj.bot.event.MessageEvent {
|
||||||
|
|
||||||
private final ContactMessage messageObject;
|
private final MessageEvent messageObject;
|
||||||
private final MessageSender messageSender;
|
private final MessageSender messageSender;
|
||||||
|
|
||||||
public MiraiMessageEvent(ContactMessage message) {
|
public MiraiMessageEvent(MessageEvent message) {
|
||||||
super(message instanceof GroupMessage ? ((GroupMessage) message).getGroup().getId() : 0,
|
super(message instanceof GroupMessageEvent ? ((GroupMessageEvent) message).getGroup().getId() : 0,
|
||||||
message.getSender().getId(), getMessageBodyWithoutSource(message.getMessage().toString()));
|
message.getSender().getId(), getMessageBodyWithoutSource(message.getMessage().toString()));
|
||||||
this.messageObject = Objects.requireNonNull(message);
|
this.messageObject = Objects.requireNonNull(message);
|
||||||
if(message instanceof GroupMessage) {
|
if(message instanceof GroupMessageEvent) {
|
||||||
messageSender = new MiraiMessageSender(((GroupMessage) message).getGroup(), MessageSource.Group);
|
messageSender = new MiraiMessageSender(((GroupMessageEvent) message).getGroup(), MessageSource.Group);
|
||||||
} else {
|
} else {
|
||||||
messageSender = new MiraiMessageSender(message.getSender(), MessageSource.Private);
|
messageSender = new MiraiMessageSender(message.getSender(), MessageSource.Private);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -60,7 +60,7 @@ public class MiraiMessageSender implements MessageSender {
|
|||||||
public int sendMessage(final String message) {
|
public int sendMessage(final String message) {
|
||||||
log.debug("处理前的消息内容:\n{}", message);
|
log.debug("处理前的消息内容:\n{}", message);
|
||||||
Message msgBody = processMessage(Objects.requireNonNull(message));
|
Message msgBody = processMessage(Objects.requireNonNull(message));
|
||||||
log.debug("处理后的消息内容(可能出现乱序的情况, 但实际上顺序是没问题的):\n{}", msgBody);
|
log.debug("处理后的消息内容(可能出现乱序的情况, 但实际上顺序是没问题的):\n{}", msgBody.contentToString());
|
||||||
member.sendMessage(msgBody);
|
member.sendMessage(msgBody);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@ import net.lamgc.cgj.bot.message.MessageSenderFactory;
|
|||||||
import net.lamgc.cgj.bot.message.MessageSource;
|
import net.lamgc.cgj.bot.message.MessageSource;
|
||||||
import net.mamoe.mirai.Bot;
|
import net.mamoe.mirai.Bot;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class MiraiMessageSenderFactory implements MessageSenderFactory {
|
public class MiraiMessageSenderFactory implements MessageSenderFactory {
|
||||||
|
|
||||||
private final Bot bot;
|
private final Bot bot;
|
||||||
@ -14,15 +16,11 @@ public class MiraiMessageSenderFactory implements MessageSenderFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MessageSender createMessageSender(MessageSource source, long id) throws Exception {
|
public MessageSender createMessageSender(MessageSource source, long id) {
|
||||||
switch(source) {
|
Objects.requireNonNull(source);
|
||||||
case Group:
|
if(id <= 0) {
|
||||||
case Discuss:
|
throw new IllegalArgumentException("id cannot be 0 or negative: " + id);
|
||||||
return new MiraiMessageSender(bot.getGroup(id), source);
|
}
|
||||||
case Private:
|
return new MiraiMessageSender(bot, source, id);
|
||||||
return new MiraiMessageSender(bot.getFriend(id), source);
|
|
||||||
default:
|
|
||||||
throw new NoSuchFieldException(source.toString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user