首页 > 解决方案 > 为什么python在运行我的discord命令两次后输出这个ValueError,有什么可能的修复吗?

问题描述

我目前正在尝试制作一个 Discord boy 命令(使用 discord.py rewrite),该命令从子 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.xdiscord.pydiscord.py-rewriteredditpraw

解决方案


您的函数可以返回不同数量的值。这是一个严重的问题。

从您粘贴的代码底部截取

        return post_url, error       <--- returns two values (what is throwing your error)
    except NameError:
        error = False
        error_type = False
    return post_url, error, error_type, top_comment, upvote, awarded, title, sfw, url, thumbnail  <--- returns more than two values, this is what you are expecting when you call the function

您需要使函数始终返回相同数量的值。您可以返回一些额外的None值,或者您可以将这些值填充到字典或元组中并检查它们是否存在。这些解决方案中的任何一个都可以。


推荐阅读