Lairex59
Lairex59

Reputation: 11

How to send image in JDA

I want to make a discord bot, that when the user types -m skelt a picture will be shown from the bot. Here is what I got so far: Can somone please help me. Thanks!

package lairex59.Commands;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import lairex59.Main;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

public class Comment extends ListenerAdapter
{
    public void onGuildMessageReceived(GuildMessageReceivedEvent event){
        String[] args = event.getMessage().getContentRaw().split("\\s+");
        
        if (args[0].equalsIgnoreCase(Main.prefix + "help")) {
            EmbedBuilder info = new EmbedBuilder();
            info.setTitle(":among_us_lime:Among Us help cnter");
            info.setDescription("Among Us code center :office:");
            info.setColor(0xf45642);
            info.setFooter("Created by Laraib", event.getMember().getUser().getAvatarUrl());
            
            event.getChannel().sendTyping().queue();
            event.getChannel().sendMessage(info.build()).queue();
            info.clear();
        }
        if ((args[0].equalsIgnoreCase(Main.prefix + "m")) && (args[1].equalsIgnoreCase("polus"))) {
            event.getChannel().sendTyping().queue();
            event.getChannel().sendMessage("Polus file").queue();
        }
        if (args[0].equalsIgnoreCase(Main.prefix + "m") && (args[1].equalsIgnoreCase("mira"))) {
            event.getChannel().sendTyping().queue();
            event.getChannel().sendMessage("Mira file").queue();
        }
        if (args[0].equalsIgnoreCase(Main.prefix + "m") && (args[1].equalsIgnoreCase("skelt"))) {
            event.getChannel().sendTyping().queue();

            BufferedImage image = null;
            File f = null;
            
            try {
                f = new File ("D:\\Laraib\\Images\\Skelt.jpg");
                image = ImageIO.read(f);
            }
            catch(IOException e) {
                System.out.println("Error"+e);
            }
            try {
                f = new File ("D:\\Laraib\\Images\\Skelt.jpg");
                ImageIO.write(image, "jpg", f);
            }
            catch (IOException e) {
                System.out.println("Error"+e);
            }
        }
        
    }


}

I managed to read the file but don't know how i can tell the bot to send an image. Every help is needed. Thanks!

Upvotes: 0

Views: 5611

Answers (1)

Assim ZEMOUCHI
Assim ZEMOUCHI

Reputation: 51

Discord does not ask to specify if the file sent is an image or something else, it just sends the file, it is the client who, from the file extension, will display the image as an image, so your BufferedImage is useless.

So, to send your image, it's just plain silly: The MessageAction interface you have a method MessageAction#addFile(File)

All you have to do is to call this method, and to send all this to the wrong person (like the queue method for example).

File file = new File("path");

if (Objects.nonNull(file)) {
    event.getChannel().sendMessage("Here is my image !").addFile(file).queue();
} else {
    event.getChannel().sendMessage("Sorry, I can't found the image :c").queue();
}

Here's the java doc : https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/requests/restaction/MessageAction.html

EDIT:

Or, as @minn reminded me, you don't have to add the file in the MessageAction but you can directly send the file without additional content with the method MessageChannel#sendFile(file).

Here's the doc: https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/entities/MessageChannel.html

Upvotes: 1

Related Questions