def __init__(self, ctx: discord.ext.commands.Context = None, name=None): self.ctx = ctx self.name = name self.data = None if isinstance(ctx, str): self.filename = f"data/Bot/{self.ctx}.msgpack" try: ensure_file_existence(self.filename) with open(self.filename, "rb") as fp: if fp.read(): fp.seek(0) self.data = msgpack.unpack(fp, encoding="utf-8") else: self.data = {} except FileNotFoundError: self.data = {} else: self.filename = f"data/{self.ctx.guild.id}/{self.name}.msgpack" try: ensure_file_existence(self.filename) with open(self.filename, "rb") as fp: if fp.read(): fp.seek(0) self.data = msgpack.unpack(fp, encoding="utf-8") else: self.data = {} except FileNotFoundError: self.data = {}
def __init__(self, *cogs): ensure_file_existence("config.json") with open("config.json", 'r+') as config_file: if config_file.read(): config_file.seek(0) config = json.load(config_file) else: config = {"discord_token": "none", "command_prefix": "v."} json.dump(config, config_file, indent=4) self.config = config self.bot = commands.Bot(command_prefix=config["command_prefix"]) for cog in cogs: self.bot.add_cog(cog(self))
def __exit__(self, exc_type, exc_value, tb): ensure_file_existence(self.filename) with open(self.filename, "wb") as fp: msgpack.pack(self.data, fp)