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

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;
}