首页 > 解决方案 > Discord.py 赠品命令

问题描述

我正在尝试使用 discord.py 创建赠品命令。我的代码有效,但我正在寻找一种方法来添加一些获胜者,例如,如果你把两个作为获胜者的数量,它会选择两个人,如果这有意义的话。这是我当前的代码:

time_regex = re.compile(r"(?:(\d{1,5})(h|s|m|d))+?")
time_dict = {"h": 3600, "s": 1, "m": 60, "d": 86400}

def convert(argument):
    args = argument.lower()
    matches = re.findall(time_regex, args)
    time = 0
    for key, value in matches:
        try:
            time += time_dict[value] * float(key)
        except KeyError:
            raise commands.BadArgument(
                f"{value} is an invalid time key! h|m|s|d are valid arguments"
            )
        except ValueError:
            raise commands.BadArgument(f"{key} is not a number!")
    return round(time)

@client.command()
@commands.has_role(': ̗̀➛ Giveaway Hosts')
async def start(ctx, timing, *, prize):
  await ctx.send('Okay, making a giveaway!', delete_after=3)
  gwembed = discord.Embed(
    title=" __**Giveaway**__ ",
    description=f'Prize: {prize}',
  )
  time = convert(timing)
  gwembed.set_footer(text=f"This giveaway ends {time} seconds from this message.")
  gwembed = await ctx.send(embed=gwembed)
  await gwembed.add_reaction("")
  await asyncio.sleep(time)
  message = await ctx.fetch_message(gwembed.id)
  users = await message.reactions[0].users().flatten()
  users.pop(users.index(ctx.guild.me))
  if len(users) == 0:
    await ctx.send("No winner was decided.")
    return
  winner = random.choice(users)
  await ctx.send(f"**Congrats {winner.mention}!**")

如何将获胜者添加为选项?

标签: pythondiscorddiscord.pydiscord.py-rewrite

解决方案


我最终这样做了:

time_regex = re.compile(r"(?:(\d{1,5})(h|s|m|d))+?")
time_dict = {"h": 3600, "s": 1, "m": 60, "d": 86400}

def convert(argument):
    args = argument.lower()
    matches = re.findall(time_regex, args)
    time = 0
    for key, value in matches:
        try:
            time += time_dict[value] * float(key)
        except KeyError:
            raise commands.BadArgument(
                f"{value} is an invalid time key! h|m|s|d are valid arguments"
            )
        except ValueError:
            raise commands.BadArgument(f"{key} is not a number!")
    return round(time)

@client.command()
@commands.has_role(': ̗̀➛ Giveaway Hosts')
async def start(ctx, timing, winners: int, *, prize):
  await ctx.send('Okay, making a giveaway!', delete_after=3)
  gwembed = discord.Embed(
    title=" __**Giveaway**__ ",
    description=f'Prize: {prize}',
    color=0xb4e0fc
  )
  time = convert(timing)
  gwembed.set_footer(text=f"This giveaway ends {time} seconds from this message.")
  gwembed = await ctx.send(embed=gwembed)
  await gwembed.add_reaction("")
  await asyncio.sleep(time)
  message = await ctx.fetch_message(gwembed.id)
  users = await message.reactions[0].users().flatten()
  users.pop(users.index(ctx.guild.me))
  if len(users) == 0:
    await ctx.send("No winner was decided.")
    return
  for i in range(winners):
    winner = random.choice(users)
    await ctx.send(f"**Congrats to: {winner}!**")

在这部分中,选择多个获胜者的部分:

  for i in range(winners):
    winner = random.choice(users)
    await ctx.send(f"**Congrats to: {winner.mention}!**")

对于您选择的获胜者数量,它将随机选择一名获胜者并提及他们。它每条消息都提到一个人,因此如果您选择很多获胜者,它可能会导致垃圾邮件。随时提出修改建议。


推荐阅读