Подобно тому, что предложил Патрик Хау, отображение перезарядки хранится в Command._buckets
. Вы можете засолить ведро до запуска бота и сохранить его после его завершения. Для вашего удобства замените класс Bot следующим (a.k.a «moneypatching»):
token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
prefix = "?"
cooldown_info_path = "cd.pkl"
from discord.ext import commands
class Bot(commands.Bot):
async def start(self, *args, **kwargs):
import os, pickle
if os.path.exists(cooldown_info_path): # on the initial run where "cd.pkl" file hadn't been created yet
with open(cooldown_info_path, 'rb') as f:
d = pickle.load(f)
for name, func in self.commands.items():
if name in d: # if the Command name has a CooldownMapping stored in file, override _bucket
self.commands[name]._buckets = d[name]
return await super().start(*args, **kwargs)
async def logout(self):
import pickle
with open(cooldown_info_path, 'wb') as f:
# dumps a dict of command name to CooldownMapping mapping
pickle.dump({name: func._buckets for name, func in self.commands.items()}, f)
return await super().logout()
bot = Bot(prefix)
# everything else as usual
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
@bot.command(pass_context=True)
@commands.cooldown(1, 3600, commands.BucketType.user)
async def hello(ctx):
msg = "Hello... {0.author.mention}".format(ctx.message)
await bot.say(msg)
class ACog:
def __init__(self, bot):
self.bot = bot
@commands.command(pass_context=True)
@commands.cooldown(1, 600, commands.BucketType.user)
async def ping(self, ctx):
msg = "Pong {0.author.mention}".format(ctx.message)
await self.bot.say(msg)
bot.add_cog(ACog(bot))
bot.run(token)
Это сохранит данные перезарядки в "cd.pkl", когда бот выйдет из системы.