#Java JDA add custom new API request optimisation

76 messages · Page 1 of 1 (latest)

knotty echo
#
    private String avatar_decoration_asset_id(User u){

           RestActionImpl<String> restAction = new RestActionImpl<>(u.getJDA(), Route.Users.GET_USER.compile(u.getId()), (response, request) -> {

               DataObject json = response.getObject();
               if (json.isNull("avatar_decoration_data")){ return "none";}
               String id = json.getObject("avatar_decoration_data").getString("asset");
               return id;
           });

        try {
            return restAction.complete(true);
        } catch (RateLimitedException e) {
            System.out.println(e);
            return "none";
        }

    }

i need to find out if this is the optimal way, i guess no, cause i somehow need to make it .queue(); so i can iterate through user list. But here is the thing i dont get.

If i make a forloop and iterate throuhg the list and then use this as my queue, how the foorloop supposed to know that the queue was sent already

tribal vaultBOT
#

This post has been reserved for your question.

Hey @knotty echo! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

knotty echo
#

and if i make .queue(); the return type is void, so i cant get the string back. so how to fix it that it properly use the queue

royal scroll
#

the queue method can take a Consumer as its argument

#

And today you learned how to use Consumers, so that's quite convenient

knotty echo
#

yeah i use that usualy to delete the original message. but i dunno what else i should consume there 😄

#

like can i consume a return type there and if i would do that, i need to safe the "variable" somewhere

royal scroll
#

If you want to return the result, it's better to stay with complete

knotty echo
#

yeah but then it ratelimits and i cannot iterate throuhg whole list

royal scroll
#

It will ratelimit even if you use queue, eventually

knotty echo
#

mh ```java
restAction.queue(a -> return this, b -> {});

#

that doesnt work

royal scroll
#

You can't return an object from lambda/anonymous class

#

But

#

You can make avatar_decoration_asset_id return RestAction

#

and then do avatar_decoration_asset_id(...).queue(obj -> {...});

#

That's a good compromise

knotty echo
#

yeah thats confusing again, i can return the whole interface?

#

dont i need the RestActionImpl

royal scroll
#

Of course you can

knotty echo
#

or whats the point of the Impl

royal scroll
#

Impl means Implementation (in that case)

#

That class implements RestAction

#

But if that's confusing for you, tou can just return RestActionImpl as well

#

Doesn't make any difference in that case

knotty echo
#
    private RestAction avatar_decoration_asset_id(User u){

           RestActionImpl<String> restAction = new RestActionImpl<>(u.getJDA(), Route.Users.GET_USER.compile(u.getId()), (response, request) -> {

               DataObject json = response.getObject();
               if (json.isNull("avatar_decoration_data")){ return "none";}
               String id = json.getObject("avatar_decoration_data").getString("asset");
               return id;
           });

            return restAction;

    }

                for (Member member : guild.getMembers()) {
                    avatar_decoration_asset_id(member.getUser()).queue();
                }
#

i still need to parse the id somehow now

royal scroll
#

That'll ratelimit quickly

#

You need to either do it in certain events, or only handle x users at a time

knotty echo
#

yeah then i guess i keep the original with Listening on ONLINE status event

royal scroll
#

Keep in mind that not all users change their status to ONLINE

#

Some are invisible

knotty echo
#

yeah thats fine, i sitll have my Menue command

royal scroll
#

Ideally you would want to listen to member joining events, and only iterate over all members once and never again

royal scroll
#

You get the asset id

#

In case of your user it's a_554b7c34f7b6c709f19535aacb128e7b

#

So the link will be https://media.discordapp.net/avatar-decoration-presets/a_554b7c34f7b6c709f19535aacb128e7b.png

#

Just do https://media.discordapp.net/avatar-decoration-presets/<asset>.png

#

And it'll be fine

knotty echo
#

other question

#

how i can get the rhole JSON the same i get when debugging

#

printing the response doesnt work

#

printing the response.getObject gives error

royal scroll
#

It prints nothing?

knotty echo
#

that only pritns httpresponse

#

and this give exception

royal scroll
knotty echo
#

doesnt help

royal scroll
#

I meant

#

Wrap what's inside

knotty echo
#

i get the json in console tho

#

yeah it works

#

problem was i already had the bot running in background

#

so it replied twice

#

1 over the debug IDE and 1 over background bot

royal scroll
#

So it works now?

knotty echo
#

yep

#

thx i let this open here if i find another question regarding jda

royal scroll
#

`kay

tribal vaultBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

knotty echo
#

its bit buggy, like it keeps adding the more i generate (even when i the last image only 4 hearts should be generated) and the algorithm/pattern needs be better
based on google the drawImage need a while to draw, so it only works ones per loop

knotty echo
#
  public static final BufferedImage janna;
    static {
        try {
            janna = ImageIO.read(JannaBot.class.getResource("/janna.png"));
        } catch (IOException e) {throw new RuntimeException(e);}
    }

    public static final BufferedImage heart;
    static {
        try {heart = ImageIO.read(JannaBot.class.getResource("/heart.png")); 
        } catch (IOException e) {throw new RuntimeException(e);}
    }

    public static List<int[]> validAreas = new ArrayList<>(); //calculate valid positions on board
    static {
        for (int x = 32; x < 480; x += 32) {
            for (int y = 32; y < 480; y += 32) {
                validAreas.add(new int[]{x, y});
            }
        }
    }

    private void DropImage(TextChannel textChannel) {

        StringBuilder debug = new StringBuilder();
        BufferedImage input = janna;

        for (int i= ThreadLocalRandom.current().nextInt(6,10);i>0;i--){ //iterrer 6 to 9 random hearts
            int[] pos = validAreas.get(ThreadLocalRandom.current().nextInt(0,validAreas.size())); //grab positions
            int x = pos[0]+ThreadLocalRandom.current().nextInt(-8,9); //Seeder so the images not 100% centered at 32x32 positions
            int y = pos[1]+ThreadLocalRandom.current().nextInt(-8,9); //Seeder so the images not 100% centered at 32x32 positions
            debug.append(i+", x:"+x+" , y:"+y).append("\n"); //debug for the embed to see what positions

            while(!input.createGraphics().drawImage(heart,x,y, null)){ //draw the thing. (Here is the bugg i guess!)
              System.out.println("wait"); // actually never happen o.O so its fast enaugh
            };
        }
        try { ImageIO.write(input,"PNG",new File("outputIMG.png"));
        } catch (IOException e) { throw new RuntimeException(e); }

        File f = new File("outputIMG.png");
        EmbedBuilder embed = new EmbedBuilder();
        embed.setTitle("JANNA").setDescription(debug).setThumbnail("attachment://outputIMG.png");
        textChannel.sendMessageEmbeds(embed.build()).addFiles(AttachedFile.fromData(f)).queue(); //Send the embed
    }
knotty echo
#

@dreamy idol so is this thing actually open now or not? i can stilll write here. can other see this

dreamy idol
#

it is open

knotty echo
#

oki, cause it said archived above ^^

dreamy idol
#

and it was unarchived the moment you sent a message

tribal vaultBOT
tribal vaultBOT
knotty echo
#

ok thx i will take a look