提问者:小点点

我如何获得一个随机的子Reddit图像到我的discord。机器人?


我在异步python中制作一个不和谐机器人。当我执行命令时,我希望机器人发布一张随机图片(前缀!)例子!迷因。这将从subreddit中调出一张随机图片,在这种情况下是迷因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")

我该怎么做,使用discord.py和PRAW?


共2个答案

匿名用户

下面的代码将从模因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')

匿名用户

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