【问题标题】:discord.py How to make different jobs with different cooldowns work under !work commanddiscord.py 如何使具有不同冷却时间的不同工作在 !work 命令下工作
【发布时间】:2021-07-05 10:05:10
【问题描述】:
@client.command(aliases = ["Work", "w", "W"]) 
async def work(ctx):
    await open_account(ctx.author)

    users = await getbankdata()

    user = ctx.author

    job = users[str(user.id)]["job"]

    if job == None:
        await ctx.send(f"You have no job! Type !jobs for list of jobs and !getjob *jobname* to get a job.")
    elif job == "youtuber":
        await youtuberwork(ctx, ctx.author)
    elif job == "manager":
        await managerwork(ctx, ctx.author)
    elif job == "receptionist":
        await receptionistwork(ctx, ctx.author)






@client.command()
@commands.is_owner()
@cooldown(1, 18000, BucketType.user)
async def youtuberwork(ctx, user):
    await open_account(ctx.author)

    user = ctx.author

    users = await getbankdata()

    earnings = 120

    users[str(user.id)]["wallet"] += earnings

    await ctx.send(f"You Worked as a YouTuber and gained {earnings} coins!")

    with open("mainbank.json", "w") as f:
        json.dump(users, f)






@client.command()
@commands.is_owner()
@cooldown(1, 19800, BucketType.user)
async def managerwork(ctx, user):

    await open_account(ctx.author)

    users = await getbankdata()

    earnings = 150

    users[str(user.id)]["wallet"] += earnings

    await ctx.send(f"You Worked as a Manager and gained {earnings} coins!")

    with open("mainbank.json", "w") as f:
        json.dump(users, f)



@client.command()
@commands.is_owner()
@cooldown(1, 16200, BucketType.user)
async def receptionistwork(ctx, user):

    await open_account(ctx.author)

    users = await getbankdata()

    earnings = 100

    users[str(user.id)]["wallet"] += earnings

    await ctx.send(f"You Worked as a Receptionist and gained {earnings} coins!")

    with open("mainbank.json", "w") as f:
        json.dump(users, f)

大家好!所以我想让不同的工作在不同的冷却时间下工作!工作命令。我上面给出的代码不起作用。更特殊的冷却是行不通的。它会做它应该做的其他事情(给用户钱并在频道中发送消息)。我试图解决这个问题已经好几个星期了。

【问题讨论】:

标签: discord.py


【解决方案1】:

好的,很清楚为什么它不起作用。 因此,当用户运行命令 !work 时,根据他们的工作,它会运行负责工作的函数。不幸的是,因为用户实际上只运行work 函数,而不是youtuberwork,所以没有应用youtuberwork 的冷却时间。 (用户运行的函数运行的是 youtuberwork,而不是用户,如果用户不使用 !work 但 !youtuberwork 等,它将起作用......)

不幸的是,我无法为您现在拥有的代码提供帮助,我不完全确定如何运行冷却,但您可以做的是让用户必须运行负责用户工作的实际命令。

之前的“解决方案”的替代方法是在工作函数中添加一个检查 - 如果 ctx.author 在最后 X 秒内工作,则通过,否则运行该函数。 尽管如此,我不建议这样做,我真的没有足够的答案可以帮助你。

如前所述,我最好的解决方案是让用户运行实际的命令,这样会更简单。

【讨论】:

    猜你喜欢
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2022-08-02
    • 2021-03-11
    • 2021-05-13
    • 1970-01-01
    • 2021-04-06
    相关资源
    最近更新 更多