#access variable in method in another package

67 messages · Page 1 of 1 (latest)

sand axle
#

`
package org.example.events;

import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import java.util.Objects;

import static org.example.Discord4Shell.Discord4Shell.user;

public class SelectTargetMessage extends ListenerAdapter {
public void onMessageReceived(MessageReceivedEvent event) {
if (event.getAuthor().isBot()) {
return;
}

    if (event.getMessage().getContentRaw().contains("SelectTarget")) {
        String[] message = event.getMessage().getContentRaw().split(" ");
        String SelectedTarget = message[1];
        if (Objects.equals(SelectedTarget, user)) {
            event.getChannel().sendMessage("Logged In As " + SelectedTarget).queue();
        }
    }
}

}

`

cedar burrowBOT
#

This post has been reserved for your question.

Hey @sand axle! Please use /close or the Close Post button above when you're finished. 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.

sand axle
#

So I want to use the variable SelectedTarget in another package

#

package org.example.Discord4Shell;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import org.example.events.SelectTargetMessage;
import org.example.events.TargetsListMessage;
import static org.example.events.SelectTargetMessage.*;

import java.util.Objects;


public class Discord4Shell {
    public static String user = System.getProperty("user.name");
    public static void main(String[] args) {
        JDA bot = JDABuilder.createDefault("Token”)
                .setActivity(Activity.playing("No Larry?"))
                .enableIntents(GatewayIntent.MESSAGE_CONTENT)
                .build();


        bot.addEventListener(new TargetsListMessage());
        bot.addEventListener(new SelectTargetMessage());




    }
}

#

This package

#

@humble berry sorry for ping yes I know every time but hello, so hi

humble berry
#

when do you need to use it?

#

and for what?

sand axle
sand axle
humble berry
#

where exactly do you need it?

sand axle
#

Under bot.addeventlisterners

humble berry
#

that happens when the bot starts

#

SelectedTarget exists when a message is sent

sand axle
#

Yes

humble berry
#

you cannot read something that will be created in the future

#

Java doesn't allow you to time-travel

sand axle
humble berry
#

same issue

sand axle
#

How

humble berry
#

after you add the listeners - no message has been sent yet

sand axle
#

okay

#

So I can’t

#

Add it

humble berry
#

not before it has been created

sand axle
#

So if I’m right I have to do everything in that file?

humble berry
#

no

sand axle
#

Then?

humble berry
#

depends where you actually need it

sand axle
#

I want to use it in other files and do if(Objrcts.equals(SelectedTarget, user)

stone ocean
sand axle
#

Okay

stone ocean
#

you did it wrong

sand axle
#

Done

stone ocean
#

gameboy that is not supposed to be in the code block

#

```java

#

it goes next to the back ticks

sand axle
#

Ahh it doesn’t work

#

This goofy thing

humble berry
sand axle
humble berry
#

what if it's sent multiple times

sand axle
#

Then it will remember the last time they sent it

humble berry
#

ok first of all make the variable a field

#

local variables only ecist during the method

sand axle
humble berry
#

a variable outside of any method but inside a class

sand axle
#

oh they

#

that

#

Okay

#

I’ll do that

humble berry
#

so now it's part of the SelectedTargetMessage object

sand axle
#

Yeah

humble berry
#

so in order to access it, you need a reference to it

sand axle
#

A reference to it?

humble berry
#

you need to be able to access the object in order to access the field

humble berry
#

where do you create the SelectedTargetMessage object?

sand axle
#

I don’t,

humble berry
#

you do

#

look in your main

sand axle
#

Ah yes the listeners

humble berry
#

so you need access to that listener in the other class

#

so first create the listener then create the other listener and pass the first listener as a constructor parameter and then add the listeners to JDA