Skip to content

Commit

Permalink
Fixed the Twitch Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice committed Jul 13, 2024
1 parent 445e2ed commit 31d21dd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.github.philippheuer.events4j.simple.domain.EventSubscriber;
import com.github.twitch4j.TwitchClient;
import com.github.twitch4j.events.ChannelGoLiveEvent;
import com.github.twitch4j.events.ChannelGoOfflineEvent;
import com.github.twitch4j.helix.domain.GameList;
import com.github.twitch4j.helix.domain.UserList;
import net.dv8tion.jda.api.EmbedBuilder;
Expand All @@ -25,12 +24,12 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class TwitchLiveEventListener extends SimpleEventHandler {
public class TwitchGoLiveEventListener extends SimpleEventHandler {

private final TwitchClient twitchClient;
private final CafeBot cafeBot;

public TwitchLiveEventListener(final TwitchClient twitchClient, final CafeBot cafeBot) {
public TwitchGoLiveEventListener(final TwitchClient twitchClient, final CafeBot cafeBot) {
this.twitchClient = twitchClient;
this.cafeBot = cafeBot;
}
Expand All @@ -41,14 +40,14 @@ public void onChannelGoLive(final ChannelGoLiveEvent event) {
String twitchID = event.getChannel().getId();
String gameID = event.getStream().getGameId();

cafeBot.getLogger().log(TwitchLiveEventListener.class, LogLevel.DEBUG, "LIVE: " + event.getChannel().getName());
cafeBot.getLogger().log(TwitchGoLiveEventListener.class, LogLevel.DEBUG, "LIVE: " + event.getChannel().getName());

CompletableFuture<UserList> userFuture = CompletableFuture.supplyAsync(() -> {
try { return twitchClient.getHelix().getUsers(null, List.of(twitchID), null).queue().get(); }
catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); }
});

cafeBot.getLogger().log(TwitchLiveEventListener.class, LogLevel.DEBUG, "GAME ID: " + gameID);
cafeBot.getLogger().log(TwitchGoLiveEventListener.class, LogLevel.DEBUG, "GAME ID: " + gameID);
if (gameID == null || gameID.isBlank()) {
userFuture.thenAcceptAsync((users) -> {
handleLiveEvent(event, users.getUsers().getFirst().getProfileImageUrl(), null);
Expand All @@ -67,10 +66,6 @@ public void onChannelGoLive(final ChannelGoLiveEvent event) {
});
}

// TODO: Add offline edit.
@EventSubscriber
public void onChannelGoOffline(final ChannelGoOfflineEvent event) { }

private void handleLiveEvent(final ChannelGoLiveEvent event, final String profileImageURL, @Nullable final String boxArtURL) {
String twitchName = event.getChannel().getName().toLowerCase();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.beanbeanjuice.cafebot.utility.sections.twitch;

import com.beanbeanjuice.cafeapi.wrapper.CafeAPI;
import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.utility.logging.LogLevel;
import com.github.philippheuer.credentialmanager.domain.OAuth2Credential;
Expand All @@ -18,14 +17,10 @@ public TwitchHandler(final String token, final CafeBot cafeBot) {
twitchClient = TwitchClientBuilder.builder()
.withEnableHelix(true)
.withDefaultAuthToken(new OAuth2Credential("twitch", token))
// .withDefaultEventHandler(TwitchLiveEventListener.class)
.build();

// twitchClient.getEventManager().registerEventHandler(new TwitchLiveEventListener(twitchClient, cafeBot));
twitchClient.getEventManager().registerEventHandler(new SimpleEventHandler());
twitchClient.getEventManager().getEventHandler(SimpleEventHandler.class).registerListener(new TwitchLiveEventListener(twitchClient, cafeBot));

// twitchClient.getEventManager().registerEventHandler(new SimpleEventHandler()); // TODO: CONFIRM THIS WORKS
twitchClient.getEventManager().getEventHandler(SimpleEventHandler.class).registerListener(new TwitchGoLiveEventListener(twitchClient, cafeBot));

handleStartup(cafeBot);
}
Expand Down

0 comments on commit 31d21dd

Please sign in to comment.