首页 文章

如何获得随机的subreddit图像到我的discord.py机器人?

提问于
浏览
-5

我在async python中制作一个discord bot . 我希望机器人在执行命令(前缀!)示例时发布 random 图片!meme . 这会从subreddit中显示一个随机图片,在这种情况下是memes subreddit . 我已经开始了我想要的,但我需要随机subreddit位的帮助 .

import discord
import praw
from discord.ext import commands

bot = commands.Bot(description="test", command_prefix="!")

@bot.command()
async def meme():
await bot.say(---)   
#--- WOULD BE THE REDDIT URL
bot.run("TOKEN")

How would I do this, using discord.py and PRAW?

谢谢阅读! - 亚历克斯

1 回答

  • 1

    以下代码将从memes subreddit中获取随机帖子 . 目前,它从热门部分的前10个帖子中随机提交 .

    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')
    

相关问题