【问题标题】:How do I add a Reddit function to my Python Discord Bot?如何将 Reddit 函数添加到我的 Python Discord Bot?
【发布时间】:2019-04-20 18:08:41
【问题描述】:

我想制作一个机器人,当您执行 "!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)

【问题讨论】:

  • 应该是client.say,而不是bot.say。你真的在运行你的机器人吗? client.run("token")
  • 我解决了这个问题,但它仍然无法正常工作。有什么建议吗?
  • “行不通”是什么意思?
  • 这就是错误。我对 discord.py 很陌生,所以这个错误对我来说没有多大意义

标签: python discord discord.py reddit


【解决方案1】:

我在 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)

【讨论】:

    猜你喜欢
    • 2019-09-05
    • 2020-05-05
    • 2021-05-17
    • 2021-03-29
    • 2018-06-06
    • 2021-07-20
    • 2021-07-18
    • 2021-04-29
    • 1970-01-01
    相关资源
    最近更新 更多