【问题标题】:Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "makePoll" is not found忽略命令中的异常无:discord.ext.commands.errors.CommandNotFound:找不到命令“makePoll”
【发布时间】:2021-02-06 17:40:47
【问题描述】:

所以我尝试使用 python 为我的 discord 机器人做这个简单的投票功能:

@command(name="makePoll")
@has_permissions(manage_guild=True)
async def createPoll(self, ctx, question, *options):
    if len(options) > 10:
        await ctx.send("You can only supply a maximum of 10 options.")

    else:
        embed = Embed(title="Poll",
                     description=question,
                     colour=ctx.author.colour,
                     timestamp=datetime.utcnow())
    fields = [("Options", "\n".join([f"{numbers[idx]} {options}" for idx, option in enumerate(options)]), False),
              ("Instructions", "React to case a vote!", False)]

    for name, value, inline in fields:
        embed.add_field(name=name, value=value, inline=inline)

    await ctx.send(embed=embed)

问题是当我尝试调用该函数时它会抛出一个错误。 错误:忽略命令无中的异常: discord.ext.commands.errors.CommandNotFound:找不到命令“makePoll”。

编辑:试图命名 client.command makepoll 但知道它给了我这个错误:忽略命令 makePoll 中的异常: 回溯(最近一次通话最后): 文件“F:\first try on python\venv\lib\site-packages\discord\ext\commands\core.py”,第 85 行,已包装 ret = 等待 coro(*args, **kwargs) 文件“F:/first try on python/for loop/bot1.py”,第 40 行,在 createPoll 中 颜色=ctx.author.color, AttributeError: 'str' 对象没有属性 'author'

上述异常是以下异常的直接原因:

Traceback(最近一次调用最后一次): 文件“F:\first try on python\venv\lib\site-packages\discord\ext\commands\bot.py”,第 903 行,在调用中 等待 ctx.command.invoke(ctx) 文件“F:\first try on python\venv\lib\site-packages\discord\ext\commands\core.py”,第 859 行,在调用中 等待注入(*ctx.args,**ctx.kwargs) 文件“F:\first try on python\venv\lib\site-packages\discord\ext\commands\core.py”,第 94 行,已包装 从 exc 引发 CommandInvokeError(exc) discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:'str'对象没有属性'author'

【问题讨论】:

  • 你能展示你的进口吗?
  • 你可以尝试用@client.command(name="makePoll") 代替@command(name="makePoll")吗?
  • import discord, import time, import random, from discord import Embed, from date time, from discord.ext import command, tasks, from itertools import cycle from discord.ext.comand import has_permissions, command
  • @Nurqm 使用了你的方法,但现在它抛出了一个完全不同的错误
  • 试试commands.command(name='makepool')。

标签: python-3.x discord.py


【解决方案1】:

参考您的编辑,它说“str”对象没有“作者”属性。

str 对象在你的函数中引用ctx。你不必添加变量self,因为我不认为这个命令在一个类中。

Discord.py 看到这一点,并认为“self 是 context,ctx 是字符串参数(用于命令)......”

解决办法是去掉“self”。

所以代码是:

@client.command(name="makePoll")
@has_permissions(manage_guild=True)
async def createPoll(ctx, question, *options):
    if len(options) > 10:
        await ctx.send("You can only supply a maximum of 10 options.")

    else:
        embed = Embed(title="Poll",
                     description=question,
                     colour=ctx.author.colour,
                     timestamp=datetime.utcnow())
    fields = [("Options", "\n".join([f"{numbers[idx]} {options}" for idx, option in enumerate(options)]), False),
              ("Instructions", "React to case a vote!", False)]

    for name, value, inline in fields:
        embed.add_field(name=name, value=value, inline=inline)

    await ctx.send(embed=embed)

self 主要用于类 btw

我也建议你将命令的名称设置为与函数名称相同

【讨论】:

    猜你喜欢
    • 2021-02-19
    • 2023-02-25
    • 2021-09-12
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多