【问题标题】:How To make a giveaway Command in Discord.py?如何在 Discord.py 中制作赠品命令?
【发布时间】:2021-07-20 07:55:09
【问题描述】:

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

@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!"
    )
 

有人知道如何解决这个问题吗?

【问题讨论】:

  • 我也想知道是否可以使用 d 表示天 h 数小时等来设置赠品的持续时间。
  • 那是一个单独的问题,但看看this

标签: python command discord.py


【解决方案1】:

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

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

【讨论】:

  • 哦,我也应该使用 new.message 而不是 new_message 吗?
  • 任何有“.”的东西作为变量名无效。确保你的变量名符合python variable naming rules
  • 好的,但是,我已经记下了分钟,但它计算它有秒。我试了 5 分钟,但它只用了 5 秒。
  • 因为你还没有完成转换,变量名什么都不做,它只是一个名字。您必须将秒转换为分钟,只需将分钟乘以 60 并将该值传递给代码
猜你喜欢
  • 2021-03-20
  • 2021-11-01
  • 2021-05-02
  • 2021-06-02
  • 2020-03-17
  • 1970-01-01
  • 2021-04-02
  • 2021-04-18
  • 2021-05-25
相关资源
最近更新 更多