use of net.dv8tion.jda.core.entities.Icon in project FredBoat by Frederikam.
the class SetAvatarCommand method onInvoke.
@Override
public void onInvoke(@Nonnull CommandContext context) {
URI imageUrl = null;
try {
if (!context.msg.getAttachments().isEmpty()) {
Attachment attachment = context.msg.getAttachments().get(0);
imageUrl = new URI(attachment.getUrl());
} else if (context.hasArguments()) {
imageUrl = AVATARS.containsKey(context.args[0]) ? AVATARS.get(context.args[0]) : new URI(context.args[0]);
}
} catch (URISyntaxException e) {
context.reply("Not a valid link.");
return;
}
if (imageUrl != null && imageUrl.getScheme() != null) {
if (imageUrl.getScheme().equals("branding")) {
// for branding:resource.png URLs
// clear off the 'scheme'
imageUrl = URI.create(imageUrl.getSchemeSpecificPart());
if (!checkRelativeToBrandingDir(imageUrl)) {
throw new MessagingException("url is not relative");
}
imageUrl = BRANDING_DIR.resolve(imageUrl);
}
Icon avatar;
switch(imageUrl.getScheme()) {
case "http":
case "https":
avatar = fetchRemote(imageUrl);
break;
case "file":
avatar = fetchFile(imageUrl);
break;
default:
throw new MessagingException("Not a readable image");
}
setBotAvatar(context, avatar);
return;
}
// if not handled it's not a proper invocation
HelpCommand.sendFormattedCommandHelp(context);
}
use of net.dv8tion.jda.core.entities.Icon in project Saber-Bot by notem.
the class AvatarCommand method action.
@Override
public void action(String head, String[] args, MessageReceivedEvent event) {
AccountManagerUpdatable manager = Main.getShardManager().getJDA().getSelfUser().getManagerUpdatable();
if (event.getMessage().getAttachments().isEmpty())
return;
Message.Attachment attachment = event.getMessage().getAttachments().get(0);
try {
File file = new File(attachment.getFileName());
attachment.download(file);
Icon icon = Icon.from(file);
manager.getAvatarField().setValue(icon).update().complete();
MessageUtilities.sendPrivateMsg("Updated bot avatar!", event.getAuthor(), null);
file.delete();
} catch (IOException e) {
Logging.exception(this.getClass(), e);
MessageUtilities.sendPrivateMsg("Failed to update bot avatar!", event.getAuthor(), null);
}
}
Aggregations