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

@@ -56,10 +56,6 @@ async function checkEvent(e) {
case "a73247ee-e33e-4e9b-9105-bd9d11e111fc":
await russianRoulette(data);
break;
// timeout a un amigo
case "638c642d-23d8-4264-9702-e77eeba134de":
await timeoutFriend(data);
break;
// highlight message
case "a26c0d9e-fd2c-4943-bc94-c5c2f2c974e4":
await highlightMessage(data);
@@ -192,25 +188,6 @@ function russianRoulette({ userDisplayName, message }) {
});
}
async function timeoutFriend(data) {
const senderUser = data.userDisplayName;
const receptorUser = data.message.split(" ")[0];
sendWsActions({
type: "timeout",
userId: data.userId,
channelId: data.channelId,
data: {
username: receptorUser,
time: "60",
reason: `Timeout dado por @${senderUser} con puntos del canal.`
}
});
const cardMessage = `@${senderUser} ha expulsado a @${receptorUser} por 60 segundos.`;
await createCard(data.rewardName, cardMessage, data.backgroundColor, data.rewardImage);
}
async function highlightMessage(data) {
const urlRegex = /(https?:\/\/)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;

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!`;