🎨 Format code
This commit is contained in:
@@ -52,10 +52,17 @@ async function onConnect(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function handleClientAction(action: any): Promise<void> {
|
async function handleClientAction(action: any): Promise<void> {
|
||||||
if (action.channel && !isNaN(action.channel)) {
|
if (
|
||||||
|
action.channel &&
|
||||||
|
!isNaN(action.channel)
|
||||||
|
) {
|
||||||
action.channel = await getUsernameFromId(parseInt(action.channel));
|
action.channel = await getUsernameFromId(parseInt(action.channel));
|
||||||
}
|
}
|
||||||
if (action.username && !isNaN(action.username)) {
|
|
||||||
|
if (
|
||||||
|
action.username &&
|
||||||
|
!isNaN(action.username)
|
||||||
|
) {
|
||||||
action.username = await getUsernameFromId(parseInt(action.username));
|
action.username = await getUsernameFromId(parseInt(action.username));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +76,12 @@ async function handleClientAction(action: any): Promise<void> {
|
|||||||
say(action.channel, action.message);
|
say(action.channel, action.message);
|
||||||
break;
|
break;
|
||||||
case "timeout":
|
case "timeout":
|
||||||
await timeout(action.channel, action.username, action.time, action.reason);
|
await timeout(
|
||||||
|
action.channel,
|
||||||
|
action.username,
|
||||||
|
action.time,
|
||||||
|
action.reason
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
case "broadcast":
|
case "broadcast":
|
||||||
broadcast(action.message);
|
broadcast(action.message);
|
||||||
@@ -113,7 +125,11 @@ async function timeout(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// adds a user to vips
|
// adds a user to vips
|
||||||
async function addVip(channel: string, username: string, message?: string): Promise<void> {
|
async function addVip(
|
||||||
|
channel: string,
|
||||||
|
username: string,
|
||||||
|
message?: string
|
||||||
|
): Promise<void> {
|
||||||
if (!message) {
|
if (!message) {
|
||||||
message = `Otorgado VIP a @${username}.`;
|
message = `Otorgado VIP a @${username}.`;
|
||||||
}
|
}
|
||||||
@@ -123,7 +139,11 @@ async function addVip(channel: string, username: string, message?: string): Prom
|
|||||||
}
|
}
|
||||||
|
|
||||||
// removes a user from vips
|
// removes a user from vips
|
||||||
async function removeVip(channel: string, username: string, message?: string): Promise<void> {
|
async function removeVip(
|
||||||
|
channel: string,
|
||||||
|
username: string,
|
||||||
|
message?: string
|
||||||
|
): Promise<void> {
|
||||||
if (!message) {
|
if (!message) {
|
||||||
message = `VIP de @${username} eliminado.`;
|
message = `VIP de @${username} eliminado.`;
|
||||||
}
|
}
|
||||||
|
@@ -8,7 +8,7 @@ import { broadcast } from "./webServer";
|
|||||||
|
|
||||||
export {
|
export {
|
||||||
registerUserListener
|
registerUserListener
|
||||||
}
|
};
|
||||||
|
|
||||||
// TODO: clean/refactor code
|
// TODO: clean/refactor code
|
||||||
|
|
||||||
@@ -101,7 +101,11 @@ async function stealVip(msg: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// adds a user to vips
|
// adds a user to vips
|
||||||
async function addVip(channel: string, username: string, message?: string): Promise<void> {
|
async function addVip(
|
||||||
|
channel: string,
|
||||||
|
username: string,
|
||||||
|
message?: string
|
||||||
|
): Promise<void> {
|
||||||
if (!message) {
|
if (!message) {
|
||||||
message = `Otorgado VIP a @${username}.`;
|
message = `Otorgado VIP a @${username}.`;
|
||||||
}
|
}
|
||||||
@@ -120,7 +124,11 @@ async function hasVip(channel: string, username: string): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// removes a user from vips
|
// removes a user from vips
|
||||||
async function removeVip(channel: string, username: string, message?: string): Promise<void> {
|
async function removeVip(
|
||||||
|
channel: string,
|
||||||
|
username: string,
|
||||||
|
message?: string
|
||||||
|
): Promise<void> {
|
||||||
if (!message) {
|
if (!message) {
|
||||||
message = `Se ha acabado el chollo, VIP de @${username} eliminado.`;
|
message = `Se ha acabado el chollo, VIP de @${username} eliminado.`;
|
||||||
}
|
}
|
||||||
|
@@ -44,7 +44,7 @@ async function start(): Promise<void> {
|
|||||||
async function checkScheduledActions(): Promise<void> {
|
async function checkScheduledActions(): Promise<void> {
|
||||||
if (checkingScheduled) {
|
if (checkingScheduled) {
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
checkingScheduled = true;
|
checkingScheduled = true;
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import { TokenData } from "../../interfaces/TokenData";
|
import { TokenData } from "../../interfaces/TokenData";
|
||||||
import {promises as fs} from "fs";
|
import { promises as fs } from "fs";
|
||||||
import { resolve } from "path";
|
import { resolve } from "path";
|
||||||
|
|
||||||
const TOKENS_FILE = "tokens.json";
|
const TOKENS_FILE = "tokens.json";
|
||||||
@@ -8,7 +8,7 @@ const LOG_PREFIX = "[TokenData] ";
|
|||||||
export {
|
export {
|
||||||
getTokenData,
|
getTokenData,
|
||||||
saveTokenData
|
saveTokenData
|
||||||
}
|
};
|
||||||
|
|
||||||
function getTokenDataFilePath(): string {
|
function getTokenDataFilePath(): string {
|
||||||
return resolve(process.cwd(), TOKENS_FILE);
|
return resolve(process.cwd(), TOKENS_FILE);
|
||||||
@@ -21,7 +21,9 @@ async function getTokenData(): Promise<TokenData> {
|
|||||||
try {
|
try {
|
||||||
buffer = await fs.readFile(tokenDataFilePath);
|
buffer = await fs.readFile(tokenDataFilePath);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(`${LOG_PREFIX}${TOKENS_FILE} not found on ${tokenDataFilePath}.`);
|
console.error(
|
||||||
|
`${LOG_PREFIX}${TOKENS_FILE} not found on ${tokenDataFilePath}.`
|
||||||
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,11 +43,10 @@ async function saveTokenData(tokenData: TokenData): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function checkTokenData(tokenData: TokenData): void {
|
function checkTokenData(tokenData: TokenData): void {
|
||||||
if (
|
if (!tokenData.access_token || !tokenData.refresh_token) {
|
||||||
!tokenData.access_token ||
|
console.error(
|
||||||
!tokenData.refresh_token
|
`${LOG_PREFIX}Missing refresh_token or access_token in ${TOKENS_FILE}.`
|
||||||
) {
|
);
|
||||||
console.error(`${LOG_PREFIX}Missing refresh_token or access_token in ${TOKENS_FILE}.`);
|
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,4 +1,8 @@
|
|||||||
import { AccessToken, RefreshableAuthProvider, StaticAuthProvider } from "twitch-auth";
|
import {
|
||||||
|
AccessToken,
|
||||||
|
RefreshableAuthProvider,
|
||||||
|
StaticAuthProvider
|
||||||
|
} from "twitch-auth";
|
||||||
import { getTokenData, saveTokenData } from "./tokenData";
|
import { getTokenData, saveTokenData } from "./tokenData";
|
||||||
|
|
||||||
import { ApiClient } from "twitch";
|
import { ApiClient } from "twitch";
|
||||||
@@ -13,7 +17,7 @@ export {
|
|||||||
getAuthProvider,
|
getAuthProvider,
|
||||||
getApiClient,
|
getApiClient,
|
||||||
getUsernameFromId
|
getUsernameFromId
|
||||||
}
|
};
|
||||||
|
|
||||||
function getClientCredentials(): ClientCredentials {
|
function getClientCredentials(): ClientCredentials {
|
||||||
if (
|
if (
|
||||||
@@ -36,10 +40,7 @@ async function createStaticAuthProvider(): Promise<StaticAuthProvider> {
|
|||||||
let tokenData = await getTokenData();
|
let tokenData = await getTokenData();
|
||||||
const credentials = getClientCredentials();
|
const credentials = getClientCredentials();
|
||||||
|
|
||||||
return new StaticAuthProvider(
|
return new StaticAuthProvider(credentials.clientId, tokenData.access_token);
|
||||||
credentials.clientId,
|
|
||||||
tokenData.access_token
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getAuthProvider(): Promise<RefreshableAuthProvider> {
|
async function getAuthProvider(): Promise<RefreshableAuthProvider> {
|
||||||
@@ -52,30 +53,32 @@ async function getAuthProvider(): Promise<RefreshableAuthProvider> {
|
|||||||
const staticAuthProvider = await createStaticAuthProvider();
|
const staticAuthProvider = await createStaticAuthProvider();
|
||||||
const credentials = getClientCredentials();
|
const credentials = getClientCredentials();
|
||||||
|
|
||||||
const expiry = tokenData.expiryTimestamp === null
|
const expiry =
|
||||||
|
tokenData.expiryTimestamp === null
|
||||||
? null
|
? null
|
||||||
: new Date(tokenData.expiryTimestamp);
|
: new Date(tokenData.expiryTimestamp);
|
||||||
|
|
||||||
refreshAuthProvider = new RefreshableAuthProvider(
|
refreshAuthProvider = new RefreshableAuthProvider(staticAuthProvider, {
|
||||||
staticAuthProvider,
|
|
||||||
{
|
|
||||||
clientSecret: credentials.clientSecret,
|
clientSecret: credentials.clientSecret,
|
||||||
refreshToken: tokenData.refresh_token,
|
refreshToken: tokenData.refresh_token,
|
||||||
expiry,
|
expiry,
|
||||||
onRefresh: onRefresh
|
onRefresh: onRefresh
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
|
||||||
return refreshAuthProvider;
|
return refreshAuthProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onRefresh(refreshData: AccessToken): Promise<void> {
|
async function onRefresh(refreshData: AccessToken): Promise<void> {
|
||||||
const { accessToken, refreshToken, expiryDate } = refreshData;
|
const {
|
||||||
|
accessToken,
|
||||||
|
refreshToken,
|
||||||
|
expiryDate
|
||||||
|
} = refreshData;
|
||||||
console.log(`${LOG_PREFIX}Tokens refreshed`);
|
console.log(`${LOG_PREFIX}Tokens refreshed`);
|
||||||
|
|
||||||
const expiryTimestamp = expiryDate === null
|
const expiryTimestamp = expiryDate === null
|
||||||
? 0
|
? 0
|
||||||
: expiryDate.getTime()
|
: expiryDate.getTime();
|
||||||
|
|
||||||
const newTokenData: TokenData = {
|
const newTokenData: TokenData = {
|
||||||
access_token: accessToken,
|
access_token: accessToken,
|
||||||
|
@@ -24,7 +24,7 @@ let server: Server;
|
|||||||
export {
|
export {
|
||||||
listen,
|
listen,
|
||||||
broadcast
|
broadcast
|
||||||
}
|
};
|
||||||
|
|
||||||
wsServer.on("connection", onConnection);
|
wsServer.on("connection", onConnection);
|
||||||
|
|
||||||
@@ -44,7 +44,9 @@ function listen() {
|
|||||||
|
|
||||||
function onListening() {
|
function onListening() {
|
||||||
console.log(
|
console.log(
|
||||||
`${LOG_PREFIX_HTTP}Listening on port ${(server.address() as AddressInfo).port}`
|
`${LOG_PREFIX_HTTP}Listening on port ${
|
||||||
|
(server.address() as AddressInfo).port
|
||||||
|
}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +57,9 @@ function onUpgrade(req: IncomingMessage, socket: Socket, head: Buffer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onConnection(socket: WebSocket, req: IncomingMessage) {
|
function onConnection(socket: WebSocket, req: IncomingMessage) {
|
||||||
console.log(`${LOG_PREFIX_WS}${req.socket.remoteAddress} New connection established`);
|
console.log(
|
||||||
|
`${LOG_PREFIX_WS}${req.socket.remoteAddress} New connection established`
|
||||||
|
);
|
||||||
sockets.push(socket);
|
sockets.push(socket);
|
||||||
socket.send(
|
socket.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
@@ -81,7 +85,10 @@ async function onMessage(msg: string) {
|
|||||||
const socket = this as WebSocket;
|
const socket = this as WebSocket;
|
||||||
const data = JSON.parse(msg);
|
const data = JSON.parse(msg);
|
||||||
|
|
||||||
if (!data.actions || data.actions.length === 0) {
|
if (
|
||||||
|
!data.actions ||
|
||||||
|
data.actions.length === 0
|
||||||
|
) {
|
||||||
broadcast(msg, socket);
|
broadcast(msg, socket);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -96,7 +103,10 @@ async function onMessage(msg: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`${LOG_PREFIX_WS}Received message with ${data.actions.length} actions:`, data);
|
console.log(
|
||||||
|
`${LOG_PREFIX_WS}Received message with ${data.actions.length} actions:`,
|
||||||
|
data
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onClose() {
|
function onClose() {
|
||||||
|
Reference in New Issue
Block a user