Search in sources :

Example 1 with Bot

use of net.mamoe.mirai.Bot in project MapBotv4 by Maplefsnow.

the class CatImage method getImage.

public static MessageChain getImage(Long groupID) throws Exception {
    String imagePath = ".\\plugins\\MapBot\\cat_images";
    File[] imageList = Objects.requireNonNull(new File(imagePath).listFiles());
    Random random = new Random();
    int pos = random.nextInt(imageList.length);
    File image = imageList[pos];
    ExternalResource imageResource = ExternalResource.create(image);
    Bot bot = BotOperator.getBot();
    try {
        Image catImage = Objects.requireNonNull(bot.getGroup(groupID)).uploadImage(imageResource);
        return new MessageChainBuilder().append(Image.fromId(catImage.getImageId())).build();
    } catch (Exception e) {
        Bukkit.getLogger().warning(e.getClass().getName() + ": " + e.getMessage());
        throw new Exception("发送猫片失败QAQ,请再试一次吧");
    }
}
Also used : Random(java.util.Random) Bot(net.mamoe.mirai.Bot) Image(net.mamoe.mirai.message.data.Image) File(java.io.File) ExternalResource(net.mamoe.mirai.utils.ExternalResource) MessageChainBuilder(net.mamoe.mirai.message.data.MessageChainBuilder)

Example 2 with Bot

use of net.mamoe.mirai.Bot in project AngelinaFrame by Strelizia02.

the class MiraiFrameUtil method reBuildBotGroupMap.

/**
 * 重建群号-账号映射,保证一个群只有一个账号提供服务
 */
private void reBuildBotGroupMap() {
    // 一个群号只能有一个bot提供服务
    if (status == 0) {
        status = 1;
        for (Bot bot : Bot.getInstances()) {
            if (bot.isOnline()) {
                for (Group group : bot.getGroups()) {
                    messageIdMap.put(group.getId(), bot.getId());
                }
            }
        }
        status = 0;
    } else {
        log.warn("is on rebuilding group-bot");
    }
}
Also used : Group(net.mamoe.mirai.contact.Group) Bot(net.mamoe.mirai.Bot)

Example 3 with Bot

use of net.mamoe.mirai.Bot 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();
        }
    });
}
Also used : DeviceInfo(net.mamoe.mirai.utils.DeviceInfo) GroupChatController(top.angelinaBot.controller.GroupChatController) GlobalEventChannel(net.mamoe.mirai.event.GlobalEventChannel) Bot(net.mamoe.mirai.Bot) Group(net.mamoe.mirai.contact.Group) Autowired(org.springframework.beans.factory.annotation.Autowired) ActivityMapper(top.angelinaBot.dao.ActivityMapper) HashMap(java.util.HashMap) FriendChatController(top.angelinaBot.controller.FriendChatController) MessageInfo(top.angelinaBot.model.MessageInfo) StandardCharsets(java.nio.charset.StandardCharsets) InvocationTargetException(java.lang.reflect.InvocationTargetException) Value(org.springframework.beans.factory.annotation.Value) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) BotFactory(net.mamoe.mirai.BotFactory) net.mamoe.mirai.event.events(net.mamoe.mirai.event.events) Map(java.util.Map) AngelinaException(top.angelinaBot.Exception.AngelinaException) EventsController(top.angelinaBot.controller.EventsController) BotConfiguration(net.mamoe.mirai.utils.BotConfiguration) Contact(net.mamoe.mirai.contact.Contact) EventEnum(top.angelinaBot.model.EventEnum) AngelinaException(top.angelinaBot.Exception.AngelinaException) Group(net.mamoe.mirai.contact.Group) Bot(net.mamoe.mirai.Bot) InvocationTargetException(java.lang.reflect.InvocationTargetException) MessageInfo(top.angelinaBot.model.MessageInfo) Contact(net.mamoe.mirai.contact.Contact) DeviceInfo(net.mamoe.mirai.utils.DeviceInfo) BotConfiguration(net.mamoe.mirai.utils.BotConfiguration)

Example 4 with Bot

use of net.mamoe.mirai.Bot in project AngelinaFrame by Strelizia02.

the class MiraiMessageUtilImpl method sendGroupMsg.

/**
 * 发送群消息方法
 * @param replayInfo 发送消息的结构封装
 */
@Override
public void sendGroupMsg(ReplayInfo replayInfo) {
    activityMapper.sendMessage();
    // 解析replayInfo
    String replayMessage = replayInfo.getReplayMessage();
    List<ExternalResource> replayImgList = replayInfo.getReplayImg();
    String kick = replayInfo.getKick();
    Integer muted = replayInfo.getMuted();
    Boolean nudged = replayInfo.getNudged();
    // 获取登录bot
    Bot bot = Bot.getInstance(replayInfo.getLoginQQ());
    // 获取群
    Group group = bot.getGroupOrFail(replayInfo.getGroupId());
    if (replayMessage != null && replayImgList.size() > 0) {
        // 发送图片 + 文字
        MessageChainBuilder messageChainBuilder = new MessageChainBuilder().append(new PlainText(replayMessage));
        for (ExternalResource replayImg : replayImgList) {
            messageChainBuilder.append(group.uploadImage(replayImg));
        }
        MessageChain chain = messageChainBuilder.build();
        group.sendMessage(chain);
    } else if (replayMessage != null) {
        // 发送文字
        group.sendMessage(new PlainText(replayMessage));
    } else if (replayImgList.size() > 0) {
        // 发送图片
        MessageChainBuilder messageChainBuilder = new MessageChainBuilder();
        for (ExternalResource replayImg : replayImgList) {
            messageChainBuilder.append(group.uploadImage(replayImg));
        }
        MessageChain chain = messageChainBuilder.build();
        group.sendMessage(chain);
    }
    if (kick != null) {
        // 踢出群
        group.getOrFail(replayInfo.getQq()).kick("");
    }
    if (muted != null) {
        // 禁言muted分钟
        group.getOrFail(replayInfo.getQq()).mute(muted);
    }
    if (nudged) {
        // 戳一戳
        group.getOrFail(replayInfo.getQq()).nudge();
    }
    log.info("发送消息" + replayInfo.getReplayMessage());
}
Also used : Group(net.mamoe.mirai.contact.Group) Bot(net.mamoe.mirai.Bot) PlainText(net.mamoe.mirai.message.data.PlainText) MessageChain(net.mamoe.mirai.message.data.MessageChain) ExternalResource(net.mamoe.mirai.utils.ExternalResource) MessageChainBuilder(net.mamoe.mirai.message.data.MessageChainBuilder)

Example 5 with Bot

use of net.mamoe.mirai.Bot in project daily-attendance-mirai by Rominwolf.

the class Attendance method processAttendanceTimer.

/**
 * 处理打卡的奖池凭证奖励
 *
 * @param type 打卡类型
 */
public static void processAttendanceTimer(String type) {
    Bot bot = Mirai.getBot();
    // 如果当前没有 bot 则返回
    if (bot == null)
        return;
    // 增加 global 字段
    String fieldPool = type + S.Global.Field.pool;
    String fieldList = type + S.Global.Field.list;
    Yamler global = S.Data.globalYaml;
    int pool = global.getInt(fieldPool);
    String list = global.getString(fieldList);
    String[] wakeList = list.split(",");
    int amount = wakeList.length;
    // 将今日全局成功打卡列表和奖池清零
    global.set(fieldPool, 0);
    global.set(fieldList, "");
    // 分配奖池奖金
    List<Long> redPackets = Attendance.allocateRedPacket(pool, amount);
    // 循环每个参与打卡的用户列表
    for (int i = 0; i < amount; i++) {
        String userId = wakeList[i];
        // 如果 userId 不是数字则 continue
        if (!Smart.isNumeric(userId))
            continue;
        long targetId = Long.parseLong(userId);
        User user = new User(targetId);
        // 获取红包凭证数量并进行翻倍
        long score = redPackets.get(i);
        score = Use.useDiceAndResponse(targetId, score);
        // 更新用户凭证数量、设置夜光蜡烛使用状态、已加入打卡标记为假、累计发言次数归零
        user.updateScore(score);
        user.setUseItem(User.Field.candle, false);
        user.setValue(type + User.Field.isJoined, false);
        user.setValue(type + User.Field.totalTalk, 0);
        String result = S.get(type + ".pool").replace("{score}", score + "").replace("{pool}", pool + "");
        Mirai mirai = new Mirai(bot, targetId, 0, null);
        mirai.sendMessage(result);
    }
}
Also used : Mirai(ink.wsm.mirai.daily_attendance_v2.utils.Mirai) User(ink.wsm.mirai.daily_attendance_v2.cores.data.User) Bot(net.mamoe.mirai.Bot) Yamler(ink.wsm.mirai.daily_attendance_v2.utils.Yamler)

Aggregations

Bot (net.mamoe.mirai.Bot)94 Group (net.mamoe.mirai.contact.Group)43 NormalMember (net.mamoe.mirai.contact.NormalMember)13 Image (net.mamoe.mirai.message.data.Image)12 Friend (net.mamoe.mirai.contact.Friend)11 BotConfiguration (net.mamoe.mirai.utils.BotConfiguration)11 MessageChain (net.mamoe.mirai.message.data.MessageChain)10 File (java.io.File)8 PermissionDeniedException (net.mamoe.mirai.contact.PermissionDeniedException)8 IOException (java.io.IOException)7 Stranger (net.mamoe.mirai.contact.Stranger)7 Mirai (ink.wsm.mirai.daily_attendance_v2.utils.Mirai)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 NoSuchElementException (java.util.NoSuchElementException)5 MiraiBot (me.dreamvoid.miraimc.api.MiraiBot)5 AbsoluteFile (net.mamoe.mirai.contact.file.AbsoluteFile)5 MiraiHttpAPI (me.dreamvoid.miraimc.internal.httpapi.MiraiHttpAPI)4 AbnormalStatusException (me.dreamvoid.miraimc.internal.httpapi.exception.AbnormalStatusException)4 EventHandler (net.mamoe.mirai.event.EventHandler)4