如何将群里的文件发送到另外一个群
-
我通过群消息事件然后获取到了fileMessage,但是试了直接发送到目标群无法实现
-
@bigcyy
研究出来了,贴这里供大家参考,我这里是mirai最新的稳定版//判断其是否是文件信息 boolean isFileMsg = message.stream().anyMatch((singleMessage) -> singleMessage instanceof FileMessage); if (isFileMsg) { //如果是文件 Optional<SingleMessage> first = message.stream().filter((singleMessage) -> singleMessage instanceof FileMessage).findFirst(); SingleMessage singleMessage; if(first.isPresent()) { singleMessage = first.get(); AbsoluteFile absoluteFile = ((FileMessage) singleMessage).toAbsoluteFile(group); //从刚刚发送文件的的群里获取该文件 String fileName = absoluteFile.getName(); String url = absoluteFile.getUrl(); //得到url OkHttpClient okHttpClient = new OkHttpClient(); //用okhttp发送请求,转成流 Request request = new Request.Builder().url(url).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { e.printStackTrace(); } @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try { InputStream inputStream = response.body().byteStream(); //转成流 ExternalResource res = ExternalResource.create(inputStream).toAutoCloseable(); //用mirai的ExternalResource包装 pushGroup.getFiles().uploadNewFile("/" + fileName, res); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } }); } }
-
你有没有试过,游历群文件夹下的子文件夹