【发布时间】:2021-08-13 23:22:23
【问题描述】:
是的,我见过this post,但这根本没有帮助。我想预加载模因或以一种方式缓存它们,或以任何其他方式减少等待时间。我曾尝试使用 asyncpraw,但由于某种原因,subreddit.top 在那里不起作用,所以我会坚持使用 praw。我的目标是减少等待时间,因为我必须等待超过 10 秒,如果我向 meme 命令发送垃圾邮件,机器人就会崩溃。所以我需要解决这个问题。
import discord
from discord.ext import commands
import praw
from bot_config.settings import botowner, avatarowner
from discord import Embed
@commands.command(aliases=["memes", "r/memes", "reddit"])
async def meme(self, ctx, subred="memes"):
msg = await ctx.send('Loading ... <a:Loading:845258574434795570>')
reddit = praw.Reddit(client_id='ID',
client_secret='SECRET',
username="USERNAME",
password='PASSWORD',
user_agent='AGENT')
subreddit = reddit.subreddit(subred)
all_subs = []
top = subreddit.top(limit=350)
for submission in top:
all_subs.append(submission)
random_sub = random.choice(all_subs)
name = random_sub.title
url = random_sub.url
embed = Embed(title=f'__{name}__', colour=discord.Colour.random(),
timestamp=ctx.message.created_at, url=url)
embed.set_image(url=url)
embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar_url)
embed.set_footer(text=f'Bot by {botowner}', icon_url=avatarowner)
await ctx.send(embed=embed)
await msg.edit(content=f'<https://reddit.com/r/{subreddit}/> :white_check_mark:')
return
【问题讨论】:
-
您根本没有表现出进行预加载/缓存的努力。你被它困在哪里了?
-
也许不是为每个命令创建一个新的
praw.Reddit实例,为什么不拥有一个全局实例并重新使用它?也不要使用praw使用asyncpraw -
使用 asyncpraw 它告诉我“命令引发异常:AttributeError:'coroutine' 对象没有属性 'top'”并且使用普通 praw 一切正常。我不知道问题出在哪里,我读到它们在编码时都是一样的。我没有预加载进度,因为我不知道怎么做。我尝试了链接的帖子,但效果不佳。
-
他们不一样。
asyncpraw是praw的异步版本,你必须不时使用await关键字。每次运行命令时创建praw.Reddit实例的目的是什么? -
我用 asyncpraw 再试一次,但我仍然得到同样的错误,那个协程对象没有顶部属性。我想我只是不知道如何使用它。使用 praw.Reddit 我看了一个教程,但我认为我通过将其放在代码顶部使其成为全球性的。如果这是使某些东西全球化的方法,我是 discord py 的新手,没有太多经验
标签: python discord discord.py praw