|
| 1 | +package com.ashin.client; |
| 2 | + |
| 3 | +import com.ashin.config.QqConfig; |
| 4 | +import com.ashin.handler.QqMessageHandler; |
| 5 | +import lombok.Getter; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import net.mamoe.mirai.BotFactory; |
| 8 | +import net.mamoe.mirai.auth.BotAuthorization; |
| 9 | +import net.mamoe.mirai.utils.BotConfiguration; |
| 10 | +import org.springframework.stereotype.Component; |
| 11 | +import xyz.cssxsh.mirai.tool.FixProtocolVersion; |
| 12 | + |
| 13 | +import javax.annotation.PostConstruct; |
| 14 | +import javax.annotation.Resource; |
| 15 | + |
| 16 | +@Component |
| 17 | +@Slf4j |
| 18 | +public class QqBotClient { |
| 19 | + |
| 20 | + @Resource |
| 21 | + private QqMessageHandler qqMessageHandler; |
| 22 | + @Resource |
| 23 | + private QqConfig qqConfig; |
| 24 | + |
| 25 | + // qq机器人实例 |
| 26 | + @Getter |
| 27 | + private net.mamoe.mirai.Bot qqBot; |
| 28 | + |
| 29 | + @PostConstruct |
| 30 | + public void init() { |
| 31 | + if (qqConfig.getEnable()) { |
| 32 | + //登录 登陆协议有ANDROID_PHONE, ANDROID_PAD, ANDROID_WATCH, IPAD, MACOS |
| 33 | + try { |
| 34 | + log.info("正在登录qq,请按提示操作:"); |
| 35 | + if (qqConfig.getMethod() == 1) { |
| 36 | + //密码登录 |
| 37 | + qqBot = BotFactory.INSTANCE.newBot(qqConfig.getAccount(), qqConfig.getPassword().trim(), new BotConfiguration() {{ |
| 38 | + setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PAD); |
| 39 | + }}); |
| 40 | + //使用临时修复插件 |
| 41 | + FixProtocolVersion.update(); |
| 42 | + } else { |
| 43 | + //扫码登陆 |
| 44 | + qqBot = BotFactory.INSTANCE.newBot(qqConfig.getAccount(), BotAuthorization.byQRCode(), configuration -> configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_WATCH)); |
| 45 | + } |
| 46 | + |
| 47 | + qqBot.login(); |
| 48 | + log.info("成功登录账号为 {} 的qq, 登陆方式为 {}", qqConfig.getAccount(), qqConfig.getMethod() == 1 ? "密码登录" : "扫码登陆"); |
| 49 | + //订阅监听事件 |
| 50 | + qqBot.getEventChannel().registerListenerHost(qqMessageHandler); |
| 51 | + } catch (Exception e) { |
| 52 | + log.error("登陆失败,qq账号为 {}, 登陆方式为 {} ,原因:{}", qqConfig.getAccount(), qqConfig.getMethod() == 1 ? "密码登录" : "扫码登陆", e.getMessage()); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments