use of net.mamoe.mirai.utils.BotConfiguration in project AngelinaFrame by Strelizia02.
the class MiraiFrameUtil method startMirai.
/**
* Mirai框架的启动方法,一次性启动Mirai并开启消息监听
*/
public void startMirai() {
// 判断一下qq数量和密码数量是否匹配
if (qqList.length == 0 || qqList.length != pwList.length) {
throw new AngelinaException("当前配置文件中账号密码配置有误,请审视配置文件");
}
for (int i = 0; i < qqList.length; i++) {
// 循环登录所有配置的qq账号,如果有需要滑块验证的,需要单独解决
Bot bot = BotFactory.INSTANCE.newBot(Long.parseLong(qqList[i]), pwList[i], new BotConfiguration() {
{
setProtocol(MiraiProtocol.IPAD);
setDeviceInfo(bot -> new DeviceInfo("Huawei.856832.001".getBytes(StandardCharsets.UTF_8), "nova75g".getBytes(StandardCharsets.UTF_8), "JEF-AN20".getBytes(StandardCharsets.UTF_8), "Huawei Kirin 985".getBytes(StandardCharsets.UTF_8), "Huawei".getBytes(StandardCharsets.UTF_8), "Nova 7".getBytes(StandardCharsets.UTF_8), "HarmonyOS 2.0".getBytes(StandardCharsets.UTF_8), "Huawei/Nova/nova:7/MIRAI.200122.001/2736748:user/release-keys".getBytes(StandardCharsets.UTF_8), "1BBBCCA8-0B4A-2EFC-BE95-E732C84DA5F0".getBytes(StandardCharsets.UTF_8), "HarmonyOS version 2.0.0.221(C00E208R6P8)".getBytes(StandardCharsets.UTF_8), "unknown".getBytes(StandardCharsets.UTF_8), new DeviceInfo.Version(), "T-Mobile".getBytes(StandardCharsets.UTF_8), "HarmonyOS".getBytes(StandardCharsets.UTF_8), "02:00:00:00:00:00".getBytes(StandardCharsets.UTF_8), "02:00:00:00:00:00".getBytes(StandardCharsets.UTF_8), "Strelitzia".getBytes(StandardCharsets.UTF_8), "6e096dd53aa9062c".getBytes(StandardCharsets.UTF_8), "342086728277870", "wifi".getBytes(StandardCharsets.UTF_8)));
}
});
bot.login();
}
reBuildBotGroupMap();
// 监听全部qq账号所接受的消息
GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessageEvent.class, event -> {
if (messageIdMap.get(event.getGroup().getId()) == event.getBot().getId()) {
MessageInfo messageInfo = new MessageInfo(event, botNames);
try {
groupChatController.receive(messageInfo);
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
});
// 某个Bot被踢出群或退群
GlobalEventChannel.INSTANCE.subscribeAlways(BotLeaveEvent.class, event -> {
reBuildBotGroupMap();
});
// 某个Bot加入了一个新群
GlobalEventChannel.INSTANCE.subscribeAlways(BotJoinGroupEvent.class, event -> {
messageIdMap.put(event.getGroupId(), event.getBot().getId());
activityMapper.getEventMessage();
MessageInfo messageInfo = new MessageInfo();
messageInfo.setEvent(EventEnum.MemberJoinEvent);
messageInfo.setLoginQq(event.getBot().getId());
messageInfo.setGroupId(event.getGroup().getId());
try {
eventsController.receive(messageInfo);
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
});
// 群撤回消息
GlobalEventChannel.INSTANCE.subscribeAlways(MessageRecallEvent.GroupRecall.class, event -> {
if (messageIdMap.get(event.getGroup().getId()) == event.getBot().getId()) {
activityMapper.getEventMessage();
MessageInfo messageInfo = new MessageInfo();
messageInfo.setEvent(EventEnum.GroupRecall);
messageInfo.setLoginQq(event.getBot().getId());
messageInfo.setGroupId(event.getGroup().getId());
messageInfo.setQq(event.getAuthorId());
try {
eventsController.receive(messageInfo);
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
});
// 戳一戳
GlobalEventChannel.INSTANCE.subscribeAlways(NudgeEvent.class, event -> {
Contact subject = event.getSubject();
if (subject instanceof Group) {
if (messageIdMap.get(subject.getId()) == event.getBot().getId() && event.getTarget() instanceof Bot) {
activityMapper.getEventMessage();
MessageInfo messageInfo = new MessageInfo();
messageInfo.setEvent(EventEnum.NudgeEvent);
messageInfo.setLoginQq(event.getBot().getId());
messageInfo.setGroupId(subject.getId());
messageInfo.setQq(event.getFrom().getId());
try {
eventsController.receive(messageInfo);
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
});
// 某人进群
GlobalEventChannel.INSTANCE.subscribeAlways(MemberJoinEvent.class, event -> {
if (messageIdMap.get(event.getGroup().getId()) == event.getBot().getId()) {
activityMapper.getEventMessage();
MessageInfo messageInfo = new MessageInfo();
messageInfo.setEvent(EventEnum.MemberJoinEvent);
messageInfo.setLoginQq(event.getBot().getId());
messageInfo.setGroupId(event.getGroup().getId());
messageInfo.setQq(event.getMember().getId());
messageInfo.setName(event.getMember().getNick());
try {
eventsController.receive(messageInfo);
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
});
// 某人退群
GlobalEventChannel.INSTANCE.subscribeAlways(MemberLeaveEvent.class, event -> {
if (messageIdMap.get(event.getGroup().getId()) == event.getBot().getId()) {
activityMapper.getEventMessage();
MessageInfo messageInfo = new MessageInfo();
messageInfo.setEvent(EventEnum.MemberLeaveEvent);
messageInfo.setLoginQq(event.getBot().getId());
messageInfo.setGroupId(event.getGroup().getId());
messageInfo.setQq(event.getMember().getId());
messageInfo.setName(event.getMember().getNick());
try {
eventsController.receive(messageInfo);
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
});
// 账号掉线
GlobalEventChannel.INSTANCE.subscribeAlways(BotOfflineEvent.class, event -> {
reBuildBotGroupMap();
});
// 账号掉线
GlobalEventChannel.INSTANCE.subscribeAlways(BotMuteEvent.class, event -> {
if (event.component1() > 60 * 60 * 24) {
event.getGroup().quit();
}
});
// 账号上线
GlobalEventChannel.INSTANCE.subscribeAlways(BotOnlineEvent.class, event -> {
reBuildBotGroupMap();
});
// 好友消息
GlobalEventChannel.INSTANCE.subscribeAlways(FriendMessageEvent.class, event -> {
MessageInfo messageInfo = new MessageInfo(event, botNames);
activityMapper.getFriendMessage();
try {
friendChatController.receive(messageInfo);
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
});
}
use of net.mamoe.mirai.utils.BotConfiguration in project su-sunday-cloud by illeagalName.
the class QqBotConfig method bot.
@Bean
@ConditionalOnProperty(prefix = "qq.config", name = "startStatus", havingValue = "1")
public Bot bot(QqProperties qqProperties) {
Bot bot = BotFactory.INSTANCE.newBot(Long.parseLong(qqProperties.getAccount()), qqProperties.getPassword(), new BotConfiguration() {
{
setWorkingDir(new File(qqProperties.getDirectory()));
// 使用 device.json 存储设备信息
fileBasedDeviceInfo("device.json");
// 切换协议
setProtocol(MiraiProtocol.ANDROID_PHONE);
redirectNetworkLogToFile();
redirectNetworkLogToDirectory();
}
});
bot.login();
// 设置https协议,解决SSL peer shut down incorrectly的异常
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2,SSLv3");
log.info("======注册监听======");
GlobalEventChannel.INSTANCE.registerListenerHost(eventListener);
return bot;
}
use of net.mamoe.mirai.utils.BotConfiguration in project FocessQQ by MidCoard.
the class SimpleBotManager method login.
@Override
public boolean login(Bot b) throws BotLoginException {
if (!b.isOnline() && b instanceof SimpleBot) {
long id = b.getId();
BotConfiguration configuration = BotConfiguration.getDefault();
configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PAD);
File cache = new File("devices/" + id + "/cache");
if (!cache.exists())
if (!cache.mkdirs())
throw new BotLoginException(id, FocessQQ.getLangConfig().get("fatal-create-cache-dir-failed"));
configuration.fileBasedDeviceInfo("devices/" + id + "/device.json");
configuration.setCacheDir(cache);
configuration.setLoginSolver(new LoginSolver() {
@Nullable
@Override
public Object onSolvePicCaptcha(@NotNull net.mamoe.mirai.Bot bot, @NotNull byte[] bytes, @NotNull Continuation<? super String> continuation) {
try {
FileImageOutputStream outputStream = new FileImageOutputStream(new File("captcha.jpg"));
outputStream.write(bytes);
outputStream.close();
} catch (IOException e) {
FocessQQ.getLogger().thrLang("exception-load-captcha-picture", e);
}
FocessQQ.getLogger().infoLang("input-captcha-code");
try {
return IOHandler.getConsoleIoHandler().input();
} catch (InputTimeoutException e) {
return null;
}
}
@Nullable
@Override
public Object onSolveSliderCaptcha(@NotNull net.mamoe.mirai.Bot bot, @NotNull String s, @NotNull Continuation<? super String> continuation) {
FocessQQ.getLogger().info(s);
try {
IOHandler.getConsoleIoHandler().input();
} catch (InputTimeoutException ignored) {
}
return null;
}
@Nullable
@Override
public Object onSolveUnsafeDeviceLoginVerify(@NotNull net.mamoe.mirai.Bot bot, @NotNull String s, @NotNull Continuation<? super String> continuation) {
FocessQQ.getLogger().info(s);
try {
IOHandler.getConsoleIoHandler().input();
} catch (InputTimeoutException ignored) {
}
return null;
}
});
net.mamoe.mirai.Bot bot;
try {
bot = BotFactory.INSTANCE.newBot(id, ((SimpleBot) b).getPassword(), configuration);
bot.login();
((SimpleBot) b).setNativeBot(bot);
} catch (Exception e) {
throw new BotLoginException(id, e);
}
try {
EventManager.submit(new BotLoginEvent(b));
} catch (EventSubmitException e) {
FocessQQ.getLogger().thrLang("exception-submit-bot-login-event", e);
}
List<Listener<?>> listeners = Lists.newArrayList();
listeners.add(bot.getEventChannel().subscribeAlways(GroupMessageEvent.class, event -> {
GroupChatEvent e = new GroupChatEvent(b, event.getSender(), event.getMessage(), event.getSource());
try {
EventManager.submit(e);
} catch (EventSubmitException eventSubmitException) {
FocessQQ.getLogger().thrLang("exception-submit-group-chat-event", eventSubmitException);
}
}));
listeners.add(bot.getEventChannel().subscribeAlways(FriendMessageEvent.class, event -> {
FriendChatEvent e = new FriendChatEvent(b, event.getFriend(), event.getMessage(), event.getSource());
try {
EventManager.submit(e);
} catch (EventSubmitException eventSubmitException) {
FocessQQ.getLogger().thrLang("exception-submit-friend-chat-event", eventSubmitException);
}
}));
listeners.add(bot.getEventChannel().subscribeAlways(MessageRecallEvent.GroupRecall.class, event -> {
GroupRecallEvent e = new GroupRecallEvent(b, event.getAuthor(), event.getMessageIds(), event.getOperator());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-group-recall-event", ex);
}
}));
listeners.add(bot.getEventChannel().subscribeAlways(MessageRecallEvent.FriendRecall.class, event -> {
FriendRecallEvent e = new FriendRecallEvent(b, event.getAuthor(), event.getMessageIds());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-friend-recall-event", ex);
}
}));
listeners.add(bot.getEventChannel().subscribeAlways(NewFriendRequestEvent.class, event -> {
FriendRequestEvent e = new FriendRequestEvent(b, event.getFromId(), event.getFromNick(), event.getFromGroup(), event.getMessage());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-friend-request-event", ex);
}
if (e.getAccept() != null)
if (e.getAccept())
event.accept();
else
event.reject(e.isBlackList());
}));
listeners.add(bot.getEventChannel().subscribeAlways(BotInvitedJoinGroupRequestEvent.class, event -> {
GroupRequestEvent e = new GroupRequestEvent(b, event.getGroupId(), event.getGroupName(), event.getInvitor());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-group-request-event", ex);
}
if (e.getAccept() != null)
if (e.getAccept())
event.accept();
else
event.ignore();
}));
listeners.add(bot.getEventChannel().subscribeAlways(FriendInputStatusChangedEvent.class, event -> {
FriendInputStatusEvent e = new FriendInputStatusEvent(b, event.getFriend(), event.getInputting());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-friend-input-status-event", ex);
}
}));
listeners.add(bot.getEventChannel().subscribeAlways(StrangerMessageEvent.class, event -> {
StrangerChatEvent e = new StrangerChatEvent(b, event.getMessage(), event.getSender(), event.getSource());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-stranger-chat-event", ex);
}
}));
listeners.add(bot.getEventChannel().subscribeAlways(MessagePostSendEvent.class, event -> {
BotSendMessageEvent e = new BotSendMessageEvent(b, event.getMessage(), event.getTarget());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-bot-send-message-event", ex);
}
}));
listeners.add(bot.getEventChannel().subscribeAlways(MessagePreSendEvent.class, event -> {
BotPreSendMessageEvent e = new BotPreSendMessageEvent(b, event.getMessage(), event.getTarget(), event);
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-bot-pre-send-message-event", ex);
}
}));
BOT_LISTENER_MAP.put(b, listeners);
return true;
}
return false;
}
use of net.mamoe.mirai.utils.BotConfiguration in project ColorMirai by Coloryr.
the class BotStart method start.
public static boolean start() {
for (QQsObj item : ColorMiraiMain.config.qqList) {
Bot bot = BotFactory.INSTANCE.newBot(item.qq, item.password, new BotConfiguration() {
{
fileBasedDeviceInfo(ColorMiraiMain.runDir + item.info);
setProtocol(item.loginType);
setHighwayUploadCoroutineCount(ColorMiraiMain.config.highwayUpload);
redirectNetworkLogToDirectory(new File(ColorMiraiMain.runDir + "/botnetwork"));
redirectBotLogToDirectory(new File(ColorMiraiMain.runDir + "/botlog"));
setAutoReconnectOnForceOffline(ColorMiraiMain.config.autoReconnect);
}
});
try {
ColorMiraiMain.logger.info("正在登录QQ:" + item.qq);
ColorMiraiMain.logger.info("如果登录卡住,去看看BotLog文件夹里面的日志有没有验证码");
bot.login();
bots.put(item.qq, bot);
ColorMiraiMain.logger.info("QQ:" + item.qq + "已登录");
} catch (WrongPasswordException e) {
ColorMiraiMain.logger.error("机器人密码错误", e);
} catch (Exception e) {
ColorMiraiMain.logger.error("机器人错误", e);
return false;
}
}
if (bots.size() == 0) {
ColorMiraiMain.logger.error("没有QQ号登录");
return false;
}
BotEvent host = new BotEvent();
for (Bot item : bots.values()) {
item.getEventChannel().registerListenerHost(host);
messageList.put(item.getId(), new ConcurrentHashMap<>());
break;
}
service.scheduleAtFixedRate(() -> {
if (!reList.isEmpty()) {
for (ReCallObj item : reList) {
if (messageList.containsKey(item.bot)) {
Map<Integer, MessageSaveObj> list = messageList.get(item.bot);
MessageSaveObj obj = list.get(item.mid);
if (obj == null) {
ColorMiraiMain.logger.warn("不存在消息:" + item.mid);
continue;
}
if (obj.time > 0 || obj.time == -1)
try {
Mirai.getInstance().recallMessage(bots.get(item.bot), obj.source);
list.remove(item.mid);
messageList.put(item.bot, list);
} catch (Exception e) {
ColorMiraiMain.logger.error("消息撤回失败", e);
}
}
}
reList.clear();
}
for (Map<Integer, MessageSaveObj> item : messageList.values()) {
for (Map.Entry<Integer, MessageSaveObj> item1 : item.entrySet()) {
item1.getValue().time -= 1;
}
if (item.size() >= ColorMiraiMain.config.maxList) {
Iterator<Integer> iterator = item.keySet().iterator();
if (iterator.hasNext()) {
iterator.next();
iterator.remove();
}
}
}
}, 0, 1, TimeUnit.SECONDS);
service1.scheduleAtFixedRate(() -> {
if (!tasks.isEmpty()) {
SendPackObj task = tasks.remove(0);
for (ThePlugin item : PluginUtils.getAll()) {
item.callEvent(task, task.data, task.index);
}
}
}, 0, 100, TimeUnit.MICROSECONDS);
ColorMiraiMain.logger.info("机器人已启动");
return true;
}
use of net.mamoe.mirai.utils.BotConfiguration in project MiraiQQ by MidCoard.
the class SimpleBotManager method login.
@Override
public boolean login(Bot b) {
if (!b.isOnline() && b instanceof SimpleBot) {
long id = b.getId();
BotConfiguration configuration = BotConfiguration.getDefault();
configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PAD);
File cache = new File("devices/" + id + "/cache");
if (!cache.exists())
if (!cache.mkdirs())
throw new BotLoginException(id, FocessQQ.getLangConfig().get("fatal-create-cache-dir-failed"));
configuration.fileBasedDeviceInfo("devices/" + id + "/device.json");
configuration.setCacheDir(cache);
configuration.setLoginSolver(new LoginSolver() {
@Nullable
@Override
public Object onSolvePicCaptcha(@NotNull net.mamoe.mirai.Bot bot, @NotNull byte[] bytes, @NotNull Continuation<? super String> continuation) {
try {
FileImageOutputStream outputStream = new FileImageOutputStream(new File("captcha.jpg"));
outputStream.write(bytes);
outputStream.close();
} catch (IOException e) {
FocessQQ.getLogger().thrLang("exception-load-captcha-picture", e);
}
FocessQQ.getLogger().infoLang("input-captcha-code");
return IOHandler.getConsoleIoHandler().input();
}
@Nullable
@Override
public Object onSolveSliderCaptcha(@NotNull net.mamoe.mirai.Bot bot, @NotNull String s, @NotNull Continuation<? super String> continuation) {
FocessQQ.getLogger().info(s);
IOHandler.getConsoleIoHandler().input();
return null;
}
@Nullable
@Override
public Object onSolveUnsafeDeviceLoginVerify(@NotNull net.mamoe.mirai.Bot bot, @NotNull String s, @NotNull Continuation<? super String> continuation) {
FocessQQ.getLogger().info(s);
IOHandler.getConsoleIoHandler().input();
return null;
}
});
net.mamoe.mirai.Bot bot;
try {
bot = BotFactory.INSTANCE.newBot(id, ((SimpleBot) b).getPassword(), configuration);
bot.login();
((SimpleBot) b).setNativeBot(bot);
} catch (Exception e) {
throw new BotLoginException(id, e);
}
try {
EventManager.submit(new BotLoginEvent(b));
} catch (EventSubmitException e) {
FocessQQ.getLogger().thrLang("exception-submit-bot-login-event", e);
}
List<Listener<?>> listeners = Lists.newArrayList();
listeners.add(bot.getEventChannel().subscribeAlways(GroupMessageEvent.class, event -> {
GroupChatEvent e = new GroupChatEvent(b, event.getSender(), event.getMessage(), event.getSource());
try {
EventManager.submit(e);
} catch (EventSubmitException eventSubmitException) {
FocessQQ.getLogger().thrLang("exception-submit-group-chat-event", eventSubmitException);
}
}));
listeners.add(bot.getEventChannel().subscribeAlways(FriendMessageEvent.class, event -> {
FriendChatEvent e = new FriendChatEvent(b, event.getFriend(), event.getMessage(), event.getSource());
try {
EventManager.submit(e);
} catch (EventSubmitException eventSubmitException) {
FocessQQ.getLogger().thrLang("exception-submit-friend-chat-event", eventSubmitException);
}
}));
listeners.add(bot.getEventChannel().subscribeAlways(MessageRecallEvent.GroupRecall.class, event -> {
GroupRecallEvent e = new GroupRecallEvent(b, event.getAuthor(), event.getMessageIds(), event.getOperator());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-group-recall-event", ex);
}
}));
listeners.add(bot.getEventChannel().subscribeAlways(MessageRecallEvent.FriendRecall.class, event -> {
FriendRecallEvent e = new FriendRecallEvent(b, event.getAuthor(), event.getMessageIds());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-friend-recall-event", ex);
}
}));
listeners.add(bot.getEventChannel().subscribeAlways(NewFriendRequestEvent.class, event -> {
FriendRequestEvent e = new FriendRequestEvent(b, event.getFromId(), event.getFromNick(), event.getFromGroup(), event.getMessage());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-friend-request-event", ex);
}
if (e.getAccept() != null)
if (e.getAccept())
event.accept();
else
event.reject(e.isBlackList());
}));
listeners.add(bot.getEventChannel().subscribeAlways(BotInvitedJoinGroupRequestEvent.class, event -> {
GroupRequestEvent e = new GroupRequestEvent(b, event.getGroupId(), event.getGroupName(), event.getInvitor());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-group-request-event", ex);
}
if (e.getAccept() != null)
if (e.getAccept())
event.accept();
else
event.ignore();
}));
listeners.add(bot.getEventChannel().subscribeAlways(FriendInputStatusChangedEvent.class, event -> {
FriendInputStatusEvent e = new FriendInputStatusEvent(b, event.getFriend(), event.getInputting());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-friend-input-status-event", ex);
}
}));
listeners.add(bot.getEventChannel().subscribeAlways(StrangerMessageEvent.class, event -> {
StrangerChatEvent e = new StrangerChatEvent(b, event.getMessage(), event.getSender(), event.getSource());
try {
EventManager.submit(e);
} catch (EventSubmitException ex) {
FocessQQ.getLogger().thrLang("exception-submit-stranger-chat-event", ex);
}
}));
BOT_LISTENER_MAP.put(b, listeners);
return true;
}
return false;
}
Aggregations