diff --git a/src/backend/pubSubClient/actions/getVip.ts b/src/backend/pubSubClient/actions/getVip.ts index c421feb..da76f0d 100644 --- a/src/backend/pubSubClient/actions/getVip.ts +++ b/src/backend/pubSubClient/actions/getVip.ts @@ -1,48 +1,41 @@ import { addVip, hasVip, removeVip } from "../../chatClient/clientActions"; import { save, vipUsers } from "../../helpers/miniDb"; -import { LOG_PREFIX } from ".."; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { getUsernameFromId } from "../../helpers/twitch"; const MAX_VIPS = 3; -async function getVip( - msg: RedemptionMessage -): Promise { - const channelId = parseInt(msg.channelId); - const channel = await getUsernameFromId(channelId); +async function getVip(msg: RedemptionMessage): Promise { + const channelId = parseInt(msg.channelId); + const channel = await getUsernameFromId(channelId); - if (!channel) { - console.log(`${LOG_PREFIX}No channel found`); + if (!channel) { + throw new Error("No channel found"); + } - return; - } + const addVipUser = msg.userDisplayName; - const addVipUser = msg.userDisplayName; + if (await hasVip(channel, addVipUser)) { + throw new Error(`@${addVipUser} is already VIP`); + } - if (await hasVip(channel, addVipUser)) { - console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`); + const users = vipUsers[channelId]; - return; - } + if (users.length >= MAX_VIPS) { + const user = users.shift(); - const users = vipUsers[channelId]; + if (user) { + await removeVip(channel, user); + } + } - if (users.length >= MAX_VIPS) { - const user = users.shift(); + await addVip(channel, addVipUser); - if (user) { - await removeVip(channel, user); - } - } + users.push(addVipUser); + save(); - await addVip(channel, addVipUser); - - users.push(addVipUser); - save(); - - return msg; + return msg; } export { getVip }; diff --git a/src/backend/pubSubClient/actions/hidrate.ts b/src/backend/pubSubClient/actions/hidrate.ts index b1edce4..4df3bb3 100644 --- a/src/backend/pubSubClient/actions/hidrate.ts +++ b/src/backend/pubSubClient/actions/hidrate.ts @@ -1,24 +1,19 @@ -import { LOG_PREFIX } from ".."; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { getUsernameFromId } from "../../helpers/twitch"; import { say } from "../../chatClient"; -async function hidrate( - msg: RedemptionMessage -): Promise { - const channel = await getUsernameFromId(parseInt(msg.channelId)); +async function hidrate(msg: RedemptionMessage): Promise { + const channel = await getUsernameFromId(parseInt(msg.channelId)); - if (!channel) { - console.log(`${LOG_PREFIX}No channel found`); + if (!channel) { + throw new Error("No channel found"); + } - return; - } + msg.message = `@${msg.userDisplayName} ha invitado a una ronda`; - msg.message = `@${msg.userDisplayName} ha invitado a una ronda`; + await say(channel, "waterGang waterGang waterGang"); - await say(channel, "waterGang waterGang waterGang"); - - return msg; + return msg; } export { hidrate }; diff --git a/src/backend/pubSubClient/actions/highlightMessage.ts b/src/backend/pubSubClient/actions/highlightMessage.ts index 0465d70..4662f12 100644 --- a/src/backend/pubSubClient/actions/highlightMessage.ts +++ b/src/backend/pubSubClient/actions/highlightMessage.ts @@ -5,11 +5,9 @@ import { timeout } from "../../chatClient/clientActions"; async function highlightMessage( msg: RedemptionMessage -): Promise { +): Promise { if (!msg.message) { - console.log(`${LOG_PREFIX}Redemption has no message`); - - return; + throw new Error("Redemption has no message"); } const urlRegex = @@ -20,9 +18,7 @@ async function highlightMessage( const channel = await getUsernameFromId(parseInt(msg.channelId)); if (!channel) { - console.log(`${LOG_PREFIX}No channel found`); - - return; + throw new Error("No channel found"); } try { @@ -34,7 +30,7 @@ async function highlightMessage( // user probably cannot be timed out } - return; + throw new Error("The message cannot contain a url"); } return msg; diff --git a/src/backend/pubSubClient/actions/russianRoulette.ts b/src/backend/pubSubClient/actions/russianRoulette.ts index 7bf7348..3856896 100644 --- a/src/backend/pubSubClient/actions/russianRoulette.ts +++ b/src/backend/pubSubClient/actions/russianRoulette.ts @@ -1,6 +1,5 @@ import { say, timeout } from "../../chatClient/clientActions"; -import { LOG_PREFIX } from ".."; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { getUsernameFromId } from "../../helpers/twitch"; import { randomInt } from "crypto"; @@ -13,14 +12,12 @@ const maxSafeShots = 5; async function russianRoulette( msg: RedemptionMessage -): Promise { +): Promise { const { channelId, userDisplayName } = msg; const channel = await getUsernameFromId(parseInt(channelId)); if (!channel) { - console.log(`${LOG_PREFIX}No channel found`); - - return; + throw new Error("No channel found"); } if (!gunsSafeShots[channelId]) { @@ -41,21 +38,15 @@ async function russianRoulette( // eslint-disable-next-line require-atomic-updates msg.message = win ? "" : "got shot"; - try { - if (!win) { - await timeout(channel, userDisplayName, timeoutSeconds, "F en la ruleta"); + if (!win) { + await timeout(channel, userDisplayName, timeoutSeconds, "F en la ruleta"); - await say( - channel, - `PepeHands ${userDisplayName} no ha sobrevivido para contarlo` - ); - } else { - await say(channel, `rdCool Clap ${userDisplayName}`); - } - } catch (e) { - if (e instanceof Error) { - console.log(`${LOG_PREFIX}${e.message}`); - } + await say( + channel, + `PepeHands ${userDisplayName} no ha sobrevivido para contarlo` + ); + } else { + await say(channel, `rdCool Clap ${userDisplayName}`); } return msg; diff --git a/src/backend/pubSubClient/actions/stealVip.ts b/src/backend/pubSubClient/actions/stealVip.ts index 6970b6a..33db29a 100644 --- a/src/backend/pubSubClient/actions/stealVip.ts +++ b/src/backend/pubSubClient/actions/stealVip.ts @@ -1,27 +1,19 @@ import { addVip, hasVip, removeVip, say } from "../../chatClient/clientActions"; import { save, vipUsers } from "../../helpers/miniDb"; -import { LOG_PREFIX } from ".."; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { getUsernameFromId } from "../../helpers/twitch"; -// remove vip from a user to grant it to yourself -async function stealVip( - msg: RedemptionMessage -): Promise { +async function stealVip(msg: RedemptionMessage): Promise { if (!msg.message) { - console.log(`${LOG_PREFIX}Redemption has no message`); - - return; + throw new Error("Redemption has no message"); } const channelId = parseInt(msg.channelId); const channel = await getUsernameFromId(channelId); if (!channel) { - console.log(`${LOG_PREFIX}No channel found`); - - return; + throw new Error("No channel found"); } const addVipUser = msg.userDisplayName; @@ -29,27 +21,27 @@ async function stealVip( const channelVips = vipUsers[channelId]; if (!channelVips.find((u) => u.toLowerCase() === removeVipUser)) { - const message = - // eslint-disable-next-line no-magic-numbers - channelVips.length === 0 - ? "No hay nadie a quien puedas robar el VIP" - : `Solo puedes robar el VIP de: "${channelVips.sort().join('", "')}"`; + const noVips = 0; + const hasVips = channelVips.length === noVips; + + const message = !hasVips + ? "No hay nadie a quien puedas robar el VIP" + : `Solo puedes robar el VIP de: "${channelVips.sort().join('", "')}"`; await say(channel, message); - return; + if (!hasVips) { + throw new Error("No VIP users to steal from"); + } } if (channelVips.includes(addVipUser) || (await hasVip(channel, addVipUser))) { - console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`); - - return; + throw new Error(`@${addVipUser} is already VIP`); } const removed = await removeVip(channel, removeVipUser); if (!removed && (await hasVip(channel, removeVipUser))) { - console.log(`${LOG_PREFIX}Could not remove VIP of @${removeVipUser}`); - return; + throw new Error(`Could not remove VIP of @${removeVipUser}`); } const added = await addVip(channel, addVipUser); @@ -57,7 +49,7 @@ async function stealVip( if (!added) { await addVip(channel, removeVipUser); - return; + throw new Error(`Could not add VIP to ${addVipUser}`); } const removeIdx = channelVips.findIndex( diff --git a/src/backend/pubSubClient/actions/timeoutFriend.ts b/src/backend/pubSubClient/actions/timeoutFriend.ts index cb840b2..46beabc 100644 --- a/src/backend/pubSubClient/actions/timeoutFriend.ts +++ b/src/backend/pubSubClient/actions/timeoutFriend.ts @@ -1,42 +1,28 @@ -import { LOG_PREFIX } from ".."; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { getUsernameFromId } from "../../helpers/twitch"; import { timeout } from "../../chatClient/clientActions"; async function timeoutFriend( msg: RedemptionMessage -): Promise { +): Promise { const { message, channelId, userDisplayName } = msg; if (!msg.message) { - console.log(`${LOG_PREFIX}Redemption has no message`); - - return; + throw new Error("Redemption has no message"); } const channel = await getUsernameFromId(parseInt(channelId)); if (!channel) { - console.log(`${LOG_PREFIX}No channel found`); - - return; + throw new Error("No channel found"); } const time = 60; const reason = `Timeout dado por @${userDisplayName} con puntos del canal`; - try { - await timeout(channel, msg.message, time, reason); + await timeout(channel, msg.message, time, reason); - // eslint-disable-next-line require-atomic-updates - msg.message = `@${userDisplayName} ha expulsado a @${message} por ${time} segundos`; - } catch (e) { - // user can not be timed out - if (e instanceof Error) { - console.error(`${LOG_PREFIX} ${e.message}`); - } - - return; - } + // eslint-disable-next-line require-atomic-updates + msg.message = `@${userDisplayName} ha expulsado a @${message} por ${time} segundos`; return msg; } diff --git a/src/backend/pubSubClient/index.ts b/src/backend/pubSubClient/index.ts index d06b018..689b1f8 100644 --- a/src/backend/pubSubClient/index.ts +++ b/src/backend/pubSubClient/index.ts @@ -20,9 +20,7 @@ import { timeoutFriend } from "./actions/timeoutFriend"; const LOG_PREFIX = "[PubSub] "; -type RedemptionHandler = ( - msg: RedemptionMessage -) => Promise; +type RedemptionHandler = (msg: RedemptionMessage) => Promise; function getRedemptionHandlerFromRewardId(rewardId: string): RedemptionHandler { const noop = (message: RedemptionMessage): Promise => { @@ -81,6 +79,8 @@ async function onRedemption(message: PubSubRedemptionMessage) { } } + let completeOrCancelReward = cancelRewards; + if (typeof handledMessage !== "undefined") { const rewardEnumValues = Object.values(RedemptionIds); const rewardIdValueIndex = rewardEnumValues.indexOf( @@ -92,6 +92,10 @@ async function onRedemption(message: PubSubRedemptionMessage) { handledMessage.rewardId = rewardName; broadcast(JSON.stringify(handledMessage)); + + if (isProduction) { + completeOrCancelReward = completeRewards; + } } // TODO: improve this check @@ -99,13 +103,12 @@ async function onRedemption(message: PubSubRedemptionMessage) { // @ts-expect-error String is not assignable to... but all keys are strings if (keepInQueueRewards.includes(message.rewardId)) { + completeOrCancelReward = cancelRewards; + console.log(`${LOG_PREFIX}Reward kept in queue due to config`); return; } - const completeOrCancelReward = - handledMessage && isProduction ? completeRewards : cancelRewards; - if (message.rewardIsQueued) { try { await completeOrCancelReward(