♻️ Cleaned lots of code and change getvip implementation
getVip is no longer time limited, it will remove it once a limit of vip users is reached (to be implemented)
This commit is contained in:
@@ -1,18 +1,22 @@
|
|||||||
import { chatClient } from "../..";
|
import { chatClient } from "../..";
|
||||||
import { say } from "..";
|
import { say } from "..";
|
||||||
|
|
||||||
// adds a user to vips
|
|
||||||
async function addVip(
|
async function addVip(
|
||||||
channel: string,
|
channel: string,
|
||||||
username: string,
|
username: string,
|
||||||
message?: string
|
message?: string
|
||||||
): Promise<void> {
|
): Promise<boolean> {
|
||||||
if (!message) {
|
try {
|
||||||
message = `Otorgado VIP a @${username}.`;
|
await chatClient.addVip(channel, username);
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
await chatClient.addVip(channel, username);
|
if (message) {
|
||||||
say(channel, message);
|
await say(channel, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { addVip };
|
export { addVip };
|
||||||
|
27
src/backend/chatClient/clientActions/hasVip/index.ts
Normal file
27
src/backend/chatClient/clientActions/hasVip/index.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { chatClient } from "../..";
|
||||||
|
|
||||||
|
type CacheType = Record<string, Array<string>>;
|
||||||
|
const cache: CacheType = {};
|
||||||
|
|
||||||
|
async function hasVip(channel: string, username: string): Promise<boolean> {
|
||||||
|
if (!username) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cache[channel]) {
|
||||||
|
const vips = await chatClient.getVips(channel);
|
||||||
|
|
||||||
|
// * last VIP has a "." at the end of the username (bug on library?)
|
||||||
|
cache[channel] = vips.map(vip => vip.replace(/\.$/, ""));
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
delete cache[channel];
|
||||||
|
}, 2500);
|
||||||
|
}
|
||||||
|
|
||||||
|
const vips = cache[channel];
|
||||||
|
|
||||||
|
return vips.includes(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
export { hasVip };
|
@@ -1,6 +1,7 @@
|
|||||||
import { addVip } from "./addVip";
|
import { addVip } from "./addVip";
|
||||||
|
import { hasVip } from "./hasVip";
|
||||||
import { removeVip } from "./removeVip";
|
import { removeVip } from "./removeVip";
|
||||||
import { say } from "./say";
|
import { say } from "./say";
|
||||||
import { timeout } from "./timeout";
|
import { timeout } from "./timeout";
|
||||||
|
|
||||||
export { say, timeout, addVip, removeVip };
|
export { say, timeout, addVip, removeVip, hasVip };
|
||||||
|
@@ -1,18 +1,22 @@
|
|||||||
import { chatClient } from "../..";
|
import { chatClient } from "../..";
|
||||||
import { say } from "..";
|
import { say } from "..";
|
||||||
|
|
||||||
// removes a user from vips
|
|
||||||
async function removeVip(
|
async function removeVip(
|
||||||
channel: string,
|
channel: string,
|
||||||
username: string,
|
username: string,
|
||||||
message?: string
|
message?: string
|
||||||
): Promise<void> {
|
): Promise<boolean> {
|
||||||
if (!message) {
|
try {
|
||||||
message = `VIP de @${username} eliminado.`;
|
await chatClient.removeVip(channel, username);
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
await chatClient.removeVip(channel, username);
|
if (message) {
|
||||||
say(channel, message);
|
await say(channel, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { removeVip };
|
export { removeVip };
|
||||||
|
@@ -2,6 +2,7 @@ import { addVip, removeVip, say, timeout } from "./clientActions";
|
|||||||
import { getAuthProvider, getUsernameFromId } from "../helpers/twitch";
|
import { getAuthProvider, getUsernameFromId } from "../helpers/twitch";
|
||||||
|
|
||||||
import { Action } from "../../interfaces/actions/Action";
|
import { Action } from "../../interfaces/actions/Action";
|
||||||
|
import { ActionType } from "../../enums/ActionType";
|
||||||
import { ChatClient } from "twitch-chat-client";
|
import { ChatClient } from "twitch-chat-client";
|
||||||
import { broadcast } from "../helpers/webServer";
|
import { broadcast } from "../helpers/webServer";
|
||||||
import { start } from "../helpers/scheduledActions";
|
import { start } from "../helpers/scheduledActions";
|
||||||
@@ -52,20 +53,20 @@ async function handleClientAction(action: Action): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case "say":
|
case ActionType.Say:
|
||||||
say(channel, action.data.message);
|
say(channel, action.data.message);
|
||||||
break;
|
break;
|
||||||
case "timeout":
|
case ActionType.Timeout:
|
||||||
await timeout(channel, username, action.data.time, action.data.reason);
|
await timeout(channel, username, action.data.time, action.data.reason);
|
||||||
break;
|
break;
|
||||||
case "broadcast":
|
case ActionType.Broadcast:
|
||||||
broadcast(action.data.message);
|
broadcast(action.data.message);
|
||||||
break;
|
break;
|
||||||
case "addVip":
|
case ActionType.AddVip:
|
||||||
await addVip(channel, username);
|
await addVip(channel, username, `Otorgado VIP a @${username}`);
|
||||||
break;
|
break;
|
||||||
case "removeVip":
|
case ActionType.RemoveVip:
|
||||||
await removeVip(channel, username);
|
await removeVip(channel, username, `Eliminado VIP de @${username}`);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log(`${[LOG_PREFIX]}Couldn't handle action:`, action);
|
console.log(`${[LOG_PREFIX]}Couldn't handle action:`, action);
|
||||||
|
29
src/backend/pubSubClient/actions/getVip.ts
Normal file
29
src/backend/pubSubClient/actions/getVip.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { addVip, hasVip } from "../../chatClient/clientActions";
|
||||||
|
|
||||||
|
import { LOG_PREFIX } from "..";
|
||||||
|
import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
|
||||||
|
import { getUsernameFromId } from "../../helpers/twitch";
|
||||||
|
|
||||||
|
async function getVip(msg: RedemptionMessage): Promise<boolean> {
|
||||||
|
const channel = await getUsernameFromId(parseInt(msg.channelId));
|
||||||
|
|
||||||
|
if (!channel) {
|
||||||
|
console.log(`${LOG_PREFIX}No channel found`);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const addVipUser = msg.userDisplayName;
|
||||||
|
|
||||||
|
if (await hasVip(channel, addVipUser)) {
|
||||||
|
console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const addedVip = await addVip(channel, addVipUser, msg.message);
|
||||||
|
|
||||||
|
return addedVip;
|
||||||
|
}
|
||||||
|
|
||||||
|
export { getVip };
|
@@ -1,18 +1,17 @@
|
|||||||
import { chatClient, say } from "../../chatClient";
|
import { addVip, hasVip, removeVip } from "../../chatClient/clientActions";
|
||||||
import {
|
|
||||||
saveScheduledActions,
|
|
||||||
scheduledActions
|
|
||||||
} from "../../helpers/scheduledActions";
|
|
||||||
|
|
||||||
import { LOG_PREFIX } from "..";
|
import { LOG_PREFIX } from "..";
|
||||||
|
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
|
// remove vip from a user to grant it to yourself
|
||||||
async function stealVip(msg: {
|
async function stealVip(msg: RedemptionMessage): Promise<boolean> {
|
||||||
channelId: string;
|
if (!msg.message) {
|
||||||
userDisplayName: string;
|
console.log(`${LOG_PREFIX}Redemption has no message`);
|
||||||
message: string;
|
|
||||||
}): Promise<boolean> {
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const channel = await getUsernameFromId(parseInt(msg.channelId));
|
const channel = await getUsernameFromId(parseInt(msg.channelId));
|
||||||
|
|
||||||
if (!channel) {
|
if (!channel) {
|
||||||
@@ -24,60 +23,26 @@ async function stealVip(msg: {
|
|||||||
const addVipUser = msg.userDisplayName;
|
const addVipUser = msg.userDisplayName;
|
||||||
const removeVipUser = msg.message;
|
const removeVipUser = msg.message;
|
||||||
|
|
||||||
if (await hasVip(channel, removeVipUser)) {
|
if (!(await hasVip(channel, removeVipUser))) {
|
||||||
await removeVip(channel, removeVipUser);
|
console.log(`${LOG_PREFIX}@${removeVipUser} is not VIP`);
|
||||||
await addVip(channel, addVipUser);
|
|
||||||
|
|
||||||
const scheduledRemoveVipIndex = scheduledActions.findIndex(
|
|
||||||
s => s.action === "removeVip" && s.username === removeVipUser
|
|
||||||
);
|
|
||||||
|
|
||||||
if (scheduledRemoveVipIndex > -1) {
|
|
||||||
scheduledActions[scheduledRemoveVipIndex].username = addVipUser;
|
|
||||||
saveScheduledActions();
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// adds a user to vips
|
|
||||||
async function addVip(
|
|
||||||
channel: string,
|
|
||||||
username: string,
|
|
||||||
message?: string
|
|
||||||
): Promise<void> {
|
|
||||||
if (!message) {
|
|
||||||
message = `Otorgado VIP a @${username}.`;
|
|
||||||
}
|
|
||||||
|
|
||||||
await chatClient.addVip(channel, username);
|
|
||||||
say(channel, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function hasVip(channel: string, username: string): Promise<boolean> {
|
|
||||||
if (!username) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const vips = await chatClient.getVips(channel);
|
if (await hasVip(channel, addVipUser)) {
|
||||||
return vips.includes(username);
|
console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`);
|
||||||
}
|
|
||||||
|
|
||||||
// removes a user from vips
|
return false;
|
||||||
async function removeVip(
|
|
||||||
channel: string,
|
|
||||||
username: string,
|
|
||||||
message?: string
|
|
||||||
): Promise<void> {
|
|
||||||
if (!message) {
|
|
||||||
message = `Se ha acabado el chollo, VIP de @${username} eliminado.`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await chatClient.removeVip(channel, username);
|
const removed = await removeVip(channel, removeVipUser);
|
||||||
say(channel, message);
|
const added = await addVip(
|
||||||
|
channel,
|
||||||
|
addVipUser,
|
||||||
|
`@${addVipUser} ha "tomado prestado" el VIP de @${removeVipUser}`
|
||||||
|
);
|
||||||
|
|
||||||
|
return removed && added;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { stealVip };
|
export { stealVip };
|
||||||
|
@@ -1,8 +1,11 @@
|
|||||||
import { PubSubClient, PubSubRedemptionMessage } from "twitch-pubsub-client";
|
import { PubSubClient, PubSubRedemptionMessage } from "twitch-pubsub-client";
|
||||||
|
|
||||||
|
import { RedemptionIds } from "../../enums/Redemptions";
|
||||||
|
import { RedemptionMessage } from "../../interfaces/RedemptionMessage";
|
||||||
import { UserIdResolvable } from "twitch";
|
import { UserIdResolvable } from "twitch";
|
||||||
import { broadcast } from "../helpers/webServer";
|
import { broadcast } from "../helpers/webServer";
|
||||||
import { getApiClient } from "../helpers/twitch";
|
import { getApiClient } from "../helpers/twitch";
|
||||||
|
import { getVip } from "./actions/getVip";
|
||||||
import { stealVip } from "./actions/stealVip";
|
import { stealVip } from "./actions/stealVip";
|
||||||
|
|
||||||
const LOG_PREFIX = "[PubSub] ";
|
const LOG_PREFIX = "[PubSub] ";
|
||||||
@@ -24,7 +27,7 @@ async function onRedemption(message: PubSubRedemptionMessage) {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const reward = message._data.data.redemption.reward;
|
const reward = message._data.data.redemption.reward;
|
||||||
|
|
||||||
const msg = {
|
const msg: RedemptionMessage = {
|
||||||
id: message.id,
|
id: message.id,
|
||||||
channelId: message.channelId,
|
channelId: message.channelId,
|
||||||
rewardId: message.rewardId,
|
rewardId: message.rewardId,
|
||||||
@@ -40,14 +43,20 @@ async function onRedemption(message: PubSubRedemptionMessage) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
switch (msg.rewardId) {
|
switch (msg.rewardId) {
|
||||||
// robar vip
|
case RedemptionIds.StealVip:
|
||||||
case "ac750bd6-fb4c-4259-b06d-56953601243b":
|
|
||||||
if (await stealVip(msg)) {
|
if (await stealVip(msg)) {
|
||||||
msg.message = `@${msg.userDisplayName} ha robado el VIP a @${msg.message}.`;
|
msg.message = `@${msg.userDisplayName} ha "tomado prestado" el VIP de @${msg.message}`;
|
||||||
|
|
||||||
broadcast(JSON.stringify(msg));
|
broadcast(JSON.stringify(msg));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case RedemptionIds.GetVip:
|
||||||
|
msg.message = `@${msg.userDisplayName} ha encontrado diamantes!`;
|
||||||
|
|
||||||
|
if (await getVip(msg)) {
|
||||||
|
broadcast(JSON.stringify(msg));
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
console.log(LOG_PREFIX, msg);
|
console.log(LOG_PREFIX, msg);
|
||||||
|
|
||||||
|
7
src/enums/ActionType.ts
Normal file
7
src/enums/ActionType.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export enum ActionType {
|
||||||
|
Say = "say",
|
||||||
|
Timeout = "timeout",
|
||||||
|
Broadcast = "broadcast",
|
||||||
|
AddVip = "addVip",
|
||||||
|
RemoveVip = "removeVip"
|
||||||
|
}
|
9
src/enums/Redemptions.ts
Normal file
9
src/enums/Redemptions.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
export enum RedemptionIds {
|
||||||
|
KaraokeTime = "27faa7e4-f496-4e91-92ae-a51f99b9e854",
|
||||||
|
RussianRoulette = "a73247ee-e33e-4e9b-9105-bd9d11e111fc",
|
||||||
|
TimeoutFriend = "638c642d-23d8-4264-9702-e77eeba134de",
|
||||||
|
HighlightMessage = "a26c0d9e-fd2c-4943-bc94-c5c2f2c974e4",
|
||||||
|
GetVip = "a215d6a0-2c11-4503-bb29-1ca98ef046ac",
|
||||||
|
StealVip = "ac750bd6-fb4c-4259-b06d-56953601243b",
|
||||||
|
Hidrate = "232e951f-93d1-4138-a0e3-9e822b4852e0"
|
||||||
|
}
|
11
src/interfaces/RedemptionMessage.ts
Normal file
11
src/interfaces/RedemptionMessage.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
export interface RedemptionMessage {
|
||||||
|
id: string;
|
||||||
|
channelId: string;
|
||||||
|
rewardId: string;
|
||||||
|
rewardName: string;
|
||||||
|
rewardImage: string;
|
||||||
|
message?: string;
|
||||||
|
userId: string;
|
||||||
|
userDisplayName: string;
|
||||||
|
backgroundColor: string;
|
||||||
|
}
|
@@ -1,7 +1,9 @@
|
|||||||
|
import { ActionType } from "../../enums/ActionType";
|
||||||
|
|
||||||
export interface Action {
|
export interface Action {
|
||||||
type: string;
|
type: ActionType;
|
||||||
channelId: string;
|
channelId: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
scheduledAt?: number;
|
scheduledAt?: number;
|
||||||
data: any;
|
data: any;
|
||||||
}
|
}
|
@@ -1,8 +1,9 @@
|
|||||||
import { Action } from "./Action";
|
import { Action } from "./Action";
|
||||||
|
import { ActionType } from "../../enums/ActionType";
|
||||||
|
|
||||||
export interface SayAction extends Action {
|
export interface SayAction extends Action {
|
||||||
type: "say";
|
type: ActionType.Say;
|
||||||
data: {
|
data: {
|
||||||
message: "string";
|
message: string;
|
||||||
}
|
};
|
||||||
}
|
}
|
Reference in New Issue
Block a user