Java 类net.dv8tion.jda.core.entities.Message.Attachment 实例源码

项目:Supreme-Bot    文件:UploadFileCommand.java   
@Override
public void action(Invoker invoker, ArgumentList arguments, MessageEvent event) {
    final List<Attachment> attachments = event.getMessage().getAttachments();
    attachments.stream().filter((attachment) -> attachment != null).forEach((attachment) -> {
        try {
            Thread.sleep(500);
            final String filePath = arguments.consumeFirst();
            final AdvancedFile file = new AdvancedFile(Standard.STANDARD_UPLOAD_FOLDER, (filePath != null ? filePath : attachment.getFileName()));
            file.getParent().createAdvancedFile();
            attachment.download(file.toFile());
            event.sendMessageFormat(Standard.STANDARD_MESSAGE_DELETING_DELAY * 2, "%s uploaded \"%s\"%s", event.getAuthor().getAsMention(), attachment.getFileName(), (filePath != null ? String.format(" as \"%s\"", filePath) : ""));
        } catch (Exception ex) {
            event.sendMessageFormat(Standard.STANDARD_MESSAGE_DELETING_DELAY * 2, "%s %s the file \"%s\" was unable to upload (%s)!", Emoji.WARNING, event.getAuthor().getAsMention(), attachment.getFileName(), ex);
        }
    });
}
项目:Supreme-Bot    文件:Util.java   
public static final List<AdvancedFile> getAndDownloadAttachmentsOfMessage(Message message) {
    return message.getAttachments().stream().map((attachment) -> new AbstractMap.SimpleEntry<AdvancedFile, Attachment>(Standard.getTempFile(attachment.getFileName()), attachment)).filter((image) -> image.getValue().download(image.getKey().toFile())).map((image) -> image.getKey()).collect(Collectors.toList());
}
项目:Supreme-Bot    文件:MessageHandler.java   
private final void onMessage(final Object event_original, final MessageEvent event, boolean newMessage) {
    Updater.submit(() -> {
        try {
            final String content_raw = event.getMessage().getRawContent().trim();
            final String content = event.getMessage().getContent().trim();
            final Guild guild = event.getGuild();
            final CommandType commandType = CommandType.getCommandType(content, content_raw, event);
            if (commandType.isCommand()) {
                CommandHandler.handleCommand(CommandParser.parser(commandType, content, content_raw, event));
            } else if (content_raw.contains(Standard.getSelfUser().getAsMention()) || content_raw.contains(Standard.getSelfMemberByGuild(guild).getAsMention())) {
                final Emote reaction_emote = Config.CONFIG.getGuildReactionOnMention(guild);
                if (reaction_emote != null) {
                    event.getMessage().addReaction(reaction_emote).queue();
                } else {
                    final Emoji reaction_emoji = Config.CONFIG.getGuildReactionOnMention(guild.getIdLong());
                    if (reaction_emoji != null) {
                        event.getMessage().addReaction(reaction_emoji.getUnicode()).queue();
                    }
                }
            }
            final Object[] output = ListenerManager.fireListeners(MessageListener.class, CommandHandler.ADMIN_PREDICATE, new Object[]{newMessage ? event : event_original, newMessage ? MessageType.RECEIVED : MessageType.UPDATED});
            String embed_messages = event.getMessage().getEmbeds().stream().map((messageEmbed) -> Util.embedMessageToString(messageEmbed)).collect(Collectors.joining(Standard.NEW_LINE_DISCORD));
            if (!embed_messages.isEmpty()) {
                embed_messages = Standard.NEW_LINE_DISCORD + embed_messages;
            }
            final List<Attachment> attachments = event.getMessage().getAttachments();
            if (attachments == null || attachments.isEmpty()) {
                System.out.println(String.format("[Used %d time%s] [%s] [%s] %s: %s%s", output.length, (output.length == 1 ? "" : "s"), (guild != null ? guild.getName() : "PRIVATE"), event.getMessageChannel().getName(), Standard.getCompleteName(event.getAuthor(), true), content, embed_messages));
            } else {
                String text = "";
                for (Attachment attachment : attachments) {
                    text += Standard.NEW_LINE_DISCORD;
                    if (attachment.isImage()) {
                        text += String.format("IMAGE: \"%s\" (ID: %s) (PROXYURL: %s) (W: %d, H: %d)", attachment.getFileName(), attachment.getId(), attachment.getProxyUrl(), attachment.getWidth(), attachment.getHeight());
                    } else {
                        text += String.format("FILE: \"%s\" (ID: %s) (URL: %s)", attachment.getFileName(), attachment.getId(), attachment.getUrl());
                    }
                }
                System.out.println(String.format("[Used %d time%s] [%s] [%s] %s: %s%s%s", output.length, (output.length == 1 ? "" : "s"), event.getGuild().getName(), event.getMessageChannel().getName(), Standard.getCompleteName(event.getAuthor(), true), content, text, embed_messages));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    });
}
项目:Tazbot    文件:Util.java   
public static File downloadSoundAttachment(Attachment att, int maxDuration) throws UnsupportedAudioFormatException, MaxDurationException {
    String name = att.getFileName().replaceFirst("[.][^.]+$", "");
    String ext = att.getFileName().substring(att.getFileName().lastIndexOf('.') + 1);

    if (ext.equalsIgnoreCase("mp3") || ext.equalsIgnoreCase("wav")) { // TODO Change accepted extensions to some constant somewhere
        File file = new File("secrethitler/" + att.getFileName().replace(" ", "_").replace("/", "")); // TODO Change file path to constant
        System.out.println(file.getAbsolutePath());
        int i = 0;
        while (file.exists() && !file.isDirectory()) {
            file = new File("secrethitler/" + name + "_" + i + "." + ext);
            i++;
        }
        if (att.download(file)) {
            try {
                float duration = -1;
                // Check that duration is less than 10 seconds
                if (ext.equalsIgnoreCase("mp3")) {
                    AudioFileFormat format = new MpegAudioFileReader().getAudioFileFormat(file);
                    Map<String, Object> properties = format.properties();
                    Long micro = (Long) properties.get("duration");
                    duration = (float) (micro/1_000_000.0);
                }
                else if (ext.equalsIgnoreCase("wav")) {
                    AudioInputStream ais = AudioSystem.getAudioInputStream(file);
                    AudioFormat format = ais.getFormat();
                    long length = file.length();
                    int frameSize = format.getFrameSize();
                    float frameRate = format.getFrameRate();
                    duration = (length / (frameSize * frameRate));
                }

                if (maxDuration == 0) {
                    return file;
                }
                else if (duration < maxDuration && !(duration < 0)) { // TODO Change to constant from configuration
                    return file;
                }
                // Duration exceeds allowed
                else {
                    file.delete();
                    throw new MaxDurationException();
                }
            } catch (UnsupportedAudioFileException e) {
            } catch (IOException e) {}
            // Delete the file
            file.delete();
            return null;
        }
        // Failed to download attachment
        else {
            return null;
        }
    }
    // Bad file type
    else {
        throw new UnsupportedAudioFormatException();
    }
}