我想做一个不和谐的机器人,当用户键入-m skelet时,机器人会显示一张图片。到目前为止我得到的是:有人能帮我吗。谢谢!
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);
}
}
}
}
我设法读了文件,但不知道如何告诉机器人发送图像。需要一切帮助。谢谢!
1条答案
按热度按时间omtl5h9j1#
discord不要求指定发送的文件是图像还是其他东西,它只是发送文件,它是客户机,从文件扩展名来看,将图像显示为图像,因此您的bufferedimage是无用的。
所以,要发送您的图像,这很愚蠢:messageaction接口有一个方法
MessageAction#addFile(File)
您所要做的就是调用这个方法,并将所有这些发送给错误的人(例如queue方法)。以下是java文档:https://ci.dv8tion.net/job/jda/javadoc/net/dv8tion/jda/api/requests/restaction/messageaction.html
编辑:
或者,正如@minn提醒我的那样,您不必在messageaction中添加文件,但是您可以直接发送文件,而无需使用该方法添加其他内容
MessageChannel#sendFile(file)
.医生来了:https://ci.dv8tion.net/job/jda/javadoc/net/dv8tion/jda/api/entities/messagechannel.html