【问题标题】:Transfer messages from direct message to specific channel using Java Discord API使用 Java Discord API 将消息从直接消息传输到特定频道
【发布时间】:2023-01-31 02:58:46
【问题描述】:

我想让我的机器人在服务器通道中说出用户 dm 的任何内容。

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();
    }
}

起初它工作正常,直到我将其 dm 为图像。

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

我怎样才能解决这个问题?

【问题讨论】:

    标签: java discord illegalstateexception discord-jda


    【解决方案1】:
    1. 渠道没有正确声明。它没有类型...在这种情况下,您应该使用 TextChannel channel = (whatever)Channel channel = (whatever)
    2. 您收到错误消息是因为渠道不在范围内 onMessageReceived() 你需要了解作用域。
    3. onReady() 在这种情况下没有用。正如我之前提到的......因为范围。

      您的代码应该如下所示:

          @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();
          }
      

      你需要得到文本通道从一个公会通过使用他们的 ID。 然后你可以得到发送给机器人的消息,通过使用事件.getMessage()并通过获取其内容.getContentRaw()并使用发送textChannel.sendMessage().queue()

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 2021-10-28
      • 2018-09-07
      • 2020-10-01
      • 2020-12-23
      • 2023-03-27
      • 2020-10-01
      • 2020-05-18
      • 1970-01-01
      相关资源
      最近更新 更多