ฉันพยายามเปลี่ยน lib (core.py
) และเปลี่ยนคำสั่งคูลดาวน์เป็นดังนั้นคำสั่งทั้งหมดจะแชร์ 1 คูลดาวน์ เช่น หลังจากใช้ !hi
แล้ว !bye
ก็จะถูกคูลดาวน์แทนเพียง !hi
:
def shared_cooldown(rate, per, type=BucketType.default):
cooldown = Cooldown(rate, per, type=BucketType.default)
def decorator(func):
if isinstance(func, Command):
func._buckets = CooldownMapping(cooldown)
else:
func.__commands_cooldown__ = cooldown
return func
return decorator
แทนสิ่งนี้:
def cooldown(rate, per, type=BucketType.default):
"""A decorator that adds a cooldown to a :class:`.Command`
or its subclasses.
A cooldown allows a command to only be used a specific amount
of times in a specific time frame. These cooldowns can be based
either on a per-guild, per-channel, per-user, or global basis.
Denoted by the third argument of ``type`` which must be of enum
type ``BucketType`` which could be either:
- ``BucketType.default`` for a global basis.
- ``BucketType.user`` for a per-user basis.
- ``BucketType.guild`` for a per-guild basis.
- ``BucketType.channel`` for a per-channel basis.
- ``BucketType.member`` for a per-member basis.
- ``BucketType.category`` for a per-category basis.
If a cooldown is triggered, then :exc:`.CommandOnCooldown` is triggered in
:func:`.on_command_error` and the local error handler.
A command can only have a single cooldown.
Parameters
------------
rate: :class:`int`
The number of times a command can be used before triggering a cooldown.
per: :class:`float`
The amount of seconds to wait for a cooldown when it's been triggered.
type: ``BucketType``
The type of cooldown to have.
"""
def decorator(func):
if isinstance(func, Command):
func._buckets = CooldownMapping(Cooldown(rate, per, type))
else:
func.__commands_cooldown__ = Cooldown(rate, per, type)
return func
return decorator
ในโค้ดบอทของฉัน ฉันเพิ่มบรรทัดนี้: my_cooldown = commands.shared_cooldown(1, 600, commands.shared_cooldown)
และ 2 คำสั่ง:
@bot.command(name ="hi")
@my_cooldown
async def hi(ctx):
site = ["https://google.com", "https://youtube.com"]
chosen = random.choice(site)
url = chosen
embed = discord.Embed(title="Your site", description=f"[Click Here]({url})", color=0x00ff00)
if ctx.message.guild == None:
await ctx.author.send('You can not use this command in your DM!')
pass
else:
await ctx.author.send(embed=embed)
@bot.command(name ="bye")
@my_cooldown
async def bye(ctx):
site = ["https://google.com", "https://youtube.com"]
chosen = random.choice(site)
url = chosen
embed = discord.Embed(title="Your site", description=f"[Click Here]({url})", color=0x00ff00)
if ctx.message.guild == None:
await ctx.author.send('You can not use this command in your DM!')
pass
else:
await ctx.author.send(embed=embed)
ตอนนี้ ปัญหาคือคูลดาวน์ของฉันใช้ได้กับทุกคำสั่ง แต่ผู้ใช้ทุกคนจะได้รับคูลดาวน์หากมีคนคนหนึ่งใช้คำสั่ง ฉันพยายามเปลี่ยน def shared_cooldown(rate, per, type=BucketType.default):
cooldown = Cooldown(rate, per, type=BucketType.default)
เป็น def shared_cooldown(rate, per, type=BucketType.user):
cooldown = Cooldown(rate, per, type=BucketType.user)
แต่แล้วผู้คนก็คูลดาวน์สำหรับคำสั่งเดียวที่พวกเขาใช้ แทนที่จะเป็นคำสั่งทั้งหมด ฉันจะทำให้เฉพาะผู้ใช้ที่ใช้คำสั่งแทนทุกคน แต่ยังคงคูลดาวน์คำสั่งทั้งหมดสำหรับผู้ใช้รายนั้นได้อย่างไร