【发布时间】:2020-11-28 11:04:48
【问题描述】:
我目前正在尝试制作一个 Discord boy 命令(使用 discord.py 重写),该命令从子 reddit 中获取顶部帖子,其中包含所有相关信息(例如,点赞、奖励、顶级评论等)。该命令可以工作,但该命令仅在我第一次使用它时才有效。在我第二次使用它后,我得到了这个错误:
Traceback (most recent call last):
File "C:\Users\Optic\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:/Users/Optic/PycharmProjects/DiscordBot/Lilliebot.py", line 155, in reddit
post_url, error, error_type, top_comment, upvote, awarded, title, sfw, url, thumbnail = Reddit.Redget(
ValueError: not enough values to unpack (expected 10, got 2)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Optic\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Optic\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Optic\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ValueError: not enough values to unpack (expected 10, got 2)
我不明白这怎么可能。这让我非常沮丧。
这是我的实际命令代码(它只有一个部分,但代码重复了几次:
import praw
import prawcore
def Redget(inp, inptype):
try:
global post_url, thumbnail, upvote, url, awarded, sfw, top_comment, title, error, error_type, id
reddit = praw.Reddit(
client_id='my id',
client_secret='my secret',
username='please dont hack me',
password='thank you very much',
user_agent='here is a little guy waving: 0/'
)
if inptype.lower() == "hot":
subreddit = reddit.subreddit(inp)
post = subreddit.hot(limit=5)
count = 0
top_count = 0
try:
try:
for x in post:
if not x.stickied:
if count >= 1:
pass
else:
try:
url = x.url
upvote = x.ups
awarded = x.top_awarded_type
thumbnail = x.thumbnail
id = x.id
title = x.title
for top_level_comment in x.comments:
if top_count >= 1:
pass
else:
top_comment = top_level_comment.body
top_count = top_count + 1
if x.over_18:
sfw = False
else:
sfw = True
except AttributeError:
pass
count = count + 1
else:
pass
except prawcore.exceptions.NotFound:
error = True
except prawcore.exceptions.Redirect:
error = True
else:
error_type = True
post_url = f'https://reddit.com/r/{inp}/comments/{id}/'
return post_url, error
except NameError:
error = False
error_type = False
return post_url, error, error_type, top_comment, upvote, awarded, title, sfw, url, thumbnail
这是我的机器人中的代码,用于处理返回的内容:
@bot.command()
async def reddit(ctx, type, subreddit):
requests_cache.install_cache(cach_name='reddit', backend='sqlite', expire_after=1800)
post_url, error, error_type, top_comment, upvote, awarded, title, sfw, url, thumbnail = Reddit.Redget(
subreddit,
type)
embed = discord.Embed(
title=title,
color=discord.Colour.orange()
)
embed.set_image(url=url)
if sfw is False:
await ctx.send('sorry, this post is nsfw, and this command doesn\'t allow nsfw posts.')
else:
await ctx.send(embed=embed)
await ctx.send(f'''```
Upvotes: {upvote}
Awards: {awarded}
Post: {post_url}```''')
await ctx.send(f'```top comment: {top_comment}```')
任何解决此问题的帮助将不胜感激。顺便提一句。我尝试了 aPRAW,但我得到了更令人困惑的错误。如果可能的话,我想坚持使用 PRAW。我真的不想重新编写我所有的代码。
非常感谢您。
【问题讨论】:
-
可能的修复?当然。这当然是(再次?!?)“其他”程序的工作方式......具有不同的错误集。
标签: python-3.x discord.py discord.py-rewrite reddit praw