Add custom webhooks for votes

This commit is contained in:
TAG-Epic
2020-08-23 19:42:52 +02:00
parent 22fc90713a
commit f7d162b955

View File

@@ -178,8 +178,6 @@ async function voteOnSponsorTime(req, res) {
}
}
// Send discord message
if (incrementAmount < 0) {
// Get video ID
let submissionInfoRow = db.prepare('get', "SELECT s.videoID, s.userID, s.startTime, s.endTime, s.category, u.userName, " +
"(select count(1) from sponsorTimes where userID = s.userID) count, " +
@@ -197,7 +195,7 @@ async function voteOnSponsorTime(req, res) {
webhookURL = config.discordCompletelyIncorrectReportWebhookURL;
}
if (config.youtubeAPIKey !== null && webhookURL !== null) {
if (config.youtubeAPIKey !== null && (webhookURL !== null || config.webhooks.size !== 0)) {
YouTubeAPI.videos.list({
part: "snippet",
id: submissionInfoRow.videoID
@@ -206,7 +204,57 @@ async function voteOnSponsorTime(req, res) {
err && console.log(err);
return;
}
let isUpvote = incrementAmount > 0
// Send custom webhooks
if (config.webhooks.size !== 0) {
console.log("Dispatching webhooks");
config.webhooks.forEach(customWebhook => {
let customWebhookURL = customWebhook.url;
let scopes = customWebhook.scopes;
let key = customWebhook.key;
if ((!isUpvote && !scopes.includes("vote.down")) || (isUpvote && !scopes.includes("vote.up"))) {
return;
}
request.post(customWebhookURL, {
json: {
"user": {
"status": userSubmissionCountRow.submissionCount === 0 ? "new" : (isVIP ? "vip" : "normal")
},
"video": {
"id": submissionInfoRow.videoID,
"title": data.items[0].snippet.title,
"url": "https://www.youtube.com/watch?v=" + submissionInfoRow.videoID,
"thumbnail": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : ""
},
"submission": {
"id": UUID,
"views": row.views,
"category": category,
"startTime": submissionInfoRow.startTime,
"endTime": submissionInfoRow.endTime,
"user": {
"uuid": submissionInfoRow.userID,
"username": submissionInfoRow.userName,
"submissions": {
"total": submissionInfoRow.count,
"ignored": submissionInfoRow.disregarded
}
}
},
"votes": {
"before": row.votes,
"after": (row.votes + incrementAmount - oldIncrementAmount)
}
},
headers: {
"Authorization": key,
"Event-Type": isUpvote ? "upvote" : "downvote"
}
});
});
}
// Send discord message
if (webhookURL !== null && !isUpvote) {
request.post(webhookURL, {
json: {
"embeds": [{
@@ -241,8 +289,9 @@ async function voteOnSponsorTime(req, res) {
console.log("\n");
}
});
});
}
});
}
}