1
0

🎨 Improve handling of redemptions

This commit is contained in:
2022-01-05 21:53:58 +01:00
parent 5d20cce62f
commit 14584932ff
7 changed files with 68 additions and 51 deletions

View File

@@ -5,11 +5,13 @@ import { RedemptionMessage } from "../../../interfaces/RedemptionMessage";
import { getUsernameFromId } from "../../helpers/twitch";
// remove vip from a user to grant it to yourself
async function stealVip(msg: RedemptionMessage): Promise<boolean> {
async function stealVip(
msg: RedemptionMessage
): Promise<RedemptionMessage | undefined> {
if (!msg.message) {
console.log(`${LOG_PREFIX}Redemption has no message`);
return false;
return;
}
const channel = await getUsernameFromId(parseInt(msg.channelId));
@@ -17,7 +19,7 @@ async function stealVip(msg: RedemptionMessage): Promise<boolean> {
if (!channel) {
console.log(`${LOG_PREFIX}No channel found`);
return false;
return;
}
const addVipUser = msg.userDisplayName;
@@ -26,23 +28,32 @@ async function stealVip(msg: RedemptionMessage): Promise<boolean> {
if (!(await hasVip(channel, removeVipUser))) {
console.log(`${LOG_PREFIX}@${removeVipUser} is not VIP`);
return false;
return;
}
if (await hasVip(channel, addVipUser)) {
console.log(`${LOG_PREFIX}@${addVipUser} is already VIP`);
return false;
return;
}
const removed = await removeVip(channel, removeVipUser);
const added = await addVip(
channel,
addVipUser,
`@${addVipUser} ha "tomado prestado" el VIP de @${removeVipUser}`
);
return removed && added;
if (!removed) {
return;
}
const added = await addVip(channel, addVipUser);
if (!added) {
await addVip(channel, removeVipUser);
return;
}
msg.message = `@${addVipUser} ha "tomado prestado" el VIP de @${removeVipUser}`;
return msg;
}
export { stealVip };