1
0

♻️ Throw error in actions in favour of undefined returns

This commit is contained in:
2022-01-07 12:42:32 +01:00
parent 916d430a4d
commit 6d95a754cc
7 changed files with 73 additions and 117 deletions

View File

@@ -1,48 +1,41 @@
import { addVip, hasVip, removeVip } from "../../chatClient/clientActions"; import { addVip, hasVip, removeVip } from "../../chatClient/clientActions";
import { save, vipUsers } from "../../helpers/miniDb"; import { save, vipUsers } from "../../helpers/miniDb";
import { LOG_PREFIX } from "..";
import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { getUsernameFromId } from "../../helpers/twitch"; import { getUsernameFromId } from "../../helpers/twitch";
const MAX_VIPS = 3; const MAX_VIPS = 3;
async function getVip( async function getVip(msg: RedemptionMessage): Promise<RedemptionMessage> {
msg: RedemptionMessage const channelId = parseInt(msg.channelId);
): Promise<RedemptionMessage | undefined> { const channel = await getUsernameFromId(channelId);
const channelId = parseInt(msg.channelId);
const channel = await getUsernameFromId(channelId);
if (!channel) { if (!channel) {
console.log(`${LOG_PREFIX}No channel found`); 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)) { const users = vipUsers[channelId];
console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`);
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) { await addVip(channel, addVipUser);
const user = users.shift();
if (user) { users.push(addVipUser);
await removeVip(channel, user); save();
}
}
await addVip(channel, addVipUser); return msg;
users.push(addVipUser);
save();
return msg;
} }
export { getVip }; export { getVip };

View File

@@ -1,24 +1,19 @@
import { LOG_PREFIX } from "..";
import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { getUsernameFromId } from "../../helpers/twitch"; import { getUsernameFromId } from "../../helpers/twitch";
import { say } from "../../chatClient"; import { say } from "../../chatClient";
async function hidrate( async function hidrate(msg: RedemptionMessage): Promise<RedemptionMessage> {
msg: RedemptionMessage const channel = await getUsernameFromId(parseInt(msg.channelId));
): Promise<RedemptionMessage | undefined> {
const channel = await getUsernameFromId(parseInt(msg.channelId));
if (!channel) { if (!channel) {
console.log(`${LOG_PREFIX}No channel found`); 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 }; export { hidrate };

View File

@@ -5,11 +5,9 @@ import { timeout } from "../../chatClient/clientActions";
async function highlightMessage( async function highlightMessage(
msg: RedemptionMessage msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> { ): Promise<RedemptionMessage> {
if (!msg.message) { if (!msg.message) {
console.log(`${LOG_PREFIX}Redemption has no message`); throw new Error("Redemption has no message");
return;
} }
const urlRegex = const urlRegex =
@@ -20,9 +18,7 @@ async function highlightMessage(
const channel = await getUsernameFromId(parseInt(msg.channelId)); const channel = await getUsernameFromId(parseInt(msg.channelId));
if (!channel) { if (!channel) {
console.log(`${LOG_PREFIX}No channel found`); throw new Error("No channel found");
return;
} }
try { try {
@@ -34,7 +30,7 @@ async function highlightMessage(
// user probably cannot be timed out // user probably cannot be timed out
} }
return; throw new Error("The message cannot contain a url");
} }
return msg; return msg;

View File

@@ -1,6 +1,5 @@
import { say, timeout } from "../../chatClient/clientActions"; import { say, timeout } from "../../chatClient/clientActions";
import { LOG_PREFIX } from "..";
import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { getUsernameFromId } from "../../helpers/twitch"; import { getUsernameFromId } from "../../helpers/twitch";
import { randomInt } from "crypto"; import { randomInt } from "crypto";
@@ -13,14 +12,12 @@ const maxSafeShots = 5;
async function russianRoulette( async function russianRoulette(
msg: RedemptionMessage msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> { ): Promise<RedemptionMessage> {
const { channelId, userDisplayName } = msg; const { channelId, userDisplayName } = msg;
const channel = await getUsernameFromId(parseInt(channelId)); const channel = await getUsernameFromId(parseInt(channelId));
if (!channel) { if (!channel) {
console.log(`${LOG_PREFIX}No channel found`); throw new Error("No channel found");
return;
} }
if (!gunsSafeShots[channelId]) { if (!gunsSafeShots[channelId]) {
@@ -41,21 +38,15 @@ async function russianRoulette(
// eslint-disable-next-line require-atomic-updates // eslint-disable-next-line require-atomic-updates
msg.message = win ? "" : "got shot"; msg.message = win ? "" : "got shot";
try { if (!win) {
if (!win) { await timeout(channel, userDisplayName, timeoutSeconds, "F en la ruleta");
await timeout(channel, userDisplayName, timeoutSeconds, "F en la ruleta");
await say( await say(
channel, channel,
`PepeHands ${userDisplayName} no ha sobrevivido para contarlo` `PepeHands ${userDisplayName} no ha sobrevivido para contarlo`
); );
} else { } else {
await say(channel, `rdCool Clap ${userDisplayName}`); await say(channel, `rdCool Clap ${userDisplayName}`);
}
} catch (e) {
if (e instanceof Error) {
console.log(`${LOG_PREFIX}${e.message}`);
}
} }
return msg; return msg;

View File

@@ -1,27 +1,19 @@
import { addVip, hasVip, removeVip, say } from "../../chatClient/clientActions"; import { addVip, hasVip, removeVip, say } from "../../chatClient/clientActions";
import { save, vipUsers } from "../../helpers/miniDb"; import { save, vipUsers } from "../../helpers/miniDb";
import { LOG_PREFIX } from "..";
import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { getUsernameFromId } from "../../helpers/twitch"; import { getUsernameFromId } from "../../helpers/twitch";
// remove vip from a user to grant it to yourself async function stealVip(msg: RedemptionMessage): Promise<RedemptionMessage> {
async function stealVip(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
if (!msg.message) { if (!msg.message) {
console.log(`${LOG_PREFIX}Redemption has no message`); throw new Error("Redemption has no message");
return;
} }
const channelId = parseInt(msg.channelId); const channelId = parseInt(msg.channelId);
const channel = await getUsernameFromId(channelId); const channel = await getUsernameFromId(channelId);
if (!channel) { if (!channel) {
console.log(`${LOG_PREFIX}No channel found`); throw new Error("No channel found");
return;
} }
const addVipUser = msg.userDisplayName; const addVipUser = msg.userDisplayName;
@@ -29,27 +21,27 @@ async function stealVip(
const channelVips = vipUsers[channelId]; const channelVips = vipUsers[channelId];
if (!channelVips.find((u) => u.toLowerCase() === removeVipUser)) { if (!channelVips.find((u) => u.toLowerCase() === removeVipUser)) {
const message = const noVips = 0;
// eslint-disable-next-line no-magic-numbers const hasVips = channelVips.length === noVips;
channelVips.length === 0
? "No hay nadie a quien puedas robar el VIP" const message = !hasVips
: `Solo puedes robar el VIP de: "${channelVips.sort().join('", "')}"`; ? "No hay nadie a quien puedas robar el VIP"
: `Solo puedes robar el VIP de: "${channelVips.sort().join('", "')}"`;
await say(channel, message); await say(channel, message);
return; if (!hasVips) {
throw new Error("No VIP users to steal from");
}
} }
if (channelVips.includes(addVipUser) || (await hasVip(channel, addVipUser))) { if (channelVips.includes(addVipUser) || (await hasVip(channel, addVipUser))) {
console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`); throw new Error(`@${addVipUser} is already VIP`);
return;
} }
const removed = await removeVip(channel, removeVipUser); const removed = await removeVip(channel, removeVipUser);
if (!removed && (await hasVip(channel, removeVipUser))) { if (!removed && (await hasVip(channel, removeVipUser))) {
console.log(`${LOG_PREFIX}Could not remove VIP of @${removeVipUser}`); throw new Error(`Could not remove VIP of @${removeVipUser}`);
return;
} }
const added = await addVip(channel, addVipUser); const added = await addVip(channel, addVipUser);
@@ -57,7 +49,7 @@ async function stealVip(
if (!added) { if (!added) {
await addVip(channel, removeVipUser); await addVip(channel, removeVipUser);
return; throw new Error(`Could not add VIP to ${addVipUser}`);
} }
const removeIdx = channelVips.findIndex( const removeIdx = channelVips.findIndex(

View File

@@ -1,42 +1,28 @@
import { LOG_PREFIX } from "..";
import { RedemptionMessage } from "../../../interfaces/RedemptionMessage"; import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { getUsernameFromId } from "../../helpers/twitch"; import { getUsernameFromId } from "../../helpers/twitch";
import { timeout } from "../../chatClient/clientActions"; import { timeout } from "../../chatClient/clientActions";
async function timeoutFriend( async function timeoutFriend(
msg: RedemptionMessage msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> { ): Promise<RedemptionMessage> {
const { message, channelId, userDisplayName } = msg; const { message, channelId, userDisplayName } = msg;
if (!msg.message) { if (!msg.message) {
console.log(`${LOG_PREFIX}Redemption has no message`); throw new Error("Redemption has no message");
return;
} }
const channel = await getUsernameFromId(parseInt(channelId)); const channel = await getUsernameFromId(parseInt(channelId));
if (!channel) { if (!channel) {
console.log(`${LOG_PREFIX}No channel found`); throw new Error("No channel found");
return;
} }
const time = 60; const time = 60;
const reason = `Timeout dado por @${userDisplayName} con puntos del canal`; 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 // eslint-disable-next-line require-atomic-updates
msg.message = `@${userDisplayName} ha expulsado a @${message} por ${time} segundos`; 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;
}
return msg; return msg;
} }

View File

@@ -20,9 +20,7 @@ import { timeoutFriend } from "./actions/timeoutFriend";
const LOG_PREFIX = "[PubSub] "; const LOG_PREFIX = "[PubSub] ";
type RedemptionHandler = ( type RedemptionHandler = (msg: RedemptionMessage) => Promise<RedemptionMessage>;
msg: RedemptionMessage
) => Promise<RedemptionMessage | undefined>;
function getRedemptionHandlerFromRewardId(rewardId: string): RedemptionHandler { function getRedemptionHandlerFromRewardId(rewardId: string): RedemptionHandler {
const noop = (message: RedemptionMessage): Promise<RedemptionMessage> => { const noop = (message: RedemptionMessage): Promise<RedemptionMessage> => {
@@ -81,6 +79,8 @@ async function onRedemption(message: PubSubRedemptionMessage) {
} }
} }
let completeOrCancelReward = cancelRewards;
if (typeof handledMessage !== "undefined") { if (typeof handledMessage !== "undefined") {
const rewardEnumValues = Object.values(RedemptionIds); const rewardEnumValues = Object.values(RedemptionIds);
const rewardIdValueIndex = rewardEnumValues.indexOf( const rewardIdValueIndex = rewardEnumValues.indexOf(
@@ -92,6 +92,10 @@ async function onRedemption(message: PubSubRedemptionMessage) {
handledMessage.rewardId = rewardName; handledMessage.rewardId = rewardName;
broadcast(JSON.stringify(handledMessage)); broadcast(JSON.stringify(handledMessage));
if (isProduction) {
completeOrCancelReward = completeRewards;
}
} }
// TODO: improve this check // 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 // @ts-expect-error String is not assignable to... but all keys are strings
if (keepInQueueRewards.includes(message.rewardId)) { if (keepInQueueRewards.includes(message.rewardId)) {
completeOrCancelReward = cancelRewards;
console.log(`${LOG_PREFIX}Reward kept in queue due to config`); console.log(`${LOG_PREFIX}Reward kept in queue due to config`);
return; return;
} }
const completeOrCancelReward =
handledMessage && isProduction ? completeRewards : cancelRewards;
if (message.rewardIsQueued) { if (message.rewardIsQueued) {
try { try {
await completeOrCancelReward( await completeOrCancelReward(