1
0

🎨 Add file/module name to logs and

order/refactor minimal code
This commit is contained in:
2021-06-18 20:37:00 +02:00
parent fa1e87115b
commit a61accfae0
4 changed files with 52 additions and 50 deletions

View File

@@ -1,7 +1,7 @@
import { getApiClient, getAuthProvider } from "./twitch";
import { getAuthProvider, getUsernameFromId } from "./twitch";
import { ChatClient } from "twitch-chat-client";
import { sockets } from "./webServer";
import { broadcast } from "./webServer";
import { start } from "./scheduledActions";
let chatClient: ChatClient;
@@ -10,10 +10,11 @@ export {
chatClient,
connect,
handleClientAction,
broadcast,
say
};
const LOG_PREFIX = "[ChatClient] ";
async function connect(channels: Array<any>) {
const authProvider = await getAuthProvider(
@@ -33,18 +34,18 @@ async function connect(channels: Array<any>) {
chatClient.onConnect(onConnect);
chatClient.onDisconnect((e: any) => {
console.log(`[ChatClient] Disconnected ${e.message}`);
console.log(`${LOG_PREFIX}Disconnected ${e.message}`);
});
chatClient.onNoPermission((channel, message) => {
console.log(`[ChatClient] No permission on ${channel}: ${message}`);
console.log(`${LOG_PREFIX}No permission on ${channel}: ${message}`);
});
await chatClient.connect();
}
async function onConnect() {
console.log("[ChatClient] Connected");
console.log(`${LOG_PREFIX}Connected`);
start();
}
@@ -79,7 +80,7 @@ async function handleClientAction(action: any) {
await removeVip(action.channel, action.username);
break;
default:
console.log(`Couldn't handle action:`, action);
console.log(`${[LOG_PREFIX]}Couldn't handle action:`, action);
}
}
@@ -110,15 +111,6 @@ async function timeout(
}
}
// 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) {
@@ -132,20 +124,9 @@ async function addVip(channel: string, username: string, message?: string) {
// 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.`;
message = `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;
}