【问题标题】:Command raised an exception: NameError: name 'convert' is not defined命令引发异常:NameError: name 'convert' is not defined
【发布时间】:2020-12-21 02:40:05
【问题描述】:

所以我试图通过观看 yt 的一些教程来制作赠品命令 我得到一个错误....实际上我在代码中定义了 convert,但它显示错误 Command 引发了异常:NameError: name 'convert' is not defined。但是我观看的教程没有错误.....而且我正在使用 discord.py rewrite........任何帮助都得到了帮助:)。

代码:

class Giveaway(commands.Cog):
    def __init__(self, client):
        self.client = client

    def convert(time):
        pos = ["s", "m", "h", "d"]
        time_dict = {"s" : 1, "m" : 60, "h" : 3600, "d" : 3600*24}
        unit = time[-1]

        if unit not in pos:
            return -1
        try:
            val = int(time[:-1])
        except:
            return -2

        return val * time_dict[unit]

    @commands.command()
    async def giveaway(self, ctx):
        await ctx.send("Answer The Following Questions For Giveaway:")

        questions = ["Channel For Giveaway?",
                     "Duration Of Giveaway? (s , m , h , d)",
                     "Prize Of Giveaway?"]

        answers = []

        def check(m):
            return m.author == ctx.author and m.channel == ctx.channel

        for i in questions:
            await ctx.send(i)

            try:
                msg = await self.client.wait_for('message', timeout=20.0, check=check)
            except asyncio.TimeoutError:
                await ctx.send('You Did Not Answer In Time!')
                return
            else:
                answers.append(msg.content)

        try:
            c_id = int(answers[0][2:-1])
        except:
            await ctx.send(f'Mention Channel Properly! Example: {ctx.channel.mention}')
            return

        channel = self.client.get_channel(c_id)

        time = convert(answers[1])
        if time == -1:
            await ctx.send(f'Answer Time With A Proper Unit (s, m, h, d)')
            return
        elif time == -2:
            await ctx.send(f'Time Must Be A Integer!')
            return

        prize = answers[2]

        await ctx.send(f'Giveaway Will Be In Channel {channel.mention} And Will Last {time}.')

        embed = discord.Embed(title="????Giveaway????", description=f"{prize}", color=ctx.author.color)
        embed.add_field(name="Hosted by:", value= ctx.author.mention)
        embed.set_footer(text=f"Ends after {answers[1]} from now. ")

        g_msg = await channel.send(embed=embed)

        await g_msg.add_reaction("????")

        await asyncio.sleep(time)

        new_msg = await channel.fetch_message(g_msg.id)

        users = await g_msg.reactions[0].users().flatten()
        users.pop(users.index(self.client.user))

        winner = random.choice(users)

        await channel.send(f'Congrats! {winner.mention} won {prize}!')

错误:

 time = convert(answers[1])
NameError: name 'convert' is not defined

raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'convert' is not defined

【问题讨论】:

    标签: python python-3.x discord discord.py discord.py-rewrite


    【解决方案1】:

    您应该将该方法引用为self.convert

    time = self.convert(answers[1])

    另外,convert 方法的签名应该以self 参数开头:

    def convert(self, time):

    你应该学习如何classes work in Python

    【讨论】:

    • 我知道我们必须在 cogs 中使用 self ,但对在代码中进一步定义和使用它们很陌生......这对我很有帮助......非常感谢
    • 如果它解决了你的问题,点击他的答案上的复选标记接受它作为答案
    • 如果用@staticmethod装饰,则不需要self作为参数,因为该函数不引用self。它也可以作为通用辅助函数移出类,并按原样使用convert 而不是self.convert 调用。
    猜你喜欢
    • 2021-03-23
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 2018-12-12
    • 2013-04-09
    • 1970-01-01
    • 2020-11-07
    相关资源
    最近更新 更多