首页 > 解决方案 > 如何在 Discord.py 中制作赠品命令?

问题描述

因此,我尝试将此代码用于赠品命令,但由于某种原因,它从未显示用户输入的反应。我尝试更改赠品图标,也许重新运行代码,但没有运气。

@client.command()
@commands.has_role("Admin")
async def gstart(ctx, mins: int, *, prize: str):
    embed = discord.Embed(title="Giveaway!",
                          description=f"{prize}",
                          color=ctx.author.color)

    end = datetime.datetime.utcnow() + datetime.timedelta(seconds=mins * 60)

    embed.add_field(name="Ends At:", value=f"{end} UTC")

    my.msg = await ctx.send(embed=embed)
    await my.msg.add_reaction("")

    await asyncio.sleep(mins)

    new_msg = await ctx.channel.fetch_message(my_msg.id)

    users = await new_msg.reactions[0].users().flatten()

    users.pop(users.index(client.user))

    winner = random.choice(users)

    await ctx.send(f"Congratulations! {winner.mention} won {prize}!")


@client.command()
@commands.has_permissions(kick_members=True)
async def reroll(ctx, channel: discord.TextChannel, id_: int):
    try:
        new_msg = await channel.fetch_message(id_)
    except:
        await ctx.send(
            "The ID that was entered was incorrect, make sure you have entered the correct giveaway message ID."
        )
    users = await new_msg.reactions[0].users().flatten()
    users.pop(users.index(client.user))

    winner = random.choice(users)

    await channel.send(
        f"Congratulations the new winner is: {winner.mention} for the giveaway rerolled!"
    )
 

有人知道怎么修这个东西吗?

标签: pythoncommanddiscord.py

解决方案


我注意到您将“my.msg”作为变量名,官方 python 文档不允许您使用名称中的点创建变量,因为这是保留关键字。我可以建议改为:

my_msg = await ctx.send(embed=embed)
await my_msg.add_reaction("")

推荐阅读