如何在我的Python Discord Bot中添加Reddit函数?

时间:2018-11-18 00:02:16

标签: python discord discord.py reddit

我想让一个机器人在您执行“!meme”时从r / memes / hot /发布一个随机的meme。我该怎么做?我已经有了一些基本代码,但是它不起作用。 ,我不确定user_agent到底是什么。有人可以给我一个例子吗?

client = Bot(command_prefix=BOT_PREFIX)
reddit = praw.Reddit(client_id='id',
                     client_secret='secret',
                     user_agent='windows 10: Meme Scraper (by /u/PotatoLord1207)')

@client.command()
async def meme():
    memes_submissions = reddit.subreddit('memes').hot()
    post_to_pick = random.randint(1, 20)
    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)

1 个答案:

答案 0 :(得分:0)

我在 discord.py 重写版本中做了一个 meme 命令。这可能对您有所帮助:

@client.command()
async def meme(ctx):
    subreddit = reddit.subreddit("memes")
    all_subs = []

    top = subreddit.top(limit = 50)

    for submission in top:
        all_subs.append(submission)

    random_sub = random.choice(all_subs)

    name = random_sub.title
    url = random_sub.url

    embed = discord.Embed(title = name, color=discord.Color.gold())
    embed.set_image(url = url)
    embed.set_footer(text=f"Asked by {ctx.author.name}")

    await ctx.send(embed=embed)