python - 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}!**")
如何将获胜者添加为选项?
解决方案
我最终这样做了:
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}!**")
对于您选择的获胜者数量,它将随机选择一名获胜者并提及他们。它每条消息都提到一个人,因此如果您选择很多获胜者,它可能会导致垃圾邮件。随时提出修改建议。
推荐阅读
- google-cloud-platform - Terraform:无法与谷歌云平台基础设施通信
- r - 如何从 R 中的 tidygraph 对象列表中操作 tidygraph 对象?
- php - 复选框过滤器 Laravel livewire
- pandas - 如何使用 Group by 和 Order By 在 SQL 的 where 子句(列是字符串类型)中传递 Dataframe 列
- python - 将 docker 容器 ls -q 输出转换为 python 列表
- regex - 正则表达式到底是如何工作的?教授根本没有解释
- linux - 在 QSPI(Spansion s25hs512t)上写入失败且没有错误 - u-boot
- python - 如何在redis中存储特殊字符(例如“//$//”)作为使用python的键的值?
- django - 使用 Nginx 和 uWSGI 连接到上游时出现错误 abot failed (13: Permission denied)
- opengl - glsl 着色器中采样器的限制(绑定与访问)