discord-jda / JDA

Java wrapper for the popular chat & VOIP service: Discord https://discord.com
Apache License 2.0
4.33k stars 735 forks source link

getMessageByID Always Returns Failure #332

Closed ryans1230 closed 7 years ago

ryans1230 commented 7 years ago

So I'm trying to get a deleted message's content, and every time I am getting an answer from the RestAction<Message>, it is a failure, even though I know the message exists in the specified channel.

@Override
    public void onGuildMessageDelete(GuildMessageDeleteEvent e) {
        if (!e.getChannel().equals(main.log)) {
            //Check to make sure it's not a message auto removed by the bot, causing an infinite loop
            if(!deletedMsgs.contains(e.getMessageId())) {
                //Check to make sure we know which channel it is in for less work
                if(messages.containsKey(e.getMessageId())) {
                    String channel = messages.get(e.getMessageId());
                    String id = e.getMessageId();
                    System.out.println("Found the message! " + id);
                    MessageChannel origin = e.getGuild().getTextChannelById(channel);
                    System.out.println("Found the channel! " + channel);
                    //Code executes fine up to here, we know the message exists in the specified channel
                    //This should return success, which is then a Message, 
                    //so I should be able to success.getContent()
                    origin.getMessageById(id).queue(success -> System.out.println("Successfully found the message!"), 
                         failure -> System.out.println("Could not find the message!"));
                } else {
                    System.out.println("Message was not saved in the map!");
                }
            }
        }
    }
Avarel commented 7 years ago

If the message is deleted, it's most likely that Discord have removed the message and will not return it upon a REST request, therefore leading to a retrieval failure.

MinnDevelopment commented 7 years ago

You can't get a deleted message.

ryans1230 commented 7 years ago

How can I can get a deleted message then? I see other bots that do it

MinnDevelopment commented 7 years ago

You have to keep track of messages and access your own cache. Discord does not provide this.

MinnDevelopment commented 7 years ago

If you need any more help please consider to join our discord server. JDA does not provide a message cache to keep a lower memory footprint thus it is unable to provide deleted message. If you want to keep track of messages that are deleted you will have to make your own cache or database which stores messages for your own use.