Get a random image from your chosen subreddit then send the image in a Discord message.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const snekfetch = require('snekfetch');
// *** Set your subreddit and channel here ***
const subreddit = 'muffins'
const channel_id = "842483520781025304"
const { body } = await snekfetch
.get(`https://www.reddit.com/r/${subreddit}.json?sort=top&t=week`)
.query({ limit: 800 });
const posts = body.data.children
.filter(post => !!post.data.url && !post.data.over_18)
.filter(post => post.data.url.endsWith('.jpg') || post.data.url.endsWith('.png') || post.data.url.endsWith('.gif'));
if (posts.length) {
const randomPost = posts[Math.floor(Math.random() * posts.length)]
await lib.discord.channels['@0.1.1'].messages.create({
channel_id,
content: 'You got a muffin!',
tts: false,
embed: {
type: 'rich',
title: '',
description: '',
color: 0x00ffff,
image: {
url: randomPost.data.url,
},
},
});
}