From 765755a0410b84027ef91e2ed442beeaa40059ed Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 28 Dec 2019 00:29:14 -0500 Subject: [PATCH] Added first time submission notifications for discord. Also updated the downvote notification. --- config.json.example | 1 + index.js | 44 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/config.json.example b/config.json.example index 3e27111..d771410 100644 --- a/config.json.example +++ b/config.json.example @@ -4,6 +4,7 @@ "adminUserID": "[the hashed id of the user who can perform admin actions]", "youtubeAPIKey": null, //get this from Google Cloud Platform [optional] "discordReportChannelWebhookURL": null, //URL from discord if you would like notifications when someone makes a report [optional] + "discordFirstTimeSubmissionsWebhookURL": null, //URL from discord if you would like notifications when someone makes a first time submission [optional] "behindProxy": true, "db": "./databases/sponsorTimes.db", "privateDB": "./databases/private.db", diff --git a/index.js b/index.js index e287ad3..3c3c914 100644 --- a/index.js +++ b/index.js @@ -221,6 +221,46 @@ app.get('/api/postVideoSponsorTimes', async function (req, res) { } else { res.sendStatus(409); } + + //check if they are a first time user + //if so, send a notification to discord + if (config.youtubeAPIKey !== null && config.discordFirstTimeSubmissionsWebhookURL !== null) { + let userSubmissionCountResult = await new Promise((resolve, reject) => { + db.prepare("SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?").get(userID, (err, row) => resolve({err, row})); + }); + + // If it is a first time submission + if (userSubmissionCountResult.row.submissionCount === 0) { + YouTubeAPI.videos.list({ + part: "snippet", + id: videoID + }, function (err, data) { + if (err) { + console.log(err); + return; + } + + request.post(config.discordFirstTimeSubmissionsWebhookURL, { + json: { + "embeds": [{ + "title": data.items[0].snippet.title, + "url": "https://www.youtube.com/watch?v=" + videoID + "&t=" + (startTime.toFixed(0) - 2), + "description": "Submission ID: " + UUID + + "\n\nTimestamp: " + + getFormattedTime(startTime) + " to " + getFormattedTime(endTime), + "color": 10813440, + "author": { + "name": userID + }, + "thumbnail": { + "url": data.items[0].snippet.thumbnails.maxres.url, + } + }] + } + }); + }); + } + } }); } }); @@ -334,9 +374,9 @@ app.get('/api/voteOnSponsorTime', function (req, res) { json: { "embeds": [{ "title": data.items[0].snippet.title, - "url": "https://youtube.com/watch?v=" + submissionInfoResult.row.videoID + "&t=" + submissionInfoResult.row.startTime.toFixed(0), + "url": "https://www.youtube.com/watch?v=" + submissionInfoResult.row.videoID + "&t=" + (submissionInfoResult.row.startTime.toFixed(0) - 2), "description": "**" + row.votes + " Votes | " + row.views + " Views**\n\nSubmission ID: " + UUID + - "\n\nSubmission by " + submissionInfoResult.row.userID + "\n\nTimestamp: " + + "\n\nSubmitted by: " + submissionInfoResult.row.userID + "\n\nTimestamp: " + getFormattedTime(submissionInfoResult.row.startTime) + " to " + getFormattedTime(submissionInfoResult.row.endTime), "color": 10813440, "author": {