Skip to content

Commit

Permalink
feat: Added memes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zastinian committed Apr 19, 2024
1 parent 87b897d commit 01729c7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/commands/images/meme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { sticker } = require("../../lib/sticker")
const memes = require("../../util/memes")

module.exports = {
name: "meme",
run: async (bot) => {
const meme = await memes({ locale: "en", customSubredditName: false, fullRawBody: false })
const stiker = await sticker(null, meme.image, meme.caption)
bot.sendFile(m.chat, stiker, null, { asSticker: true })
},
}
13 changes: 13 additions & 0 deletions src/util/constants/subreddits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const subreddit = {
br: "ItHadToBeBrazil",
de: "GermanMemes",
en: "dankmemes",
es: "memesesp",
fr: "FrenchMemes",
in: "IndianDankMemes",
it: "italianmemes",
ru: "YouSeeComrade",
tr: "burdurland",
}

module.exports = subreddit
31 changes: 31 additions & 0 deletions src/util/memes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const subreddit = require("./constants/subreddits")

module.exports = async ({ locale, customSubredditName, fullRawBody }) => {
try {
const subredditName = locale ? subreddit[locale] : subreddit.en
const response = await fetch(
`https://www.reddit.com/r/${customSubredditName || subredditName}/hot/.json?count=100`,
{
method: "GET",
headers: {
"User-Agent": "https://github.com/Zastinian/HedystiaMD",
"Authorization": "",
},
}
)
const memeObject = await response.json()
const randomPost =
memeObject.data.children[Math.floor(Math.random() * memeObject.data.children.length)]

return fullRawBody
? randomPost
: {
image: randomPost.data.url,
category: randomPost.data.link_flair_text,
caption: randomPost.data.title,
permalink: randomPost.data.permalink,
}
} catch (error) {
console.error(`Error fetching meme from Reddit ${error}`)
}
}

0 comments on commit 01729c7

Please sign in to comment.