当前位置: 首页 > 知识库问答 >
问题:

如何在JDA嵌入消息中添加多个响应

郑宜民
2023-03-14

我目前拥有的代码是:

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
    String[] args = event.getMessage().getContentRaw().split("\\s+");

    if (args[0].equalsIgnoreCase(DiscordBot.prefix + "info")) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());

        MessageChannel channel = event.getChannel(); // Channel the command was sent in.

        EmbedBuilder info = new EmbedBuilder();
        info.setTitle("Server Info");
        info.setDescription("Info About the bot.");
        info.addField("Creator", "Name", false);
        info.setColor(0xf45642);
        info.setTimestamp(Instant.now());
        channel.sendMessage(info.build()).queue(message -> message.addReaction("✔️").queue());
    }
}

共有1个答案

司寇安宜
2023-03-14

我自己也有同样的问题,因为没有真正的本地方法来做我想做的事情,那就是当一个反应被点击时,有特定的机器人动作。

我最终创建了两个帮助器类,允许程序在单击响应时进行回调:

以下是将监听要单击的反应的反应:

public class ReactionListener<T> {

  private final Map<String, Consumer<Message>> reactions;
  private final long userId;
  private volatile T data;
  private Long expiresIn, lastAction;
  private boolean active;

  public ReactionListener(long userId, T data) {
    this.data = data;
    this.userId = userId;
    reactions = new LinkedHashMap<>();
    active = true;
    lastAction = System.currentTimeMillis();
    expiresIn = TimeUnit.MINUTES.toMillis(5);
  }

  public boolean isActive() {
    return active;
  }

  public void disable() {
    this.active = false;
  }

  /**
   * The time after which this listener expires which is now + specified time
   * Defaults to now+5min
   *
   * @param timeUnit time units
   * @param time     amount of time units
   */
  public void setExpiresIn(TimeUnit timeUnit, long time) {
    expiresIn = timeUnit.toMillis(time);
  }

  /**
   * Check if this listener has specified emote
   *
   * @param emote the emote to check for
   * @return does this listener do anything with this emote?
   */
  public boolean hasReaction(String emote) {
    return reactions.containsKey(emote);
  }

  /**
   * React to the reaction :')
   *
   * @param emote   the emote used
   * @param message the message bound to the reaction
   */
  public void react(String emote, Message message) {
    if (hasReaction(emote)) reactions.get(emote).accept(message);
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }

  /**
   * Register a consumer for a specified emote
   * Multiple emote's will result in overriding the old one
   *
   * @param emote    the emote to respond to
   * @param consumer the behaviour when emote is used
   */
  public void registerReaction(String emote, Consumer<Message> consumer) {
    reactions.put(emote, consumer);
  }

  /**
   * @return list of all emotes used in this reaction listener
   */
  public Set<String> getEmotes() {
    return reactions.keySet();
  }

  /**
   * updates the timestamp when the reaction was last accessed
   */
  public void updateLastAction() {
    lastAction = System.currentTimeMillis();
  }

  /**
   * When does this reaction listener expire?
   *
   * @return timestamp in millis
   */
  public Long getExpiresInTimestamp() {
    return lastAction + expiresIn;
  }

  public long getUserId() {
    return userId;
  }
}
public class ReactionHandler {

  private final ConcurrentHashMap<Long, ConcurrentHashMap<Long, ReactionListener<?>>> reactions;

  private ReactionHandler() {
    reactions = new ConcurrentHashMap<>();
  }

  public synchronized void addReactionListener(long guildId, Message message, ReactionListener<?> handler) {
    addReactionListener(guildId, message, handler, true);
  }

  public synchronized void addReactionListener(long guildId, Message message, ReactionListener<?> handler, boolean queue) {
    if (handler == null) {
      return;
    }
    if (message.getChannelType().equals(ChannelType.TEXT)) {
      if (!PermissionUtil.checkPermission(message.getTextChannel(), message.getGuild().getSelfMember(), Permission.MESSAGE_ADD_REACTION)) {
        return;
      }
    }
    if (!reactions.containsKey(guildId)) {
      reactions.put(guildId, new ConcurrentHashMap<>());
    }
    if (!reactions.get(guildId).containsKey(message.getIdLong())) {
      for (String emote : handler.getEmotes()) {
        RestAction<Void> action = message.addReaction(emote);
        if (queue) action.queue(); else action.complete();
      }
    }
    reactions.get(guildId).put(message.getIdLong(), handler);
  }

  public synchronized void removeReactionListener(long guildId, long messageId) {
    if (!reactions.containsKey(guildId)) return;
    reactions.get(guildId).remove(messageId);
  }

  /**
   * Handles the reaction
   *
   * @param channel   TextChannel of the message
   * @param messageId id of the message
   * @param userId    id of the user reacting
   * @param reaction  the reaction
   */
  public void handle(TextChannel channel, long messageId, long userId, MessageReaction reaction) {
    ReactionListener<?> listener = reactions.get(channel.getGuild().getIdLong()).get(messageId);
    if (!listener.isActive() || listener.getExpiresInTimestamp() < System.currentTimeMillis()) {
      reactions.get(channel.getGuild().getIdLong()).remove(messageId);
    } else if ((listener.hasReaction(reaction.getReactionEmote().getName())) && listener.getUserId() == userId) {
      reactions.get(channel.getGuild().getIdLong()).get(messageId).updateLastAction();
      Message message = channel.retrieveMessageById(messageId).complete();
      listener.react(reaction.getReactionEmote().getName(), message);
    }
  }

  /**
   * Do we have an event for a message?
   *
   * @param guildId   discord guild-id of the message
   * @param messageId id of the message
   * @return do we have an handler?
   */
  public boolean canHandle(long guildId, long messageId) {
    return reactions.containsKey(guildId) && reactions.get(guildId).containsKey(messageId);
  }

  public synchronized void removeGuild(long guildId) {
    reactions.remove(guildId);
  }

  /**
   * Delete expired handlers
   */
  public synchronized void cleanCache() {
    long now = System.currentTimeMillis();
    for (Iterator<Map.Entry<Long, ConcurrentHashMap<Long, ReactionListener<?>>>> iterator = reactions.entrySet().iterator(); iterator.hasNext(); ) {
      Map.Entry<Long, ConcurrentHashMap<Long, ReactionListener<?>>> mapEntry = iterator.next();
      mapEntry.getValue().values().removeIf(listener -> !listener.isActive() || listener.getExpiresInTimestamp() < now);
      if (mapEntry.getValue().values().isEmpty()) {
        reactions.remove(mapEntry.getKey());
      }
    }
  }
}
channel.sendMessage(info.build()).queue((msg) -> {

  ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId());
  handler.setExpiresIn(TimeUnit.MINUTES, 1);
  handler.registerReaction("✔️", (ret) -> foo());
  handler.registerReaction("X",  (ret) -> bar());

  reactionHandler.addReactionListener(guild.getIdLong(), msg, handler);
});
 类似资料:
  • 如何在一条消息中发送多个嵌入?像这样发送多个: 输出: 我找不到任何关于发送多个嵌入的文档,在discord.js文件中提到了它,发送了一个嵌入列表。虽然我试过这个,但它也不起作用。

  • 目前,我正在为不和谐制作一个相当简单的日志机器人。它所做的只是向服务器中的任何#log通道发送。 下面是一段凌乱的代码: 是记录非机器人聊天的设置。目前,它似乎没有在行中设置到正在交谈的用户的直接配置文件链接。我试图搜索一个特定的方法,获得一个配置文件链接,但目前我无法找到它。

  • 我有:作者(BOT)、令牌和频道号。 我的问题是:如何在没有事件的情况下将消息发送到这个通道?

  • 我有一个Java Servlet类,当我的应用程序中存在身份验证问题时,我尝试在其中设置错误状态。但是,问题是,我可以设置错误状态,但无法在响应正文中设置错误消息。以下是我的尝试: 授权筛选器。Java语言 例外处理程序。Java语言 我的Trowable包含要显示的正确HTTP状态。我尝试了两件事: 我试着按照这个方法做:public ultsendError(int sc,java.lang.

  • 我希望在邮件中插入一行“view.mail.warning”。它包含一个html代码,但是使用encodeas=“html”,消息也会显示一个href...而不显示链接。做这些事情的正确方法是什么?。我不想在以下部分中插入消息:(。 在普惠制中: <“p style='color:#ddd;font-size:12px;text-align:center;'><”g:message code=“v

  • 我正在使用nodemailer和express。。。 我想在表单提交或未提交时在前端添加文本消息和加载。 .ts