1
0

♻️ Extract timeout friend logic to server

This commit is contained in:
2022-01-05 19:37:54 +01:00
parent 324f3faec2
commit 1b48261b94
3 changed files with 46 additions and 23 deletions

View File

@@ -0,0 +1,44 @@
import { LOG_PREFIX } from "..";
import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { broadcast } from "../../helpers/webServer";
import { getUsernameFromId } from "../../helpers/twitch";
import { timeout } from "../../chatClient/clientActions";
async function timeoutFriend(msg: RedemptionMessage): Promise<boolean> {
const { message, channelId, userDisplayName } = msg;
if (!msg.message) {
console.log(`${LOG_PREFIX}Redemption has no message`);
return false;
}
const channel = await getUsernameFromId(parseInt(channelId));
if (!channel) {
console.log(`${LOG_PREFIX}No channel found`);
return false;
}
const time = 60;
const reason = `Timeout dado por @${userDisplayName} con puntos del canal`;
try {
await timeout(channel, msg.message, time, reason);
msg.message = `@${userDisplayName} ha expulsado a @${message} por ${time} segundos`;
broadcast(JSON.stringify(msg));
} catch (e) {
// user can not be timed out
if (e instanceof Error) {
console.error(`${LOG_PREFIX} ${e.message}`);
}
return false;
}
return true;
}
export { timeoutFriend };

View File

@@ -8,6 +8,7 @@ import { getApiClient } from "../helpers/twitch";
import { getVip } from "./actions/getVip";
import { russianRoulette } from "./actions/russianRoulette";
import { stealVip } from "./actions/stealVip";
import { timeoutFriend } from "./actions/timeoutFriend";
const LOG_PREFIX = "[PubSub] ";
@@ -48,6 +49,7 @@ async function onRedemption(message: PubSubRedemptionMessage) {
await russianRoulette(msg);
break;
case RedemptionIds.TimeoutFriend:
await timeoutFriend(msg);
break;
case RedemptionIds.GetVip:
msg.message = `@${msg.userDisplayName} ha encontrado diamantes!`;