mirror of
https://github.com/heavycaffeiner/ayaya.git
synced 2025-10-27 20:34:55 +09:00
101 lines
3.5 KiB
Java
101 lines
3.5 KiB
Java
package dev.ptnr;
|
|
|
|
import net.dv8tion.jda.api.EmbedBuilder;
|
|
import net.dv8tion.jda.api.JDA;
|
|
import net.dv8tion.jda.api.JDABuilder;
|
|
import net.dv8tion.jda.api.entities.Guild;
|
|
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
|
import net.dv8tion.jda.api.events.session.ReadyEvent;
|
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
|
import net.dv8tion.jda.api.interactions.InteractionContextType;
|
|
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
|
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
|
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
|
|
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction;
|
|
import net.dv8tion.jda.api.utils.FileUpload;
|
|
|
|
import java.util.List;
|
|
|
|
public class AyayaBot {
|
|
public static void main(String[] args) {
|
|
String token = System.getenv("DISCORD_TOKEN");
|
|
|
|
JDA jda = JDABuilder.createDefault(token)
|
|
.addEventListeners(new BotListener())
|
|
.build();
|
|
|
|
try {
|
|
jda.awaitReady();
|
|
} catch (InterruptedException e) {
|
|
System.err.println("[ERR] In AyayaBot.main() > " + e.getMessage());
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
class BotListener extends ListenerAdapter {
|
|
@Override
|
|
public void onReady(ReadyEvent event) {
|
|
JDA jda = event.getJDA();
|
|
|
|
CommandListUpdateAction commands = jda.updateCommands();
|
|
|
|
commands.addCommands(
|
|
Commands.slash("hitomi", "Get Image from ID (Hitomi)")
|
|
.addOptions(new OptionData(OptionType.INTEGER, "id", "gallery id")
|
|
.setRequiredRange(0, 9999999).setRequired(true))
|
|
.setContexts(InteractionContextType.GUILD)
|
|
);
|
|
|
|
commands.queue();
|
|
|
|
List<Guild> guildList = jda.getGuilds();
|
|
for (Guild guild : guildList) {
|
|
System.out.println("Contain Guild : " + guild.getName());
|
|
// System.out.println("ID: " + guild.get);
|
|
}
|
|
|
|
System.out.println(jda.getSelfUser().getName() + " is Ready.");
|
|
}
|
|
|
|
@Override
|
|
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
|
|
if (event.getGuild() == null) return;
|
|
|
|
switch (event.getName()) {
|
|
case "hitomi":
|
|
hitomi(event, event.getOption("id").getAsInt());
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void hitomi(SlashCommandInteractionEvent event, Integer id) {
|
|
MessageChannel channel = event.getChannel();
|
|
event.reply("Message Sent.").setEphemeral(true).queue();
|
|
|
|
EmbedBuilder eb = new EmbedBuilder();
|
|
eb.setTitle("품번 : " + id);
|
|
|
|
// eb.setDescription("This image has been added to the bot.");
|
|
// eb.addBlankField(false);
|
|
|
|
byte[] webpData = AyayaUtils.GetFileFromUrl(Hitomi.GetHitomiData(id));
|
|
if (webpData == null) {
|
|
channel.sendMessage("URL에서 이미지를 가져오지 못했습니다. URL이나 서버 상태를 확인해주세요.").queue();
|
|
return;
|
|
}
|
|
|
|
byte[] pngData = AyayaUtils.ConvertWebpToPng(webpData);
|
|
if (pngData == null) {
|
|
channel.sendMessage("이미지를 PNG로 변환하는 데 실패했습니다. 파일이 올바른 WebP 형식이 아닐 수 있습니다.").queue();
|
|
return;
|
|
}
|
|
|
|
FileUpload fileUpload = FileUpload.fromData(pngData, "1.png");
|
|
eb.setImage("attachment://" + "1.png");
|
|
|
|
channel.sendFiles(fileUpload).setEmbeds(eb.build()).queue();
|
|
}
|
|
}
|