-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharedFunctions.js
191 lines (179 loc) · 6.07 KB
/
sharedFunctions.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const {
RedditContent,
Listing
} = require('snoowrap');
const snoowrap = require('snoowrap'); //Reddit lib
const config = require('./config/config.json'); //Config lib
const defaultConfig = config.creds; //Config
const fs = require('fs');
const command = require('nodemon/lib/config/command');
const {
title
} = require('process');
//SnooWrap request with OAuth credentials
const r = new snoowrap({
userAgent: defaultConfig.userAgent,
clientId: defaultConfig.clientId,
clientSecret: defaultConfig.clientSecret,
refreshToken: defaultConfig.refreshToken,
accessToken: defaultConfig.accessToken
});
//////////////////////////////////FUNCTIONS/////////////////////////////////////////
//Get Hot Posts from SWRP
async function getHotPosts(input) {
if (input === "content") {
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve(r.getHot('StarwarsRP').map(RedditContent => RedditContent.id)))
})
let result = await promise
return Promise.resolve(result)
}
if (input === "title") {
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve(r.getHot('Starwarsrp').map(RedditContent => RedditContent.title)))
})
let result = await promise
return Promise.resolve(result)
}
};
//Get Post Data
// Needs:
// - the segment of the post you want (Body | Title)
// - the postID (found in the posts URL)
async function getPostData(segment, postID) {
let promise = new Promise((resolve, reject) => {
if (segment == 'Body') {
setTimeout(() => resolve(r.getSubmission(postID).selftext), 100)
}
if (segment == 'Title') {
setTimeout(() => resolve(r.getSubmission(postID).title), 100)
}
if (segment == 'postID') {
setTimeout(() => resolve(r.getSubmission(postID).postID), 100)
}
if (segment == 'URL') {
setTimeout(() => resolve(r.getSubmission(postID).url), 100)
}
if (segment == "Author") {
setTimeout(() => resolve(r.getSubmission(postID).author), 100)
}
if (segment == "AuthorIcon") {
setTimeout(() => resolve(r.getSubmission(postID).author.icon_img), 100)
}
if (segment == "PostTitle") {
setTimeout(() => resolve(r.getSubmission(postID).title), 100)
}
});
let result = await promise;
return result;
};
////gets the new posts
async function getNewPosts(type) {
if (type === 'postID') {
let promise = r.getNew('Starwarsrp').map(RedditContent => RedditContent.id)
let result = await promise
return result
}
if (type === 'title') {
let promise = r.getNew('Starwarsrp').map(RedditContent => RedditContent.title)
let result = await promise;
return result
}
if (type === 'url') {
let promise = r.getNew('Starwarsrp').map(RedditContent => RedditContent.url)
let result = await promise;
return result
}
}
//////Get lists of things and content
//////You can basically just construct a list of things here and then for loop it to map the data accordingly in a discord post.
async function getNewPostsV2(subred, segment) {
if (segment === 'title') {
let promisePostTitles = r.getNew(subred, {
limit: 3
}).map(RedditContent => RedditContent.title)
let result = await promisePostTitles;
return result
}
if (segment === 'author') {
let promisePostAuthors = r.getNew(subred, {
limit: 3
}).map(RedditContent => RedditContent.author.name)
let result = await promisePostAuthors;
return result
}
if (segment === 'postid') {
let promisePostID = r.getNew(subred, {
limit: 3
}).map(RedditContent => RedditContent.id)
let result = await promisePostID
return result
}
if (segment === 'url') {
let promisePostURL = r.getNew(subred, {
limit: 3
}).map(RedditContent => RedditContent.url)
let result = await promisePostURL
return result
}
if (segment === 'authicon') {
let promiseAuthIcon = r.getNew(subred, {
limit: 3
}).map(RedditContent => RedditContent.author.icon_img)
let result = await promiseAuthIcon
return result
}
if (segment === 'body') {
let promiseAuthIcon = r.getNew(subred, {
limit: 3
}).map(RedditContent => RedditContent.body)
let result = await promiseAuthIcon
return result
}
}
async function getRecentComments(subreddit, setting, limit) {
if (setting === "id") {
let promiseComments = r.getNewComments(subreddit, {
limit: limit
}).map(RedditContent => RedditContent.id);
let result = await promiseComments
return result
}
if (setting === "author") {
let id = subreddit
let promiseComments = r.getComment(id).author.name;
let result = await promiseComments
return result
}
if (setting === "body") {
let id = subreddit
let promiseComments = r.getComment(id).body;
let result = await promiseComments
return result
}
if (setting === "sub") {
let id = subreddit
let promiseComments = r.getComment(id).subreddit.display_name;
let result = await promiseComments
return result
}
if (setting === "icon") {
let id = subreddit
let promiseComments = r.getComment(id).author.icon_img;
let result = await promiseComments
return result
}
if (setting === "parent") {
let id = subreddit
let promiseComments = r.getComment(id).link_id;
let result = await promiseComments
return result.slice(3, 100)
}
}
module.exports = {
getHotPosts,
getPostData,
getNewPosts,
getNewPostsV2,
getRecentComments
};