Skip to content

Commit

Permalink
feature: add PubSub support for moderation actions (#133)
Browse files Browse the repository at this point in the history
* Add PubSub support for moderation actions

* Fix ModerationAction twitchString & document action types

* Document the fields of ChatModerationAction

* Add channelId to ChatModerationEvent and cache modType & modAction

* Add another version of the chat_moderator_actions topic

* Add AutoMod-related events to ModerationAction

* Add raid/unraid to ModerationAction as they are distinct from host

* Allow the raided channel to be obtained from getTargetedUserName()

* Resolve api inconsistency in ModerationType
  • Loading branch information
iProdigy committed Jun 2, 2020
1 parent b82257e commit 282e2e7
Show file tree
Hide file tree
Showing 4 changed files with 395 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class IRCMessageEvent extends TwitchEvent {
/**
* Client Permissions
*/
private final Set<CommandPermission> clientPermissions = new HashSet<>();
private final Set<CommandPermission> clientPermissions = EnumSet.noneOf(CommandPermission.class);

/**
* RAW Message
Expand Down
35 changes: 35 additions & 0 deletions pubsub/src/main/java/com/github/twitch4j/pubsub/TwitchPubSub.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import com.github.twitch4j.common.util.TwitchUtils;
import com.github.twitch4j.common.util.TypeConvert;
import com.github.twitch4j.pubsub.domain.ChannelPointsRedemption;
import com.github.twitch4j.pubsub.domain.ChatModerationAction;
import com.github.twitch4j.pubsub.domain.PubSubRequest;
import com.github.twitch4j.pubsub.domain.PubSubResponse;
import com.github.twitch4j.pubsub.enums.PubSubType;
import com.github.twitch4j.pubsub.enums.TMIConnectionState;
import com.github.twitch4j.pubsub.events.ChannelPointsRedemptionEvent;
import com.github.twitch4j.pubsub.events.ChatModerationEvent;
import com.github.twitch4j.pubsub.events.RedemptionStatusUpdateEvent;
import com.github.twitch4j.pubsub.events.RewardRedeemedEvent;
import com.neovisionaries.ws.client.WebSocket;
Expand Down Expand Up @@ -300,6 +302,10 @@ public void onTextMessage(WebSocket ws, String text) {
default: eventManager.publish(new ChannelPointsRedemptionEvent(timestamp, redemption));
}

} else if (topic.startsWith("chat_moderator_actions")) {
String channelId = topic.substring(topic.lastIndexOf('.') + 1);
ChatModerationAction data = TypeConvert.convertValue(msgData, ChatModerationAction.class);
eventManager.publish(new ChatModerationEvent(channelId, data));
} else {
log.warn("Unparseable Message: " + message.getType() + "|" + message.getData());
}
Expand Down Expand Up @@ -484,6 +490,35 @@ public PubSubSubscription listenForWhisperEvents(OAuth2Credential credential, St
return listenOnTopic(request);
}

/**
* Event Listener: A moderator performs an action in the channel
*
* @param credential Credential (for channelId, scope: channel:moderate)
* @param channelId Target Channel Id
* @return PubSubSubscription
*/
public PubSubSubscription listenForModerationEvents(OAuth2Credential credential, String channelId) {
final PubSubRequest request = new PubSubRequest();
request.setType(PubSubType.LISTEN);
request.setNonce(UUID.randomUUID().toString());
request.getData().put("auth_token", credential.getAccessToken());
request.getData().put("topics", Collections.singletonList("chat_moderator_actions." + channelId));

return listenOnTopic(request);
}

/**
* Event Listener: A moderator performs an action in the channel
*
* @param credential Credential (for userId, scope: channel:moderate)
* @param userId The user id associated with the credential
* @param roomId The user id associated with the target channel
* @return PubSubSubscription
*/
public PubSubSubscription listenForModerationEvents(OAuth2Credential credential, String userId, String roomId) {
return listenForModerationEvents(credential, userId + "." + roomId);
}

/**
* Event Listener: Anyone makes a channel points redemption on a channel.
*
Expand Down

0 comments on commit 282e2e7

Please sign in to comment.