【问题标题】:How to make my meme command response faster? in discord py rewrite如何让我的 meme 命令响应更快?在不和谐 py 重写
【发布时间】: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 一切正常。我不知道问题出在哪里,我读到它们在编码时都是一样的。我没有预加载进度,因为我不知道怎么做。我尝试了链接的帖子,但效果不佳。
  • 他们不一样。 asyncprawpraw 的异步版本,你必须不时使用 await 关键字。每次运行命令时创建praw.Reddit 实例的目的是什么?
  • 我用 asyncpraw 再试一次,但我仍然得到同样的错误,那个协程对象没有顶部属性。我想我只是不知道如何使用它。使用 praw.Reddit 我看了一个教程,但我认为我通过将其放在代码顶部使其成为全球性的。如果这是使某些东西全球化的方法,我是 discord py 的新手,没有太多经验

标签: python discord discord.py praw


【解决方案1】:

基本上,这使用了与您引用的帖子相同的想法,但我从您的代码中改编了它。

all_subs = []
oldSubred = "memes"
@commands.command(aliases=["memes", "r/memes", "reddit"])
async def meme(self, ctx, subred="memes"):
    global oldSubred
    if oldSubred != subred: # This checks if the subreddit from the previous time you used this command is the same as this time, if it's not, it picks brand new submissions, if it is, then the bot will use the submissions it got from the last time it ran the command, shortening the time needed to get a submission to send by a LOT
        all_subs.clear() # Clears all_subs if the desired subreddit is different from the posts inside all_subs

    msg = await ctx.send('Loading ... <a:Loading:845258574434795570>')

# You really shouldn't be logging into praw EVERY time you want to run this command, put this above the @commands.command
 #   reddit = praw.Reddit(client_id='ID',
 #                           client_secret='SECRET',
 #                           username="USERNAME",
 #                           password='PASSWORD',
 #                           user_agent='AGENT')

    subreddit = reddit.subreddit(subred)

    if all_subs == []: # Only gathers posts if all_subs is empty
        top = subreddit.top(limit=350)
        for submission in top:
            all_subs.append(submission)

    random_sub = all_subs[0] # Not really random anymore, just goes through the list
    all_subs.pop() # Gets rid of the 0 index variable from all_subs that we just used above

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

本人不是专业人士,如有不足请指出。

【讨论】:

    猜你喜欢
    • 2021-07-07
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 2022-01-26
    • 2019-01-29
    相关资源
    最近更新 更多