是的,我见过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发布于 2021-08-11 15:37:41
基本上,这使用了与您引用的帖子相同的想法,但我根据您的代码对其进行了修改。
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:')我不是专业人士,所以如果你看到任何缺陷,请指出。
https://stackoverflow.com/questions/67692654
复制相似问题