Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Discord.py重写冷却命令_Python_Python 3.x_Discord_Discord.py_Discord.py Rewrite - Fatal编程技术网

Python Discord.py重写冷却命令

Python Discord.py重写冷却命令,python,python-3.x,discord,discord.py,discord.py-rewrite,Python,Python 3.x,Discord,Discord.py,Discord.py Rewrite,我正在使用Discord.py Rewrite创建一个Discord bot。我有一个名为work的命令,代码如下: import discord from discord.ext import commands client = commands.Bot(command_prefix="c!") # on_ready command @client.command() async def work(ctx): author_id = ctx.author.id

我正在使用Discord.py Rewrite创建一个Discord bot。我有一个名为
work
的命令,代码如下:

import discord
from discord.ext import commands

client = commands.Bot(command_prefix="c!")
# on_ready command

@client.command()
async def work(ctx):
    author_id = ctx.author.id
    currency_dict[author_id] += 50
    await ctx.send("You Gained 50 Coins")
# currency_dict is a dictionary that is already defined

有没有办法让用户每30秒只能使用一次命令?谢谢

首先,您必须从discord.ext.commands.cooldowns导入
,导入BucketType
。这将帮助你冷却。下面是可用于此导入的
冷却时间检查和
最大并发性检查

从discord.ext.commands.cooldowns导入BucketType
#BucketType可以是BucketType.default、成员、用户、公会、角色或频道
@命令。冷却时间(速率,每,BucketType)
#限制可以使用命令的频率(每分钟数、秒数、BucketType)
@commands.max_并发性(number,per=BucketType.default,*,wait=False)
#限制同时可以运行的命令实例数。
#设置wait=True将使其他命令排队。False将引发MaxConcurrencyReached
#检查可以堆叠,如果任何检查失败,将引发检查失败。
在您的情况下,您可能需要使用
命令。冷却时间(速率、每分钟、BucketType)

导入不一致
从discord.ext导入命令
从discord.ext.commands.cooldowns导入BucketType
client=commands.Bot(command_prefix=“c!”)
#on_ready命令
@client.command()
@命令。冷却时间(1,30,commands.BucketType.user)#每30秒一个命令,每个用户
异步def工作(ctx):
author\u id=ctx.author.id
货币目录[作者id]+=50
等待ctx.send(“您获得50枚硬币”)
#currency_dict是一本已经定义的词典
#冷却错误处理
@工作错误
异步def工作错误(ctx,错误):
如果isinstance(错误,commands.CommandOnColdDown):
等待ctx.send(如果该命令处于冷却状态,您可以在{round(error.retry\u after,2)}秒内使用它)

参考资料:


首先,您必须从discord.ext.commands.cooldowns导入
,导入BucketType
。这将帮助你冷却。下面是可用于此导入的
冷却时间检查和
最大并发性检查

从discord.ext.commands.cooldowns导入BucketType
#BucketType可以是BucketType.default、成员、用户、公会、角色或频道
@命令。冷却时间(速率,每,BucketType)
#限制可以使用命令的频率(每分钟数、秒数、BucketType)
@commands.max_并发性(number,per=BucketType.default,*,wait=False)
#限制同时可以运行的命令实例数。
#设置wait=True将使其他命令排队。False将引发MaxConcurrencyReached
#检查可以堆叠,如果任何检查失败,将引发检查失败。
在您的情况下,您可能需要使用
命令。冷却时间(速率、每分钟、BucketType)

导入不一致
从discord.ext导入命令
从discord.ext.commands.cooldowns导入BucketType
client=commands.Bot(command_prefix=“c!”)
#on_ready命令
@client.command()
@命令。冷却时间(1,30,commands.BucketType.user)#每30秒一个命令,每个用户
异步def工作(ctx):
author\u id=ctx.author.id
货币目录[作者id]+=50
等待ctx.send(“您获得50枚硬币”)
#currency_dict是一本已经定义的词典
#冷却错误处理
@工作错误
异步def工作错误(ctx,错误):
如果isinstance(错误,commands.CommandOnColdDown):
等待ctx.send(如果该命令处于冷却状态,您可以在{round(error.retry\u after,2)}秒内使用它)

参考资料: