Skip to content

Commit

Permalink
chore: The days of PubSub draw near to an end
Browse files Browse the repository at this point in the history
- Migrate Channel Moderate events over to EventSub
  • Loading branch information
phroggster committed Dec 27, 2024
1 parent 2bc5e84 commit 84868a8
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/backend/auth/twitch-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class TwitchAuthProviders {
'moderator:manage:shield_mode',
'moderator:manage:shoutouts',
'moderator:manage:unban_requests',
'moderator:manage:warnings',
'moderator:read:automod_settings',
'moderator:read:blocked_terms',
'moderator:read:chat_settings',
Expand Down
2 changes: 1 addition & 1 deletion src/backend/events/builtin/twitch-event-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ module.exports = {
subscribers: "Subscribers Only",
followers: "Followers",
slow: "Slow",
r9kbeta: "Unique Chat"
uniquechat: "Unique Chat"
},
value: "emoteonly"
},
Expand Down
2 changes: 1 addition & 1 deletion src/backend/events/filters/builtin/twitch/chat-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const filter: EventFilter = {
display: "Slow"
},
{
value: "r9kbeta",
value: "uniquechat",
display: "Unique Chat"
}
];
Expand Down
87 changes: 71 additions & 16 deletions src/backend/twitch-api/eventsub/eventsub-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,22 +472,6 @@ class TwitchEventSubClient {
});
this._subscriptions.push(channelUpdateSubscription);

// Moderator added
const channelModeratorAddSubscription = this._eventSubListener.onChannelModeratorAdd(streamer.userId, (event) => {
chatRolesManager.addModeratorToModeratorsList({
id: event.userId,
username: event.userName,
displayName: event.userDisplayName
});
});
this._subscriptions.push(channelModeratorAddSubscription);

// Moderator removed
const channelModeratorRemoveSubscription = this._eventSubListener.onChannelModeratorRemove(streamer.userId, (event) => {
chatRolesManager.removeModeratorFromModeratorsList(event.userId);
});
this._subscriptions.push(channelModeratorRemoveSubscription);

// Ad break start/end
const channelAdBreakBeginSubscription = this._eventSubListener.onChannelAdBreakBegin(streamer.userId, (event) => {
twitchEventsHandler.ad.triggerAdBreakStart(
Expand All @@ -513,6 +497,77 @@ class TwitchEventSubClient {
}, adBreakEndTime.getTime() - (new Date()).getTime());
});
this._subscriptions.push(channelAdBreakBeginSubscription);

// Channel Moderate
const channelModerateSubscription = this._eventSubListener.onChannelModerate(streamer.userId, streamer.userId, (event) => {
switch (event.moderationAction) {
case "clear":
frontendCommunicator.send("twitch:chat:clear-feed", event.moderatorName);
twitchEventsHandler.chat.triggerChatCleared(event.moderatorName, event.moderatorId);
break;
case "mod":
chatRolesManager.addModeratorToModeratorsList({
id: event.userId,
username: event.userName,
displayName: event.userDisplayName
});
break;
case "unmod":
chatRolesManager.removeModeratorFromModeratorsList(event.userId);
break;
case "vip":
chatRolesManager.addVipToVipList({
id: event.userId,
username: event.userName,
displayName: event.userDisplayName
});
break;
case "unvip":
chatRolesManager.removeVipFromVipList(event.userId);
break;

// chat modes
case "emoteonly":
case "emoteonlyoff":
case "subscribers":
case "subscribersoff":
case "followers":
case "followersoff":
case "slow":
case "slowoff":
case "uniquechat":
case "uniquechatoff":
twitchEventsHandler.chatModeChanged.triggerChatModeChanged(
event.moderationAction,
event.moderationAction.includes("off") ? "disabled" : "enabled",
event.moderatorName,
event.moderationAction === "slow" ? event.waitTimeSeconds : null
);
break;

// Reserving; already handled in bespoke events; less expensive to move those here.
case "ban":
case "unban":
case "raid":
case "unraid":
case "timeout":
case "untimeout":
break;

// Available for future use:
case "add_blocked_term":
case "add_permitted_term":
case "approve_unban_request":
case "deny_unban_request":
case "delete":
case "remove_blocked_term":
case "remove_permitted_term":
case "warn":
default:
break;
}
});
this._subscriptions.push(channelModerateSubscription);
}

async createClient(): Promise<void> {
Expand Down
44 changes: 0 additions & 44 deletions src/backend/twitch-api/pubsub/pubsub-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const logger = require("../../logwrapper");
const accountAccess = require("../../common/account-access");
const frontendCommunicator = require("../../common/frontend-communicator");
const firebotDeviceAuthProvider = require("../../auth/firebot-device-auth-provider");
const chatRolesManager = require("../../roles/chat-roles-manager");
const { PubSubClient } = require("@twurple/pubsub");
const chatCommandHandler = require("../../chat/commands/chat-command-handler");

Expand Down Expand Up @@ -116,49 +115,6 @@ async function createClient() {
});
listeners.push(autoModListener);

const modListener = pubSubClient.onModAction(streamer.userId, streamer.userId, (message) => {
const frontendCommunicator = require("../../common/frontend-communicator");

switch (message.type) {
case "vip_added":
chatRolesManager.addVipToVipList({
id: message.targetUserId,
username: message.targetUserName
});
break;
default:
switch (message.action) {
case "clear": {
const { userName, userId } = message;
frontendCommunicator.send("twitch:chat:clear-feed", userName);
twitchEventsHandler.chat.triggerChatCleared(userName, userId);
break;
}
case "emoteonly":
case "emoteonlyoff":
case "subscribers":
case "subscribersoff":
case "followers":
case "followersoff":
case "slow":
case "slowoff":
case "r9kbeta": // Unique Chat
case "r9kbetaoff":
twitchEventsHandler.chatModeChanged.triggerChatModeChanged(
message.action,
message.action.includes("off") ? "disabled" : "enabled",
message.userName,
message.args ? parseInt(message.args[0]) : null
);
break;
default:
return;
}
break;
}
});
listeners.push(modListener);

const chatRoomListener = pubSubClient.onCustomTopic(streamer.userId, "stream-chat-room-v1", async (event) => {
const message = event?.data;
if (message?.type === "extension_message") {
Expand Down
4 changes: 2 additions & 2 deletions src/backend/variables/builtin/twitch/chat/mode/chat-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const model : ReplaceVariable = {
case "slow":
case "slowoff":
return "Slow";
case "r9kbeta":
case "r9kbetaoff":
case "uniquechat":
case "uniquechatoff":
return "Unique Chat";
default:
return "";
Expand Down

0 comments on commit 84868a8

Please sign in to comment.