如何使用JDA对嵌入消息添加多个反应

时间:2019-12-19 19:49:06

标签: java discord discord-jda

我正在尝试使机器人通过嵌入的消息来响应命令,该消息在其下具有多个反应。 我已经知道它可以添加1个反应,但是我还需要添加不同的反应,例如:(http://prntscr.com/qd8da4)在线上有很多教程添加了1个反应,但是没有一个教程添加了多个反应

我正在使用最新版本的Discord JDA。

我当前拥有的代码是:

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

2 个答案:

答案 0 :(得分:2)

我自己也遇到了同样的问题,因为实际上并没有一种本能的方式来完成我想要的工作,即单击反应时要执行某些机器人操作。

我最终创建了几个帮助器类,当单击反应时,该类允许程序进行回调:

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

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

接下来,我们要在单击反应时对其进行处理。此方法需要当前登录的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);
});

答案 1 :(得分:0)

在回调中使用多行块:

channel.sendMessage(info.build()).queue(message -> {
  message.addReaction(a).queue();
  message.addReaction(b).queue();
  message.addReaction(c).queue();
...
});