Python Discord.py重写冷却命令
我正在使用Discord.py Rewrite创建一个Discord bot。我有一个名为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
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)}秒内使用它)
参考资料: