♻️ Extract timeout friend logic to server
This commit is contained in:
44
src/backend/pubSubClient/actions/timeoutFriend.ts
Normal file
44
src/backend/pubSubClient/actions/timeoutFriend.ts
Normal 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 };
|
@@ -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!`;
|
||||
|
Reference in New Issue
Block a user