简体   繁体   中英

Transfer messages from direct message to specific channel using Java Discord API

I want to make my bot in a server channel to say whatever an user dm it.

public class PrivateMessage extends ListenerAdapter
{
    private TextChannel channel;

    @Override
    public void onReady(@NotNull ReadyEvent event)
    {
        channel = event.getJDA().getChannelById(TextChannel.class, 962688156942073887L);
    }

    @Override
    public void onMessageReceived(@NotNull MessageReceivedEvent event)
    {
        if (event.isFromType(ChannelType.PRIVATE))
            channel.sendMessage(MessageCreateData.fromMessage(event.getMessage())).queue();
    }
}

At first it was working properly, until I dm it an image.

java.lang.IllegalStateException: Cannot build an empty message. You need at least one of content, embeds, components, or files

How can I fix this?

The issue is caused by trying to send an image message, which does not have any text content. To fix this issue, you can add a conditional statement to check if the message has any text content before sending it to the server channel.

@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event)
{
    if (event.isFromType(ChannelType.PRIVATE) && event.getMessage().getContentRaw() != null)
        channel.sendMessage(event.getMessage().getContentRaw()).queue();
}

This code will check if the received message has any text content before sending it to the server channel. If the message does not have any text content, it will be ignored.

You can also add a check for attachments, if the message has attachments but no text content, you can send a message with the attachment links.

@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event)
{
    if (event.isFromType(ChannelType.PRIVATE)){
        if(event.getMessage().getContentRaw() != null){
            channel.sendMessage(event.getMessage().getContentRaw()).queue();
        }else if(!event.getMessage().getAttachments().isEmpty()){
            String attachments = event.getMessage().getAttachments().stream().map(Attachment::getUrl).collect(Collectors.joining(","));
            channel.sendMessage("Attachment(s) : " + attachments).queue();
        }
    }
}

This way, you can send a message to the server channel with the link of the attachments if the user sent an image or other type of attachment.

  1. The channel is not declared properly. It does not have a type... In this case you should use either TextChannel channel = (whatever) or Channel channel = (whatever)
  2. You get the error message because channel is not within the scope of onMessageReceived() You need to learn about scopes.
  3. The onReady() will have no use in this case. Just as i mentioned before... Because of scopes.

Here is what your code should look like:

    @Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
    if(event.isFromType(ChannelType.PRIVATE)){
        TextChannel textChannel = event.getJDA().getGuildById("1046510699809079448").getTextChannelById("1046510701184831540");
        textChannel.sendMessage(event.getMessage().getContentRaw()).queue();
    }

You need to get the TextChannel from a Guild by using their ID's. Then you can get the message that was sent to the bot, by using event.getMessage() and getting its content via .getContentRaw() and send it using textChannel.sendMessage().queue()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM