get data from reddit api

This commit is contained in:
night0721 2021-09-14 02:02:25 +08:00
parent 29dbe6d309
commit 90a08897e0
2 changed files with 25 additions and 4 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "cath", "name": "cath",
"version": "1.1.3-dev", "version": "1.2.0-dev",
"description": "A powerful package that can interact with Cath API", "description": "A powerful package that can interact with Cath API",
"main": "./dist/index.js", "main": "./dist/index.js",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",
@ -38,9 +38,6 @@
"discord.js": "^13.1.0" "discord.js": "^13.1.0"
}, },
"devDependencies": { "devDependencies": {
"@types/jsdom": "^16.2.13",
"ts-node": "^10.2.1",
"typedoc": "^0.21.9",
"typescript": "^4.4.3" "typescript": "^4.4.3"
} }
} }

24
src/functions/reddit.ts Normal file
View file

@ -0,0 +1,24 @@
import axios from "axios";
/**
* Sends an embed of reddit
* @return {Promise<Object>}
*/
export async function getreddit(sub: string): Promise<object> {
const content = await axios
.get(`https://www.reddit.com/r/${sub}/random/.json`)
.then(res => res.data);
let permalink = content[0].data.children[0].data.permalink;
let memeURL = `https://reddit.com${permalink}`;
let memeImage = content[0].data.children[0].data.url;
let memeTitle = content[0].data.children[0].data.title;
let memeUpvotes = content[0].data.children[0].data.ups;
let memeDownvotes = content[0].data.children[0].data.downs;
let memeNumComments = content[0].data.children[0].data.num_comments;
const obj: object = {
title: `A cat image | ${memeTitle}`,
url: `${memeURL}`,
image: memeImage,
footer: ` 👍 ${memeUpvotes} 💬 ${memeNumComments}`,
};
return obj;
}