是的,我见过 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
答案 0 :(得分:0)
基本上这使用了与您引用的帖子相同的想法,但我根据您的代码对其进行了修改。
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:')
我不是专业人士,所以如果你看到它们,请指出任何缺陷。