EventWaiter.java (1604B)
1 package com.wimdupont.sxcybot.listeners; 2 3 import com.wimdupont.sxcybot.services.guild.ChannelDetailService; 4 import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel; 5 import net.dv8tion.jda.api.events.message.MessageReceivedEvent; 6 import org.springframework.stereotype.Component; 7 8 import java.util.HashMap; 9 import java.util.Map; 10 import java.util.concurrent.TimeUnit; 11 import java.util.function.Consumer; 12 13 @Component 14 public class EventWaiter { 15 16 private final ChannelDetailService channelDetailService; 17 private static final int TIMEOUT = 1; 18 private static final TimeUnit TIME_UNIT = TimeUnit.MINUTES; 19 private final Map<Long, MessageResponseWaiter> waitingMessages; 20 21 public EventWaiter(ChannelDetailService channelDetailService) { 22 this.channelDetailService = channelDetailService; 23 this.waitingMessages = new HashMap<>(); 24 } 25 26 public void waitForPrivateChannelEvent(Consumer<MessageReceivedEvent> action, 27 MessageReceivedEvent event, 28 PrivateChannel privateChannel) { 29 var authorId = event.getAuthor().getIdLong(); 30 var messageResponseWaiter = new MessageResponseWaiter(channelDetailService.getJda(), authorId, action); 31 32 if (waitingMessages.containsKey(authorId)) 33 waitingMessages.get(authorId).delete(); 34 35 waitingMessages.put(authorId, messageResponseWaiter); 36 37 messageResponseWaiter.waitForEvent(TIMEOUT, TIME_UNIT, 38 () -> privateChannel.sendMessage("Timed out... Please try again later.").queue()); 39 } 40 41 }