-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeme.js
61 lines (55 loc) · 1.36 KB
/
meme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const Discord = require("discord.js");
Array.prototype.randomElement = function () {
return this[Math.floor(Math.random() * this.length)]
}
function imageUrl(result) {
return (result.data.url.match(/\.(jpeg|jpg|gif|png)$/) != null)
}
function memeDisallowed(result) {
if (result.data.is_video ||
result.data.selftext.length > 2049 ||
result.data.over_18) {
return true
} else {
return false
}
}
function youtubeUrl(result) {
return (result.data.media && result.data.media.type == 'youtube.com')
}
function generateMsg(results) {
let result = results.randomElement()
if (memeDisallowed(result)) {
return 'Sorry Dave, I cannot do that'
}
if (imageUrl(result)) {
return {
title: result.data.title,
image: { url: result.data.url }
}
} else if (youtubeUrl(result)) {
return {
title: result.data.title,
video: { url: result.data.url }
}
}
else {
return {
title: result.data.title,
description: result.data.selftext
}
}
}
module.exports = {
new: function(subreddit) {
return fetch(`https://www.reddit.com/r/${subreddit}/new/.json`)
.then(function(response) {
return response.json()
})
.then(function(json) {
let results = json.data.children
let result = generateMsg(results)
return result
})
}
}