1
0

♻️ Extract chatClient and actions into folders

This commit is contained in:
2021-12-26 01:16:23 +01:00
parent ca34a6a090
commit 93ef43ef12
10 changed files with 155 additions and 157 deletions

View File

@@ -0,0 +1,18 @@
import { chatClient } from "../..";
import { say } from "..";
// adds a user to vips
async function addVip(
channel: string,
username: string,
message?: string
): Promise<void> {
if (!message) {
message = `Otorgado VIP a @${username}.`;
}
await chatClient.addVip(channel, username);
say(channel, message);
}
export { addVip };

View File

@@ -0,0 +1,6 @@
import { addVip } from "./addVip";
import { removeVip } from "./removeVip";
import { say } from "./say";
import { timeout } from "./timeout";
export { say, timeout, addVip, removeVip };

View File

@@ -0,0 +1,18 @@
import { chatClient } from "../..";
import { say } from "..";
// removes a user from vips
async function removeVip(
channel: string,
username: string,
message?: string
): Promise<void> {
if (!message) {
message = `VIP de @${username} eliminado.`;
}
await chatClient.removeVip(channel, username);
say(channel, message);
}
export { removeVip };

View File

@@ -0,0 +1,8 @@
import { chatClient } from "../..";
// send a chat message
async function say(channel: string, message: string): Promise<void> {
await chatClient.say(channel, message);
}
export { say };

View File

@@ -0,0 +1,25 @@
import { chatClient } from "../..";
// timeouts a user in a channel
async function timeout(
channel: string,
username: string,
time?: number,
reason?: string
): Promise<void> {
if (!time) {
time = 60;
}
if (!reason) {
reason = "";
}
try {
await chatClient.timeout(channel, username, time, reason);
} catch (e) {
// user cannot be timed out
}
}
export { timeout };

View File

@@ -0,0 +1,73 @@
import { addVip, removeVip, say, timeout } from "./clientActions";
import { getAuthProvider, getUsernameFromId } from "../helpers/twitch";
import { Action } from "../../interfaces/actions/Action";
import { ChatClient } from "twitch-chat-client";
import { broadcast } from "../helpers/webServer";
import { start } from "../helpers/scheduledActions";
let chatClient: ChatClient;
export { chatClient, connect, handleClientAction, say, LOG_PREFIX };
const LOG_PREFIX = "[ChatClient] ";
async function connect(channels: Array<any>): Promise<void> {
const authProvider = await getAuthProvider();
if (chatClient && (chatClient.isConnecting || chatClient.isConnected)) {
return;
}
chatClient = new ChatClient(authProvider, { channels: channels });
chatClient.onConnect(onConnect);
chatClient.onDisconnect((e: any) => {
console.log(`${LOG_PREFIX}Disconnected ${e.message}`);
});
chatClient.onNoPermission((channel, message) => {
console.log(`${LOG_PREFIX}No permission on ${channel}: ${message}`);
});
await chatClient.connect();
}
async function onConnect(): Promise<void> {
console.log(`${LOG_PREFIX}Connected`);
start();
}
async function handleClientAction(action: Action): Promise<void> {
const [channel, username] = await Promise.all([
getUsernameFromId(parseInt(action.channelId)),
getUsernameFromId(parseInt(action.userId))
]);
if (!channel || !username) {
console.log(`${[LOG_PREFIX]}ChannelId or userId could not be solved`);
return;
}
switch (action.type) {
case "say":
say(channel, action.data.message);
break;
case "timeout":
await timeout(channel, username, action.data.time, action.data.reason);
break;
case "broadcast":
broadcast(action.data.message);
break;
case "addVip":
await addVip(channel, username);
break;
case "removeVip":
await removeVip(channel, username);
break;
default:
console.log(`${[LOG_PREFIX]}Couldn't handle action:`, action);
}
}

View File

@@ -1,153 +0,0 @@
import { getAuthProvider, getUsernameFromId } from "./twitch";
import { ChatClient } from "twitch-chat-client";
import { broadcast } from "./webServer";
import { start } from "./scheduledActions";
let chatClient: ChatClient;
export {
chatClient,
connect,
handleClientAction,
say
};
// TODO: clean/refactor code
const LOG_PREFIX = "[ChatClient] ";
async function connect(channels: Array<any>): Promise<void> {
const authProvider = await getAuthProvider();
if (
chatClient &&
(
chatClient.isConnecting ||
chatClient.isConnected
)
) {
return;
}
chatClient = new ChatClient(authProvider, { channels: channels });
chatClient.onConnect(onConnect);
chatClient.onDisconnect((e: any) => {
console.log(`${LOG_PREFIX}Disconnected ${e.message}`);
});
chatClient.onNoPermission((channel, message) => {
console.log(`${LOG_PREFIX}No permission on ${channel}: ${message}`);
});
await chatClient.connect();
}
async function onConnect(): Promise<void> {
console.log(`${LOG_PREFIX}Connected`);
start();
}
async function handleClientAction(action: any): Promise<void> {
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));
}
// TODO: create a interface for action messages
if (!action.channel) {
action.channel = "alexbcberio";
}
switch (action.action) {
case "say":
say(action.channel, action.message);
break;
case "timeout":
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(`${[LOG_PREFIX]}Couldn't handle action:`, action);
}
}
// send a chat message
async function say(channel: string, message: string): Promise<void> {
await chatClient.say(channel, message);
}
// timeouts a user in a channel
async function timeout(
channel: string,
username: string,
time?: number,
reason?: string
): Promise<void> {
if (!time) {
time = 60;
}
if (!reason) {
reason = "";
}
try {
await chatClient.timeout(channel, username, time, reason);
} catch (e) {
// user cannot be timed out
}
}
// adds a user to vips
async function addVip(
channel: string,
username: string,
message?: string
): Promise<void> {
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
): Promise<void> {
if (!message) {
message = `VIP de @${username} eliminado.`;
}
await chatClient.removeVip(channel, username);
say(channel, message);
}

View File

@@ -1,5 +1,5 @@
import { promises as fs } from "fs"; import { promises as fs } from "fs";
import { handleClientAction } from "./chatClient"; import { handleClientAction } from "../chatClient";
import { resolve } from "path"; import { resolve } from "path";
export { export {

View File

@@ -1,11 +1,12 @@
import { IncomingMessage, Server } from "http"; import { IncomingMessage, Server } from "http";
import { saveScheduledActions, scheduledActions } from "./scheduledActions"; import { saveScheduledActions, scheduledActions } from "./scheduledActions";
import { Action } from "../../interfaces/actions/Action";
import { AddressInfo } from "net"; import { AddressInfo } from "net";
import { Socket } from "net"; import { Socket } from "net";
import WebSocket from "ws"; import WebSocket from "ws";
import express from "express"; import express from "express";
import { handleClientAction } from "./chatClient"; import { handleClientAction } from "../chatClient";
import { isDevelopment } from "./util"; import { isDevelopment } from "./util";
import { join } from "path"; import { join } from "path";
@@ -93,7 +94,9 @@ async function onMessage(msg: string) {
return; return;
} }
for (const action of data.actions) { const actions: Array<Action> = data.actions;
for (const action of actions) {
if (!action.scheduledAt) { if (!action.scheduledAt) {
await handleClientAction(action); await handleClientAction(action);
} else { } else {

View File

@@ -1,4 +1,4 @@
import { connect } from "./helpers/chatClient"; import { connect } from "./chatClient";
import { listen } from "./helpers/webServer"; import { listen } from "./helpers/webServer";
import { registerUserListener } from "./helpers/pubSubClient"; import { registerUserListener } from "./helpers/pubSubClient";