1
0

♻️ 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:
2022-01-05 18:13:59 +01:00
parent bc401bd531
commit 05a93ba316
13 changed files with 162 additions and 92 deletions

View File

@@ -1,18 +1,17 @@
import { chatClient, say } from "../../chatClient";
import {
saveScheduledActions,
scheduledActions
} from "../../helpers/scheduledActions";
import { addVip, hasVip, removeVip } from "../../chatClient/clientActions";
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: {
channelId: string;
userDisplayName: string;
message: string;
}): Promise<boolean> {
async function stealVip(msg: RedemptionMessage): Promise<boolean> {
if (!msg.message) {
console.log(`${LOG_PREFIX}Redemption has no message`);
return false;
}
const channel = await getUsernameFromId(parseInt(msg.channelId));
if (!channel) {
@@ -24,60 +23,26 @@ async function stealVip(msg: {
const addVipUser = msg.userDisplayName;
const removeVipUser = msg.message;
if (await hasVip(channel, removeVipUser)) {
await removeVip(channel, removeVipUser);
await addVip(channel, addVipUser);
if (!(await hasVip(channel, removeVipUser))) {
console.log(`${LOG_PREFIX}@${removeVipUser} is not VIP`);
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;
}
const vips = await chatClient.getVips(channel);
return vips.includes(username);
}
if (await hasVip(channel, addVipUser)) {
console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`);
// removes a user from vips
async function removeVip(
channel: string,
username: string,
message?: string
): Promise<void> {
if (!message) {
message = `Se ha acabado el chollo, VIP de @${username} eliminado.`;
return false;
}
await chatClient.removeVip(channel, username);
say(channel, message);
const removed = await removeVip(channel, removeVipUser);
const added = await addVip(
channel,
addVipUser,
`@${addVipUser} ha "tomado prestado" el VIP de @${removeVipUser}`
);
return removed && added;
}
export { stealVip };