【问题标题】:Giveaway command time converting error Discord.Py赠品命令时间转换错误 Discord.Py
【发布时间】:2021-06-02 13:20:54
【问题描述】:

这是我的代码,时间没有被转换,我不知道该怎么办了。如果您知道该怎么做,请告诉我该怎么做

这是我目前得到的:


def convert(time):
  pos = ["s","m","h","d"]

  time_dict = {"s" : 1, "m" : 60, "h" : 3600, "d": 3600*24}

  unit = time[-1]

  if unit not in pos:
    return -1
  try:
    val = int(time[:-1])
  except:
    return -2

  return val * time_dict[unit]

#---------------------------------------------------------------------------


    
@client.command()
@commands.has_permissions(manage_messages  = True)
async def giveaway(ctx, time : str, *, prize: str):

    embed = discord.Embed(title=prize,
                          description=f"Hosted by - {ctx.author.mention}\nReact with :tada: to enter!\nTime Remaining: **{time}** seconds",
                          color=ctx.guild.me.top_role.color, )

    msg = await ctx.channel.send(content=":tada: **GIVEAWAY** :tada:", embed=embed)
    await msg.add_reaction("????")
    await asyncio.sleep(3)
    new_msg = await ctx.channel.fetch_message(msg.id)

    user_list = [u for u in await new_msg.reactions[0].users().flatten() if u != client.user] # Check the reactions/don't count the bot reaction

    if len(user_list) == 0:
        await ctx.send("No one reacted.") 
    else:
        winner = random.choice(user_list)
        await ctx.send(f"{winner.mention} You have won the {prize}!")         

当我输入 2m 表示 2 分钟时,它显示剩余 2m 秒,现在我知道为什么它显示秒了,因为我还没有更新响应,但时间只有 2 秒加上 3 秒延迟时间。基本上总共大约 6 秒。

我只是从堆栈溢出中抛出了 2 个命令,这就像放入兰博基尼头垫和道奇发动机缸体,我知道它不应该工作,即使稍作修改,我也确实看到了什么现在错了,但我不知道如何解决它

【问题讨论】:

  • 我不知道为什么,但你只做了await asyncio.sleep(3)。你不应该在await asyncio.sleep(3) 之后添加await asyncio.sleep(int(convert(time))) 吗?
  • 我刚刚编辑它,因为我刚刚意识到时间转换器与代码本身不同

标签: python discord.py


【解决方案1】:

你可以用它来转换时间。

import re
from discord.ext.commands import BadArgument

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:
      raise BadArgument
  return round(time)

【讨论】:

  • 谢谢,但我这样做了,它不知道正则表达式是什么,谢谢你的帮助
  • re.findall 与 str.index 类似,但更复杂、更有用。
【解决方案2】:

所以我修改了您的代码并稍微更改了giveaway 命令。经过一些修改后,该命令对我来说应该可以正常工作。这是我重新定义它的方式:

def convert(time):
    pos = ["s", "m", "h", "d"]

    time_dict = {"s": 1, "m": 60, "h": 3600, "d": 3600 * 24}

    unit = time[-1]

    if unit not in pos:
        return -1
    try:
        val = int(time[:-1])
    except:
        return -2

    return val * time_dict[unit]


# ---------------------------------------------------------------------------


@client.command()
@commands.has_permissions(manage_messages=True)
async def giveaway(ctx, time: str, *, prize: str):
    time = convert(time)

    embed = discord.Embed(title=prize,
                          description=f"Hosted by - {ctx.author.mention}\nReact with :tada: to enter!\nTime Remaining: **{time}** seconds",
                          color=ctx.guild.me.top_role.color)

    msg = await ctx.channel.send(content=":tada: **GIVEAWAY** :tada:", embed=embed)
    await msg.add_reaction("?")

    await asyncio.sleep(3)
    await asyncio.sleep(int(time))

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

    user_list = [user for user in await new_msg.reactions[0].users().flatten() if
                 user != client.user]  # Check the reactions/don't count the bot reaction

    if len(user_list) == 0:
        await ctx.send("No one reacted.")
    else:
        winner = random.choice(user_list)
        await ctx.send(f"{winner.mention} You have won the {prize}!")

【讨论】:

    猜你喜欢
    • 2021-03-20
    • 2021-05-02
    • 1970-01-01
    • 2021-11-01
    • 2021-07-20
    • 2021-07-11
    • 2020-03-17
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多