1
0

♻️ Extracted chatClient and scheduledActions

This commit is contained in:
2021-06-18 17:09:10 +02:00
parent 42bd583a43
commit f23e8cb81b
4 changed files with 228 additions and 153 deletions

153
index.ts
View File

@@ -1,28 +1,14 @@
import { PubSubClient, PubSubRedemptionMessage } from "twitch-pubsub-client"; import { PubSubClient, PubSubRedemptionMessage } from "twitch-pubsub-client";
import { broadcast, chatClient, connect, say, } from "./src/backend/chatClient";
import { getApiClient, getAuthProvider } from "./src/backend/helpers/twitch"; import { getApiClient, getAuthProvider } from "./src/backend/helpers/twitch";
import { listen, sockets } from "./src/backend/webServer"; import { saveScheduledActions, scheduledActions } from "./src/backend/helpers/scheduledActions";
import { ApiClient } from "twitch"; import { ApiClient } from "twitch";
import { ChatClient } from "twitch-chat-client"; import { listen, } from "./src/backend/webServer";
import { promises as fs } from "fs";
const SCHEDULED_FILE = "./scheduled.json";
const scheduledActions: Array<any> = [];
let saInterval: NodeJS.Timeout;
const channel = "alexbcberio"; const channel = "alexbcberio";
let apiClient: ApiClient; let apiClient: ApiClient;
let chatClient: ChatClient;
export {
handleClientAction,
scheduledActions,
saveScheduledActions
}
//! Important: store users & channels by id, not by username
async function init() { async function init() {
const authProvider = await getAuthProvider(); const authProvider = await getAuthProvider();
@@ -35,46 +21,13 @@ async function init() {
console.log("[Twitch PubSub] Connected & registered"); console.log("[Twitch PubSub] Connected & registered");
chatClient = new ChatClient(authProvider, { channels: [channel] }); await connect(authProvider, [channel]);
chatClient.onConnect(onConnect);
chatClient.onDisconnect((e: any) => {
console.log(`[ChatClient] Disconnected ${e.message}`);
});
chatClient.onNoPermission((channel, message) => {
console.log(`[ChatClient] No permission on ${channel}: ${message}`);
});
chatClient.connect();
listen(); listen();
} }
init(); init();
async function onConnect() {
console.log("[ChatClient] Connected");
// *Check this, not working
if (!saInterval) {
let savedActions = [];
try {
savedActions = JSON.parse(
(await fs.readFile(SCHEDULED_FILE)).toString()
);
} catch (e) {
// probably file does not exist
}
scheduledActions.push.apply(scheduledActions, savedActions);
scheduledActions.sort((a, b) => a.scheduledAt - b.scheduledAt);
setTimeout(checkScheduledActions, 1000 * 5);
saInterval = setInterval(checkScheduledActions, 1000 * 60);
}
}
async function onRedemption(message: PubSubRedemptionMessage) { async function onRedemption(message: PubSubRedemptionMessage) {
console.log( console.log(
`Reward: "${message.rewardName}" (${message.rewardId}) redeemed by ${message.userDisplayName}` `Reward: "${message.rewardName}" (${message.rewardId}) redeemed by ${message.userDisplayName}`
@@ -110,104 +63,6 @@ async function onRedemption(message: PubSubRedemptionMessage) {
} }
} }
async function handleClientAction(action: any) {
if (action.channel && !isNaN(action.channel)) {
action.channel = await getUsernameFromId(parseInt(action.channel));
}
if (action.username && !isNaN(action.username)) {
action.username = await getUsernameFromId(parseInt(action.username));
}
switch (action.action) {
case "say":
say(channel, action.message);
break;
case "timeout":
await timeout(channel, action.username, action.time, action.reason);
break;
case "broadcast":
broadcast(action.message);
break;
case "addVip":
await addVip(action.channel, action.username);
break;
case "removeVip":
await removeVip(action.channel, action.username);
break;
default:
console.log(`Couldn't handle action:`, action);
}
}
let ssaTimeout: NodeJS.Timeout | null;
function saveScheduledActions() {
if (ssaTimeout) {
clearTimeout(ssaTimeout);
ssaTimeout = null;
console.log("[Scheduled] Removed save timeout.");
}
ssaTimeout = setTimeout(async () => {
await fs.writeFile(SCHEDULED_FILE, JSON.stringify(scheduledActions));
console.log("[Scheduled] Saved actions.");
ssaTimeout = null;
}, 1000 * 30);
}
let checkingScheduled = false;
async function checkScheduledActions() {
if (checkingScheduled) return;
checkingScheduled = true;
let hasToSave = false;
for (let i = 0; i < scheduledActions.length && scheduledActions[i].scheduledAt <= Date.now(); i++) {
hasToSave = true;
const action = scheduledActions.splice(i, 1)[0];
await handleClientAction(action);
console.log(`[Scheduled] Executed: ${JSON.stringify(action)}`);
}
if (hasToSave) {
saveScheduledActions();
}
checkingScheduled = false;
}
// send a chat message
function say(channel: string, message: string) {
chatClient.say(channel, message);
}
// timeouts a user in a channel
async function timeout(
channel: string,
username: string,
time?: number,
reason?: string
) {
if (!time) {
time = 60;
}
if (!reason) {
reason = "";
}
try {
await chatClient.timeout(channel, username, time, reason);
} catch (e) {
// user cannot be timed out
}
}
// broadcast a message to all clients
function broadcast(msg: object) {
sockets.forEach(s => s.send(JSON.stringify(msg)));
}
// adds a user to vips // adds a user to vips
async function addVip(channel: string, username: string, message?: string) { async function addVip(channel: string, username: string, message?: string) {
if (!message) { if (!message) {

View File

@@ -0,0 +1,145 @@
import { AuthProvider } from "twitch-auth";
import { ChatClient } from "twitch-chat-client";
import { getApiClient } from "../helpers/twitch";
import { sockets } from "../webServer";
import { start } from "../helpers/scheduledActions";
let chatClient: ChatClient;
export {
chatClient,
connect,
handleClientAction,
broadcast,
say
};
async function connect(authProvider: AuthProvider, channels: Array<any>) {
if (
!chatClient ||
chatClient.isConnecting ||
chatClient.isConnected
) {
return;
}
chatClient = new ChatClient(authProvider, { channels: channels });
chatClient.onConnect(onConnect);
chatClient.onDisconnect((e: any) => {
console.log(`[ChatClient] Disconnected ${e.message}`);
});
chatClient.onNoPermission((channel, message) => {
console.log(`[ChatClient] No permission on ${channel}: ${message}`);
});
await chatClient.connect();
}
async function onConnect() {
console.log("[ChatClient] Connected");
start();
}
async function handleClientAction(action: any) {
if (action.channel && !isNaN(action.channel)) {
action.channel = await getUsernameFromId(parseInt(action.channel));
}
if (action.username && !isNaN(action.username)) {
action.username = await getUsernameFromId(parseInt(action.username));
}
switch (action.action) {
case "say":
// TODO: check if it works
// say(channel, action.message);
say(action.channel, action.message);
break;
case "timeout":
// TODO: check if it works
// await timeout(channel, action.username, action.time, action.reason);
await timeout(action.channel, action.username, action.time, action.reason);
break;
case "broadcast":
broadcast(action.message);
break;
case "addVip":
await addVip(action.channel, action.username);
break;
case "removeVip":
await removeVip(action.channel, action.username);
break;
default:
console.log(`Couldn't handle action:`, action);
}
}
// send a chat message
function say(channel: string, message: string) {
chatClient.say(channel, message);
}
// timeouts a user in a channel
async function timeout(
channel: string,
username: string,
time?: number,
reason?: string
) {
if (!time) {
time = 60;
}
if (!reason) {
reason = "";
}
try {
await chatClient.timeout(channel, username, time, reason);
} catch (e) {
// user cannot be timed out
}
}
// broadcast a message to all clients
function broadcast(msg: string, socket?: any) {
const filteredSockets = socket
? sockets.filter(s => s !== socket)
: sockets;
filteredSockets.forEach(s => s.send(msg));
}
// adds a user to vips
async function addVip(channel: string, username: string, message?: string) {
if (!message) {
message = `Otorgado VIP a @${username}.`;
}
await chatClient.addVip(channel, username);
say(channel, message);
}
// removes a user from vips
async function removeVip(channel: string, username: string, message?: string) {
if (!message) {
message = `Se ha acabado el chollo, VIP de @${username} eliminado.`;
}
await chatClient.removeVip(channel, username);
say(channel, message);
}
async function getUsernameFromId(userId: number) {
const apiClient = await getApiClient();
const user = await apiClient.helix.users.getUserById(userId);
if (!user) {
return null;
}
return user.displayName;
}

View File

@@ -0,0 +1,74 @@
import { promises as fs } from "fs";
import { handleClientAction } from "../chatClient";
import { resolve } from "path";
export {
start,
scheduledActions,
checkScheduledActions,
saveScheduledActions
};
const SCHEDULED_FILE = resolve(process.cwd(), "scheduled.json");
const scheduledActions: Array<any> = [];
let checkingScheduled = false;
let scheduledActionsInterval: NodeJS.Timeout;
let saveScheduledActionsTimeout: NodeJS.Timeout | null;
async function start() {
// *Check this, not working
if (!scheduledActionsInterval) {
let savedActions = [];
try {
savedActions = JSON.parse((await fs.readFile(SCHEDULED_FILE)).toString());
} catch (e) {
// probably file does not exist
}
scheduledActions.push.apply(scheduledActions, savedActions);
scheduledActions.sort((a, b) => a.scheduledAt - b.scheduledAt);
setTimeout(checkScheduledActions, 1000 * 5);
scheduledActionsInterval = setInterval(checkScheduledActions, 1000 * 60);
}
}
async function checkScheduledActions() {
if (checkingScheduled) return;
checkingScheduled = true;
let hasToSave = false;
for (
let i = 0;
i < scheduledActions.length &&
scheduledActions[i].scheduledAt <= Date.now();
i++
) {
hasToSave = true;
const action = scheduledActions.splice(i, 1)[0];
await handleClientAction(action);
console.log(`[Scheduled] Executed: ${JSON.stringify(action)}`);
}
if (hasToSave) {
saveScheduledActions();
}
checkingScheduled = false;
}
function saveScheduledActions() {
if (saveScheduledActionsTimeout) {
clearTimeout(saveScheduledActionsTimeout);
saveScheduledActionsTimeout = null;
console.log("[Scheduled] Removed save timeout.");
}
saveScheduledActionsTimeout = setTimeout(async () => {
await fs.writeFile(SCHEDULED_FILE, JSON.stringify(scheduledActions));
console.log("[Scheduled] Saved actions.");
saveScheduledActionsTimeout = null;
}, 1000 * 30);
}

View File

@@ -1,5 +1,6 @@
import { IncomingMessage, Server } from "http"; import { IncomingMessage, Server } from "http";
import { handleClientAction, saveScheduledActions, scheduledActions } from "../../.."; import { broadcast, handleClientAction } from "../chatClient";
import { saveScheduledActions, scheduledActions } from "../helpers/scheduledActions";
import { AddressInfo } from "net"; import { AddressInfo } from "net";
import { Socket } from "net"; import { Socket } from "net";
@@ -19,6 +20,7 @@ let server: Server;
export { export {
listen, listen,
// TODO: use intermediate class to handle socket messages
sockets sockets
} }
@@ -67,9 +69,8 @@ function onConnection(socket: WebSocket, req: IncomingMessage) {
async function onMessage(msg: string, socket: WebSocket) { async function onMessage(msg: string, socket: WebSocket) {
const data = JSON.parse(msg); const data = JSON.parse(msg);
// broadcast message
if (!data.actions || data.actions.length === 0) { if (!data.actions || data.actions.length === 0) {
sockets.filter(s => s !== socket).forEach(s => s.send(msg)); broadcast(msg, socket);
return; return;
} }
@@ -78,7 +79,7 @@ async function onMessage(msg: string, socket: WebSocket) {
await handleClientAction(action); await handleClientAction(action);
} else { } else {
scheduledActions.push(action); scheduledActions.push(action);
scheduledActions.sort((a, b) => a.scheduledAt - b.scheduledAt); scheduledActions.sort((a: any, b: any) => a.scheduledAt - b.scheduledAt);
saveScheduledActions(); saveScheduledActions();
} }
} }