Skip to content

Commit

Permalink
feat: Conditional events (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 committed Feb 28, 2021
1 parent 66e5ed7 commit 0385b3a
Show file tree
Hide file tree
Showing 48 changed files with 870 additions and 76 deletions.
1 change: 1 addition & 0 deletions app/package.json
Expand Up @@ -24,6 +24,7 @@
"human-id": "^2.0.1",
"i18next": "^19.8.3",
"i18next-fs-backend": "^1.0.7",
"json-logic-js": "^2.0.0",
"jszip": "^3.6.0",
"mime": "^2.4.6",
"ms": "^2.1.3",
Expand Down
14 changes: 4 additions & 10 deletions app/server/api/twitch.js
@@ -1,9 +1,10 @@
const { events } = require("../../stores");
const loggers = require("../libs/loggers");
const settings = require("../libs/settings");
const state = require("../libs/twitch/state");
const twitchLogin = require("../libs/twitch/login");
const chatJoin = require("../libs/twitch/chat/join");
const setEvent = require("../libs/twitch/api/setEvent");
const getEvents = require("../libs/twitch/api/getEvents");
const chatConnect = require("../libs/twitch/chat/connect");
const addCommand = require("../libs/twitch/api/addCommand");
const updateReward = require("../libs/twitch/api/updateReward");
Expand Down Expand Up @@ -53,15 +54,8 @@ module.exports = {
return result;
});
},
getEvents: () => events.get("events"),
setEvent: (event) => {
events.set(
"events",
events.get("events").map((e) => {
return e.name === event.name ? { ...e, ...event } : e;
})
);
},
getEvents: () => getEvents(),
setEvent: (event) => setEvent(event),
getRewardList: () => getRewardList(),
getCommandList: () => getCommandList(),
getCommandNames: () => getCommandNames(),
Expand Down
37 changes: 37 additions & 0 deletions app/server/libs/twitch/api/getEvents.js
@@ -0,0 +1,37 @@
const { events } = require("../../../../stores");
const api = require("./getUserInfoVars");

const eventsWithUserVars = [
"onCommand",
"onBits",
"onRedemption",
"onAction",
"onBitsBadgeUpgrade",
"onCommunityPayForward",
"onCommunitySub",
"onGiftPaidUpgrade",
"onMessage",
"onPrimeCommunityGift",
"onPrimePaidUpgrade",
"onRaid",
"onResub",
"onRewardGift",
"onRitual",
"onStandardPayForward",
"onSub",
"onSubExtend",
"onSubGift",
];

module.exports = function getEvents() {
const userVars = api.getDefaultVars();
return events.get("events").map((event) => {
if (eventsWithUserVars.includes(event.name)) {
event.tags = {
...event.tags,
...userVars,
};
}
return event;
});
};
60 changes: 60 additions & 0 deletions app/server/libs/twitch/api/getUserInfoVars.js
@@ -0,0 +1,60 @@
const getConnectedUser = require("./getConnectedUser");
const twitch = require("../index");

function getDefaultVars() {
return {
isBroadcaster: "0",
isMod: "0",
isVip: "0",
isSubscriber: "0",
// timestamp: Date.now(),
};
}

function getChatUserInfoVars(data) {
const { isBroadcaster, isMod, isSubscriber, isVip } = data.userInfo;

return {
...getDefaultVars(),
isBroadcaster,
isMod,
isVip,
isSubscriber,
};
}

async function getPubSubUserInfoVars(user) {
const broadcaster = await getConnectedUser();
const isBroadcaster = broadcaster.login === user.login;

const isSubscriber =
isBroadcaster ||
!!(await twitch.api.helix.subscriptions.getSubscriptionForUser(
broadcaster.id,
user.id
));

let isMod = false;
let isVip = false;

if (!isBroadcaster) {
const mods = await twitch.chat.getMods(broadcaster.login);
const vips = await twitch.chat.getVips(broadcaster.login);
isMod = mods.includes(user.login);
isVip = vips.includes(user.login);
}

return {
...getDefaultVars(),
isBroadcaster,
isMod,
isVip,
isSubscriber,
};
}

module.exports = {
getDefaultVars,
getChatUserInfoVars,
getPubSubUserInfoVars,
};
10 changes: 10 additions & 0 deletions app/server/libs/twitch/api/setEvent.js
@@ -0,0 +1,10 @@
const { events } = require("../../../../stores");

module.exports = function setEvent(event) {
events.set(
"events",
events.get("events").map((e) => {
return e.name === event.name ? { ...e, ...event } : e;
})
);
};
2 changes: 2 additions & 0 deletions app/server/libs/twitch/chat/events/index.js
Expand Up @@ -4,6 +4,8 @@ const fs = require("fs");

fs.readdirSync(__dirname).forEach((filename) => {
const name = path.parse(filename).name;

if (name === "index") return;

twitch.chat[name](require(`./${name}`).bind(twitch.chat));
});
7 changes: 4 additions & 3 deletions app/server/libs/twitch/chat/events/onAction.js
@@ -1,6 +1,7 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onAction(channel, user, message) {
const date = new Intl.DateTimeFormat("fr-FR").format(Date.now());
pushActions("onAction", { user, message, date });
module.exports = function onAction(channel, user, message, data) {
const userVars = api.getChatUserInfoVars(data);
pushActions("onAction", { user, message, ...userVars });
};
10 changes: 8 additions & 2 deletions app/server/libs/twitch/chat/events/onBitsBadgeUpgrade.js
@@ -1,6 +1,12 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onBitsBadgeUpgrade(channel, user, upgradeInfo) {
module.exports = function onBitsBadgeUpgrade(channel, user, upgradeInfo, data) {
const userVars = api.getChatUserInfoVars(data);
const { displayName, threshold } = upgradeInfo;
pushActions("onBitsBadgeUpgrade", { user: displayName, threshold });
pushActions("onBitsBadgeUpgrade", {
user: displayName,
threshold,
...userVars,
});
};
10 changes: 9 additions & 1 deletion app/server/libs/twitch/chat/events/onCommunityPayForward.js
@@ -1,9 +1,17 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onCommunityPayForward(channel, user, forwardInfo) {
module.exports = function onCommunityPayForward(
channel,
user,
forwardInfo,
data
) {
const userVars = api.getChatUserInfoVars(data);
const { displayName, originalGifterDisplayName } = forwardInfo;
pushActions("onCommunityPayForward", {
fromUser: originalGifterDisplayName,
toUser: displayName,
...userVars,
});
};
5 changes: 4 additions & 1 deletion app/server/libs/twitch/chat/events/onCommunitySub.js
@@ -1,11 +1,14 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onCommunitySub(channel, user, subInfo) {
module.exports = function onCommunitySub(channel, user, subInfo, data) {
const userVars = api.getChatUserInfoVars(data);
const { count, gifter, gifterGiftCount, plan } = subInfo;
pushActions("onCommunitySub", {
user: gifter,
count,
total: gifterGiftCount,
tiers: plan,
...userVars,
});
};
5 changes: 4 additions & 1 deletion app/server/libs/twitch/chat/events/onGiftPaidUpgrade.js
@@ -1,10 +1,13 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onGiftPaidUpgrade(channel, user, subInfo) {
module.exports = function onGiftPaidUpgrade(channel, user, subInfo, data) {
const userVars = api.getChatUserInfoVars(data);
const { gifter, displayName, plan } = subInfo;
pushActions("onGiftPaidUpgrade", {
fromUser: gifter,
toUser: displayName,
tiers: plan,
...userVars,
});
};
6 changes: 4 additions & 2 deletions app/server/libs/twitch/chat/events/onMessage.js
@@ -1,3 +1,4 @@
const api = require("../../api/getUserInfoVars");
const pushActions = require("../../pushActions");
const settings = require("../../../settings");
const onCommand = require("../onCommand");
Expand All @@ -14,15 +15,16 @@ function parseCommand(prefix, message) {
}

module.exports = async function onMessage(channel, nick, message, data) {
pushActions("onMessage", { user: nick, message });
const userVars = api.getChatUserInfoVars(data);
pushActions("onMessage", { user: nick, message, ...userVars });

const prefix = await settings.get("command.prefix");
if (!isCommand(prefix, message)) return;
const command = parseCommand(prefix, message);

try {
const _onCommand = onCommand.bind(this);
await _onCommand({ command, channel, nick, message, data });
await _onCommand({ command, channel, nick, message, userVars });
} catch (error) {
this.say(channel, error.message);
}
Expand Down
8 changes: 5 additions & 3 deletions app/server/libs/twitch/chat/events/onPrimeCommunityGift.js
@@ -1,6 +1,8 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onPrimeCommunityGift(channel, user, subInfo) {
const { gifter } = subInfo;
pushActions("onPrimeCommunityGift", { user: gifter, name });
module.exports = function onPrimeCommunityGift(channel, user, subInfo, data) {
const userVars = api.getChatUserInfoVars(data);
const { gifter, name } = subInfo;
pushActions("onPrimeCommunityGift", { user: gifter, name, ...userVars });
};
10 changes: 8 additions & 2 deletions app/server/libs/twitch/chat/events/onPrimePaidUpgrade.js
@@ -1,6 +1,12 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onPrimePaidUpgrade(channel, user, subInfo) {
module.exports = function onPrimePaidUpgrade(channel, user, subInfo, data) {
const userVars = api.getChatUserInfoVars(data);
const { displayName, plan } = subInfo;
pushActions("onPrimePaidUpgrade", { user: displayName, tiers: plan });
pushActions("onPrimePaidUpgrade", {
user: displayName,
tiers: plan,
...userVars,
});
};
6 changes: 4 additions & 2 deletions app/server/libs/twitch/chat/events/onRaid.js
@@ -1,6 +1,8 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onRaid(channel, user, raidInfo) {
module.exports = function onRaid(channel, user, raidInfo, data) {
const userVars = api.getChatUserInfoVars(data);
const { displayName, viewerCount } = raidInfo;
pushActions("onRaid", { channel: displayName, viewerCount });
pushActions("onRaid", { channel: displayName, viewerCount, ...userVars });
};
5 changes: 4 additions & 1 deletion app/server/libs/twitch/chat/events/onResub.js
@@ -1,6 +1,8 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onResub(channel, user, subInfo) {
module.exports = function onResub(channel, user, subInfo, data) {
const userVars = api.getChatUserInfoVars(data);
const {
displayName,
plan,
Expand All @@ -19,5 +21,6 @@ module.exports = function onResub(channel, user, subInfo) {
months,
message,
tiers: plan,
...userVars,
});
};
5 changes: 4 additions & 1 deletion app/server/libs/twitch/chat/events/onRewardGift.js
@@ -1,6 +1,8 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onRewardGift(channel, user, rewardGiftInfo) {
module.exports = function onRewardGift(channel, user, rewardGiftInfo, data) {
const userVars = api.getChatUserInfoVars(data);
const {
count,
domain,
Expand All @@ -15,5 +17,6 @@ module.exports = function onRewardGift(channel, user, rewardGiftInfo) {
type: triggerType,
user: gifterDisplayName,
total: gifterGiftCount,
...userVars,
});
};
6 changes: 4 additions & 2 deletions app/server/libs/twitch/chat/events/onRitual.js
@@ -1,6 +1,8 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onRitual(channel, user, ritualInfo) {
module.exports = function onRitual(channel, user, ritualInfo, data) {
const userVars = api.getChatUserInfoVars(data);
const { ritualName, message } = ritualInfo;
pushActions("onRitual", { ritualName, message });
pushActions("onRitual", { ritualName, message, ...userVars });
};
10 changes: 9 additions & 1 deletion app/server/libs/twitch/chat/events/onStandardPayForward.js
@@ -1,6 +1,13 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onStandardPayForward(channel, user, forwardInfo) {
module.exports = function onStandardPayForward(
channel,
user,
forwardInfo,
data
) {
const userVars = api.getChatUserInfoVars(data);
const {
displayName,
recipientDisplayName,
Expand All @@ -11,5 +18,6 @@ module.exports = function onStandardPayForward(channel, user, forwardInfo) {
user: displayName,
fromUser: originalGifterDisplayName,
toUser: recipientDisplayName,
...userVars,
});
};
5 changes: 4 additions & 1 deletion app/server/libs/twitch/chat/events/onSub.js
@@ -1,6 +1,8 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onSub(channel, user, subInfo) {
module.exports = function onSub(channel, user, subInfo, data) {
const userVars = api.getChatUserInfoVars(data);
const {
displayName,
isPrime,
Expand All @@ -19,5 +21,6 @@ module.exports = function onSub(channel, user, subInfo) {
months,
message,
tiers: plan,
...userVars,
});
};
5 changes: 4 additions & 1 deletion app/server/libs/twitch/chat/events/onSubExtend.js
@@ -1,11 +1,14 @@
const pushActions = require("../../pushActions");
const api = require("../../api/getUserInfoVars");

module.exports = function onSubExtend(channel, user, subInfo) {
module.exports = function onSubExtend(channel, user, subInfo, data) {
const userVars = api.getChatUserInfoVars(data);
const { displayName, endMonth, months, plan } = subInfo;
pushActions("onSubExtend", {
user: displayName,
months,
endMonth,
tiers: plan,
...userVars,
});
};

0 comments on commit 0385b3a

Please sign in to comment.