Skip to content

Instantly share code, notes, and snippets.

@ekgame
Last active January 13, 2021 09:14
Show Gist options
  • Save ekgame/d6e6aa87039d7e179c084c02d92e4326 to your computer and use it in GitHub Desktop.
Save ekgame/d6e6aa87039d7e179c084c02d92e4326 to your computer and use it in GitHub Desktop.
Sending a BufferedImage with Discord4J
package lt.ekgame.animation_test;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.EnumSet;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.message.BasicNameValuePair;
import com.google.gson.JsonSyntaxException;
import sx.blah.discord.Discord4J;
import sx.blah.discord.api.ClientBuilder;
import sx.blah.discord.api.EventDispatcher;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.api.IListener;
import sx.blah.discord.api.internal.DiscordEndpoints;
import sx.blah.discord.api.internal.DiscordUtils;
import sx.blah.discord.api.internal.Requests;
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
import sx.blah.discord.handle.impl.events.MessageSendEvent;
import sx.blah.discord.handle.impl.events.ReadyEvent;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IUser;
import sx.blah.discord.handle.obj.Permissions;
import sx.blah.discord.json.responses.MessageResponse;
import sx.blah.discord.util.DiscordException;
import sx.blah.discord.util.HTTP429Exception;
import sx.blah.discord.util.MissingPermissionsException;
public class Test {
public static void main(String... args) throws DiscordException, SQLException {
String token = "...";
final IDiscordClient client = new ClientBuilder().withToken(token).login();
EventDispatcher dispatcher = client.getDispatcher();
dispatcher.registerListener(new IListener<MessageReceivedEvent>() {
public void handle(MessageReceivedEvent event) {
IChannel channel = event.getMessage().getChannel();
BufferedImage img = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = img.createGraphics();
graphics.setColor(Color.BLACK);
graphics.drawLine(0, 0, 50, 50);
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(img, "png", output);
sendBytesAsFile(client, channel, output.toByteArray(), "image/png", "thing.png");
} catch (IOException e) {
e.printStackTrace();
} catch (JsonSyntaxException e) {
e.printStackTrace();
} catch (HTTP429Exception e) {
e.printStackTrace();
} catch (DiscordException e) {
e.printStackTrace();
} catch (MissingPermissionsException e) {
e.printStackTrace();
}
}
});
}
public static IMessage sendBytesAsFile(IDiscordClient client, IChannel channel, byte[] bytes, String mime, String filename) throws JsonSyntaxException, HTTP429Exception, DiscordException, MissingPermissionsException {
DiscordUtils.checkPermissions(client, channel, EnumSet.of(Permissions.SEND_MESSAGES, Permissions.ATTACH_FILES));
if (client.isReady()) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create().addBinaryBody("file", bytes, ContentType.create(mime), filename);
HttpEntity fileEntity = builder.build();
MessageResponse response = DiscordUtils.GSON.fromJson(Requests.POST.makeRequest(
DiscordEndpoints.CHANNELS+channel.getID()+"/messages",
fileEntity, new BasicNameValuePair("authorization", client.getToken())), MessageResponse.class);
IMessage message = DiscordUtils.getMessageFromJSON(client, channel, response);
client.getDispatcher().dispatch(new MessageSendEvent(message));
return message;
} else {
Discord4J.LOGGER.error("Bot has not signed in yet!");
return null;
}
}
}
@ConsumerJunk
Copy link

Cool

Copy link

ghost commented May 28, 2020

Can you give a tutorial ?

Because , I am new to java.

help!!!

@Jonathing
Copy link

Jonathing commented Jan 13, 2021

This is what I did, I used ImageIO.write to get

BufferedImage bufferedImage; // pretend this is initialized
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", outputStream);
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
event.getMessage().getChannel().block().createMessage(messageCreateSpec ->
{
	messageCreateSpec.addFile("output.png", inputStream);
}).block();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment