Created
July 19, 2016 19:14
-
-
Save ekgame/a116d027e6c2d0d8676abac1466eb791 to your computer and use it in GitHub Desktop.
Send a BufferedImage with JDA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lt.ekgame.storasbot.utils; | |
import java.awt.image.BufferedImage; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.util.function.Consumer; | |
import javax.imageio.ImageIO; | |
import org.apache.http.entity.ContentType; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import com.mashape.unirest.http.Unirest; | |
import com.mashape.unirest.http.exceptions.UnirestException; | |
import com.mashape.unirest.request.body.MultipartBody; | |
import net.dv8tion.jda.MessageBuilder; | |
import net.dv8tion.jda.Permission; | |
import net.dv8tion.jda.entities.Channel; | |
import net.dv8tion.jda.entities.Guild; | |
import net.dv8tion.jda.entities.Message; | |
import net.dv8tion.jda.entities.impl.JDAImpl; | |
import net.dv8tion.jda.exceptions.PermissionException; | |
import net.dv8tion.jda.handle.EntityBuilder; | |
import net.dv8tion.jda.requests.Requester; | |
import net.dv8tion.jda.utils.PermissionUtil; | |
public class JDAUtils { | |
public static Message sendImage(Guild guild, Channel channel, BufferedImage image, Message message) | |
{ | |
guild.checkVerification(); | |
if (!PermissionUtil.checkPermission(guild.getJDA().getSelfInfo(), Permission.MESSAGE_WRITE, channel)) | |
throw new PermissionException(Permission.MESSAGE_WRITE); | |
if (!PermissionUtil.checkPermission(guild.getJDA().getSelfInfo(), Permission.MESSAGE_ATTACH_FILES, channel)) | |
throw new PermissionException(Permission.MESSAGE_ATTACH_FILES); | |
if(image == null) | |
throw new IllegalArgumentException("Provided image is null!"); | |
JDAImpl api = (JDAImpl) guild.getJDA(); | |
try | |
{ | |
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | |
ImageIO.write(image, "png", bytes); | |
bytes.flush(); | |
MultipartBody body = Unirest.post(Requester.DISCORD_API_PREFIX + "channels/" + channel.getId() + "/messages") | |
.header("authorization", guild.getJDA().getAuthToken()) | |
.header("user-agent", Requester.USER_AGENT) | |
.field("empty", ""); | |
body.field("file", bytes.toByteArray(), ContentType.create("image/png"), "imge.png"); | |
bytes.close(); | |
if (message != null) | |
body.field("content", message.getRawContent()).field("tts", message.isTTS()); | |
String dbg = String.format("Requesting %s -> %s\n\tPayload: image, message: %s, tts: %s\n\tResponse: ", | |
body.getHttpRequest().getHttpMethod().name(), body.getHttpRequest().getUrl(), message == null ? "null" : message.getRawContent(), message == null ? "N/A" : message.isTTS()); | |
String requestBody = body.asString().getBody(); | |
Requester.LOG.trace(dbg + body); | |
try | |
{ | |
JSONObject messageJson = new JSONObject(requestBody); | |
return new EntityBuilder(api).createMessage(messageJson); | |
} | |
catch (JSONException e) | |
{ | |
Requester.LOG.fatal("Following json caused an exception: " + requestBody); | |
Requester.LOG.log(e); | |
} | |
} | |
catch (UnirestException | IOException e) | |
{ | |
Requester.LOG.log(e); | |
} | |
return null; | |
} | |
public static void sendImageAsync(Guild guild, Channel channel, BufferedImage image, String message, Consumer<Message> callback) | |
{ | |
guild.checkVerification(); | |
if (!PermissionUtil.checkPermission(guild.getJDA().getSelfInfo(), Permission.MESSAGE_WRITE, channel)) | |
throw new PermissionException(Permission.MESSAGE_WRITE); | |
if (!PermissionUtil.checkPermission(guild.getJDA().getSelfInfo(), Permission.MESSAGE_ATTACH_FILES, channel)) | |
throw new PermissionException(Permission.MESSAGE_ATTACH_FILES); | |
Thread thread = new Thread(() -> | |
{ | |
Message messageReturn = sendImage(guild, channel, image, new MessageBuilder().appendString(message).build()); | |
if (callback != null) | |
callback.accept(messageReturn); | |
}); | |
thread.setName("TextChannelImpl sendFileAsync Channel: " + channel.getId()); | |
thread.setDaemon(true); | |
thread.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
May I ask how can I use this to make my bit send a pictures ?