栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

仅在满足条件时才启动discord.py命令冷却

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

仅在满足条件时才启动discord.py命令冷却

可以使用多种方法来制作自己的冷却时间,这里有一个简单的方法可以解决问题。其背后的想法是让机器人“记住”上次有人使用此特定命令的时间,并在允许玩家移动之前对其进行检查。

from datetime import datetime, timedeltaon_cooldown = {} # Dictionary with user IDs as keys and datetime as valuesdestinations=["d1", "d2", "d3"] # List of valid arguments for the commandmove_cooldown = 5 # cooldown of the move command in seconds@bot.command()async def move(ctx, destination):    if destination in destinations:        author = ctx.author.id        try: # calculate the amount of time since the last (successful) use of the command last_move = datetime.now() - on_cooldown[author]         except KeyError: # the key doesn't exist, the player used the command for the first time # or the bot has been shut down since last_move = None on_cooldown[author] = datetime.now()        if last_move is None or last_move.seconds > move_cooldown: # move(...) on_cooldown[author] = datetime.now() # the player successfully moved so we start his cooldown again await ctx.send("You moved!")        else: await ctx.send("You're still on cooldown.")    else:        await ctx.send("This is not a valid destination")

注意:您可以在

@bot.command
装饰器后删除括号,也可以不必删除括号。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625621.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号