Travellerrr / AronaBot

机器人阿洛娜的额外功能插件
GNU Affero General Public License v3.0
9 stars 1 forks source link

随机柴郡指令有执行问题 #5

Open Orisland opened 1 month ago

Orisland commented 1 month ago

https://pste.ch/epuxewulok.pl 问题出现频繁且错误完全一致,是否是因为api调用的时候有限额问题? QQ_1721992030806 我在进行测试的时候似乎没有发现问题,不确定问题来源。 QQ_1721992133612 https://api.lolimi.cn/API/chaiq/c.php

Travellerrr commented 1 month ago

能否检查出运行时获取到的文件或者格式?我这边测试结果貌似都是jpg、png之类的图片格式

Orisland commented 1 month ago

能否检查出运行时获取到的文件或者格式?我这边测试结果貌似都是jpg、png之类的图片格式

除了在源码上加try catch来获取异常外,我暂时不知道还有什么办法可以得到错误时的格式。 如果你愿意,可以给源码增加try catch之后重新编译一份插件,我会继续测试并在这里给出遇到错误时的log信息。

Travellerrr commented 1 month ago

试着将 https://github.com/Travellerrr/AronaBot/blob/4a6a36164f043f3c25e212b67ac6a911f95f3110/src/main/java/cn/travellerr/tools/api.java#L31-L41 改为

    public static void chaiq(Contact subject, User user) {
        try {
            URL url = new URL("https://api.lolimi.cn/API/chaiq/c.php");
            InputStream stream = url.openStream();

             // 文件类型测试
                File directory= new File("./aronaBotDebug");
                if (!directory.exists()) {
                    if (directory.mkdir()){
                        Log.debug("debug文件夹创建成功");
                    } else {
                        Log.error("debug文件夹创建失败");
                    }
                }
                long time = System.currentTimeMillis();
                File tempFile = File.createTempFile("tempFile", null);

                try (
                     BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(tempFile))) {
                    outputStream.write(stream.readAllBytes());
                }

                URLConnection connection = tempFile.toURI().toURL().openConnection();
                String mimeType = connection.getContentType();
                Log.debug("文件类型: " + mimeType);
                String[] suf = mimeType.split("/");
                String suffix = suf[suf.length - 1];

                File finalFile = new File("./aronaBotDebug/" + time + "file." + suffix);
                FileUtil.copy(tempFile, finalFile, true);

            ExternalResource resource = ExternalResource.create(stream);
            Image image = subject.uploadImage(resource);
            subject.sendMessage(new At(user.getId()).plus(image));
            stream.close();
            resource.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

来获取文件的格式与保存获取到的文件

Orisland commented 1 month ago

试着将

https://github.com/Travellerrr/AronaBot/blob/4a6a36164f043f3c25e212b67ac6a911f95f3110/src/main/java/cn/travellerr/tools/api.java#L31-L41

改为

    public static void chaiq(Contact subject, User user) {
        try {
            URL url = new URL("https://api.lolimi.cn/API/chaiq/c.php");
            InputStream stream = url.openStream();

             // 文件类型测试
                File dictionary = new File("./aronaBotDebug");
                if (!dictionary.exists()) {
                    if (dictionary.mkdir()){
                        Log.debug("debug文件夹创建成功");
                    } else {
                        Log.error("debug文件夹创建失败");
                    }
                }
                long time = System.currentTimeMillis();
                File tempFile = File.createTempFile("tempFile", null);

                try (
                     BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(tempFile))) {
                    outputStream.write(stream.readAllBytes());
                }

                URLConnection connection = tempFile.toURI().toURL().openConnection();
                String mimeType = connection.getContentType();
                Log.debug("文件类型: " + mimeType);
                String[] suf = mimeType.split("/");
                String suffix = suf[suf.length - 1];

                File finalFile = new File("./aronaBotDebug/" + time + "file." + suffix);
                FileUtil.copy(tempFile, finalFile, true);

            ExternalResource resource = ExternalResource.create(stream);
            Image image = subject.uploadImage(resource);
            subject.sendMessage(new At(user.getId()).plus(image));
            stream.close();
            resource.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

来获取文件的格式与保存获取到的文件

那这边我会重新编译一份插件后测试该问题,有最新的问题报告我会同步在这个issue。