Skip to content

Instantly share code, notes, and snippets.

@iProdigy
Last active October 31, 2021 01:49
Show Gist options
  • Save iProdigy/eb7e34497bc67c5b0793e1a62e422d16 to your computer and use it in GitHub Desktop.
Save iProdigy/eb7e34497bc67c5b0793e1a62e422d16 to your computer and use it in GitHub Desktop.
import com.github.twitch4j.chat.TwitchChat;
import com.github.twitch4j.chat.TwitchChatBuilder;
import com.github.twitch4j.chat.events.AbstractChannelEvent;
import com.github.twitch4j.chat.events.channel.GiftSubscriptionsEvent;
import com.github.twitch4j.chat.events.channel.SubscriptionEvent;
import com.github.twitch4j.common.events.domain.EventChannel;
import com.github.twitch4j.common.events.domain.EventUser;
import com.github.twitch4j.common.util.TwitchUtils;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
public class SubGiftBufferExample {
public static void main(String[] args) {
final TwitchChat chat = TwitchChatBuilder.builder().build();
final Map<String, Map<String, Collection<BlockingQueue<SubscriptionEvent>>>> buffersByGifterIdByChannelId = new ConcurrentHashMap<>();
chat.getEventManager().onEvent(GiftSubscriptionsEvent.class, e -> {
buffersByGifterIdByChannelId.computeIfAbsent(e.getChannel().getId(), c -> new ConcurrentHashMap<>())
.computeIfAbsent(getGifterId(e.getUser()), s -> ConcurrentHashMap.newKeySet(1))
.add(new ArrayBlockingQueue<>(e.getCount()));
});
chat.getEventManager().onEvent(SubscriptionEvent.class, e -> {
if (!e.getGifted()) return;
Map<String, Collection<BlockingQueue<SubscriptionEvent>>> buffersByGifterId = buffersByGifterIdByChannelId.get(e.getChannel().getId());
if (buffersByGifterId == null || buffersByGifterId.isEmpty()) return;
String gifterId = getGifterId(e.getGiftedBy());
Collection<BlockingQueue<SubscriptionEvent>> buffers = buffersByGifterId.get(gifterId);
if (buffers == null || buffers.isEmpty()) return;
for (BlockingQueue<SubscriptionEvent> buffer : buffers) {
if (buffer.offer(e)) {
if (buffer.remainingCapacity() <= 0 && buffers.remove(buffer)) {
if (buffers.isEmpty())
buffersByGifterId.computeIfPresent(gifterId, (k, v) -> v.isEmpty() ? null : v); // some more cleanup
chat.getEventManager().publish(new SpecificSubGiftEvent(e.getChannel(), e.getGiftedBy(), buffer));
}
break;
}
}
});
// example handler
chat.getEventManager().onEvent(SpecificSubGiftEvent.class, e -> {
System.out.println(e.gifter + " gifted in " + e.getChannel() + " to: " + e.events.stream().map(SubscriptionEvent::getUser).collect(Collectors.toList()));
});
}
public static class SpecificSubGiftEvent extends AbstractChannelEvent {
private final EventUser gifter;
private final Collection<SubscriptionEvent> events;
public SpecificSubGiftEvent(EventChannel channel, EventUser gifter, Collection<SubscriptionEvent> events) {
super(channel);
this.gifter = gifter;
this.events = events;
}
public EventUser getGifter() {
return this.gifter;
}
public Collection<SubscriptionEvent> getEvents() {
return this.events;
}
}
private static String getGifterId(EventUser user) {
return user != null && user.getId() != null ? user.getId() : TwitchUtils.ANONYMOUS_GIFTER.getId();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment