ERavn29
ERavn29

Reputation: 9

Getting the message contents from specific channel on startup (Discord Bot using IDA)

I'm trying to make a discord bot that retrieves the message content (as a message variable) from the latest message in a specific channel when i start the bot.

I've tried to research but i cant seem to find exactly how to do it anywhere, I've also tried many different things from things i find online, like the .getHistory() method, but nothing seems to give me what i need.

Upvotes: 0

Views: 50

Answers (1)

Loso
Loso

Reputation: 84

Here is how you do it:

@Override
    public void onReady(ReadyEvent event) {
        TextChannel textChannel = event.getJDA().getTextChannelById("1119241996708630598");
        MessageHistory messageHistory = textChannel.getHistoryFromBeginning(100).complete();
        List<Message> messages = messageHistory.getRetrievedHistory();

        System.out.println(messages.get(0).getContentRaw());
    }

getHistoryFromBeginning() must always be completed before doing anything with it, you then have to use getRetrievedHistory() and save it to a list ( otherwise it wont work ) and then you can work with it however you like.

Upvotes: 0

Related Questions