python 获取错误-“int”对象没有属性“time”

7ivaypg9  于 2023-01-01  发布在  Python
关注(0)|答案(1)|浏览(254)

我正在尝试为我的机器人程序创建一个免费命令,但每次尝试运行该命令时都会收到错误
第一个月
我的giveaway命令代码

@client.command(description="Starts a giveaway.")
@has_permissions(manage_messages=True)
async def gstart(ctx, time: int, winners: int, *, prize: str):
    global users, new_msg
    try:
        em = discord.Embed(
            title=f"<a:fun:1052215771738165269> {prize} <a:fun:1052215771738165269>",
            color=discord.Colour.random()
        )
        timestamp = time.time() + time
        em.set_footer(text=f"Started by {ctx.author}")
        em.add_field(name=f"** **", value=f"**Ends at**: <t:{int(timestamp)}:f> or <t:{int(timestamp)}:R> \n **Prize**: {prize} \n **Hosted by**: {ctx.author.mention}", inline=False)
        my_msg = await ctx.send(embed=em)
        await my_msg.add_reaction("🎉")
        await asyncio.sleep(time)
        new_msg = await ctx.channel.fetch_message(my_msg.id)
        for i in range(winners):
            users = [user async for user in new_msg.reactions[0].users()]
            users.pop(users.index(client.user))
            winner = random.choice(users)
            await ctx.send(f'Congratulations {winner.mention} won **{prize}**! Hosted by {ctx.author.mention}')
    except Exception as er:
        await ctx.send(er)
lx0bsm1f

lx0bsm1f1#

你必须重命名你的time : int参数,这样它就不会干扰到time模块。考虑到上下文,我建议使用timeUntil这样的参数。
完整代码:

@client.command(description="Starts a giveaway.")
@has_permissions(manage_messages=True)
async def gstart(ctx, timeUntil: int, winners: int, *, prize: str):
    global users, new_msg
    try:
        em = discord.Embed(
            title=f"<a:fun:1052215771738165269> {prize} <a:fun:1052215771738165269>",
            color=discord.Colour.random()
        )
        timestamp = time.time() + timeUntil
        em.set_footer(text=f"Started by {ctx.author}")
        em.add_field(name=f"** **", value=f"**Ends at**: <t:{int(timestamp)}:f> or <t:{int(timestamp)}:R> \n **Prize**: {prize} \n **Hosted by**: {ctx.author.mention}", inline=False)
        my_msg = await ctx.send(embed=em)
        await my_msg.add_reaction("🎉")
        await asyncio.sleep(time)
        new_msg = await ctx.channel.fetch_message(my_msg.id)
        for i in range(winners):
            users = [user async for user in new_msg.reactions[0].users()]
            users.pop(users.index(client.user))
            winner = random.choice(users)
            await ctx.send(f'Congratulations {winner.mention} won **{prize}**! Hosted by {ctx.author.mention}')
    except Exception as er:
        await ctx.send(er)

相关问题