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,30 +1,23 @@
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<RedemptionMessage | undefined> {
async function getVip(msg: RedemptionMessage): Promise<RedemptionMessage> {
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;
if (await hasVip(channel, addVipUser)) {
console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`);
return;
throw new Error(`@${addVipUser} is already VIP`);
}
const users = vipUsers[channelId];

View File

@@ -1,17 +1,12 @@
import { LOG_PREFIX } from "..";
import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { getUsernameFromId } from "../../helpers/twitch";
import { say } from "../../chatClient";
async function hidrate(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
async function hidrate(msg: RedemptionMessage): Promise<RedemptionMessage> {
const channel = await getUsernameFromId(parseInt(msg.channelId));
if (!channel) {
console.log(`${LOG_PREFIX}No channel found`);
return;
throw new Error("No channel found");
}
msg.message = `@${msg.userDisplayName} ha invitado a una ronda`;

View File

@@ -5,11 +5,9 @@ import { timeout } from "../../chatClient/clientActions";
async function highlightMessage(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
): Promise<RedemptionMessage> {
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;

View File

@@ -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<RedemptionMessage | undefined> {
): Promise<RedemptionMessage> {
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,7 +38,6 @@ 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");
@@ -52,11 +48,6 @@ async function russianRoulette(
} else {
await say(channel, `rdCool Clap ${userDisplayName}`);
}
} catch (e) {
if (e instanceof Error) {
console.log(`${LOG_PREFIX}${e.message}`);
}
}
return msg;
}

View File

@@ -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<RedemptionMessage | undefined> {
async function stealVip(msg: RedemptionMessage): Promise<RedemptionMessage> {
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
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(

View File

@@ -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<RedemptionMessage | undefined> {
): Promise<RedemptionMessage> {
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);
// 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;
}
return msg;
}

View File

@@ -20,9 +20,7 @@ import { timeoutFriend } from "./actions/timeoutFriend";
const LOG_PREFIX = "[PubSub] ";
type RedemptionHandler = (
msg: RedemptionMessage
) => Promise<RedemptionMessage | undefined>;
type RedemptionHandler = (msg: RedemptionMessage) => Promise<RedemptionMessage>;
function getRedemptionHandlerFromRewardId(rewardId: string): RedemptionHandler {
const noop = (message: RedemptionMessage): Promise<RedemptionMessage> => {
@@ -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(