1
0

🎨 Format code

This commit is contained in:
2021-06-20 00:51:50 +02:00
parent 6735ee5519
commit 71f7546165
6 changed files with 295 additions and 253 deletions

View File

@@ -52,10 +52,17 @@ async function onConnect(): 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));
}
if (action.username && !isNaN(action.username)) {
if (
action.username &&
!isNaN(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);
break;
case "timeout":
await timeout(action.channel, action.username, action.time, action.reason);
await timeout(
action.channel,
action.username,
action.time,
action.reason
);
break;
case "broadcast":
broadcast(action.message);
@@ -113,7 +125,11 @@ async function timeout(
}
// 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) {
message = `Otorgado VIP a @${username}.`;
}
@@ -123,7 +139,11 @@ async function addVip(channel: string, username: string, message?: string): Prom
}
// 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) {
message = `VIP de @${username} eliminado.`;
}

View File

@@ -8,7 +8,7 @@ import { broadcast } from "./webServer";
export {
registerUserListener
}
};
// TODO: clean/refactor code
@@ -101,7 +101,11 @@ async function stealVip(msg: {
}
// 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) {
message = `Otorgado VIP a @${username}.`;
}
@@ -120,7 +124,11 @@ async function hasVip(channel: string, username: string): Promise<boolean> {
}
// 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) {
message = `Se ha acabado el chollo, VIP de @${username} eliminado.`;
}

View File

@@ -44,7 +44,7 @@ async function start(): Promise<void> {
async function checkScheduledActions(): Promise<void> {
if (checkingScheduled) {
return;
};
}
checkingScheduled = true;

View File

@@ -8,7 +8,7 @@ const LOG_PREFIX = "[TokenData] ";
export {
getTokenData,
saveTokenData
}
};
function getTokenDataFilePath(): string {
return resolve(process.cwd(), TOKENS_FILE);
@@ -21,7 +21,9 @@ async function getTokenData(): Promise<TokenData> {
try {
buffer = await fs.readFile(tokenDataFilePath);
} 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);
}
@@ -41,11 +43,10 @@ async function saveTokenData(tokenData: TokenData): Promise<void> {
}
function checkTokenData(tokenData: TokenData): void {
if (
!tokenData.access_token ||
!tokenData.refresh_token
) {
console.error(`${LOG_PREFIX}Missing refresh_token or access_token in ${TOKENS_FILE}.`);
if (!tokenData.access_token || !tokenData.refresh_token) {
console.error(
`${LOG_PREFIX}Missing refresh_token or access_token in ${TOKENS_FILE}.`
);
process.exit(1);
}
}

View File

@@ -1,4 +1,8 @@
import { AccessToken, RefreshableAuthProvider, StaticAuthProvider } from "twitch-auth";
import {
AccessToken,
RefreshableAuthProvider,
StaticAuthProvider
} from "twitch-auth";
import { getTokenData, saveTokenData } from "./tokenData";
import { ApiClient } from "twitch";
@@ -13,7 +17,7 @@ export {
getAuthProvider,
getApiClient,
getUsernameFromId
}
};
function getClientCredentials(): ClientCredentials {
if (
@@ -36,10 +40,7 @@ async function createStaticAuthProvider(): Promise<StaticAuthProvider> {
let tokenData = await getTokenData();
const credentials = getClientCredentials();
return new StaticAuthProvider(
credentials.clientId,
tokenData.access_token
);
return new StaticAuthProvider(credentials.clientId, tokenData.access_token);
}
async function getAuthProvider(): Promise<RefreshableAuthProvider> {
@@ -52,30 +53,32 @@ async function getAuthProvider(): Promise<RefreshableAuthProvider> {
const staticAuthProvider = await createStaticAuthProvider();
const credentials = getClientCredentials();
const expiry = tokenData.expiryTimestamp === null
const expiry =
tokenData.expiryTimestamp === null
? null
: new Date(tokenData.expiryTimestamp);
refreshAuthProvider = new RefreshableAuthProvider(
staticAuthProvider,
{
refreshAuthProvider = new RefreshableAuthProvider(staticAuthProvider, {
clientSecret: credentials.clientSecret,
refreshToken: tokenData.refresh_token,
expiry,
onRefresh: onRefresh
}
);
});
return refreshAuthProvider;
}
async function onRefresh(refreshData: AccessToken): Promise<void> {
const { accessToken, refreshToken, expiryDate } = refreshData;
const {
accessToken,
refreshToken,
expiryDate
} = refreshData;
console.log(`${LOG_PREFIX}Tokens refreshed`);
const expiryTimestamp = expiryDate === null
? 0
: expiryDate.getTime()
: expiryDate.getTime();
const newTokenData: TokenData = {
access_token: accessToken,

View File

@@ -24,7 +24,7 @@ let server: Server;
export {
listen,
broadcast
}
};
wsServer.on("connection", onConnection);
@@ -44,7 +44,9 @@ function listen() {
function onListening() {
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) {
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);
socket.send(
JSON.stringify({
@@ -81,7 +85,10 @@ async function onMessage(msg: string) {
const socket = this as WebSocket;
const data = JSON.parse(msg);
if (!data.actions || data.actions.length === 0) {
if (
!data.actions ||
data.actions.length === 0
) {
broadcast(msg, socket);
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() {