How Do I Get A Random Subreddit Image To My Discord.py Bot?
I am making a discord bot in async python. I want the bot to post a random picture when I do a command (prefix !) example !meme. This would bring up a random picture from a subredd
Solution 1:
The below code will fetch a random post from the memes subreddit. Currently it picks a random submission from the top 10 posts from the hot section.
import praw
import random
from discord.ext import commands
bot = commands.Bot(description="test", command_prefix="!")
reddit = praw.Reddit(client_id='CLIENT_ID HERE',
client_secret='CLIENT_SECRET HERE',
user_agent='USER_AGENT HERE')
@bot.command()
async def meme():
memes_submissions = reddit.subreddit('memes').hot()
post_to_pick = random.randint(1, 10)
for i in range(0, post_to_pick):
submission = next(x for x in memes_submissions if not x.stickied)
await bot.say(submission.url)
bot.run('TOKEN')
Solution 2:
@client.command(aliases=['Meme'])
async def meme(ctx):
reddit = praw.Reddit(client_id='XXXX',
client_secret='XXXX',
user_agent='XXXX')
submission = reddit.subreddit("memes").random()
await ctx.send(submission.url)
Post a Comment for "How Do I Get A Random Subreddit Image To My Discord.py Bot?"