Exemplo n.º 1
0
    async def load(self, ctx, name, *options):
        """
        Load a template


        __Arguments__

        **name**: The name of the template
        **options**: A list of options (See examples)


        __Examples__

        Default options: ```{b.prefix}template load starter```
        Only roles: ```{b.prefix}template load starter !* roles```
        Everything but bans: ```{b.prefix}template load starter !bans```
        """
        template = await ctx.client.db.templates.find_one({"_id": name})
        if template is None:
            raise ctx.f.ERROR(
                f"There is **no template** with the name `{name}`.")

        warning_msg = await ctx.f_send(
            "Are you sure that you want to load this template?\n"
            "__**All channels and roles will get replaced!**__",
            f=ctx.f.WARNING)
        reactions = ("✅", "❌")
        for reaction in reactions:
            await ctx.client.add_reaction(warning_msg, reaction)

        try:
            data, = await ctx.client.wait_for(
                "message_reaction_add",
                ctx.shard_id,
                check=lambda d: d["message_id"] == warning_msg.id and d[
                    "user_id"] == ctx.author.id and d["emoji"]["name"
                                                               ] in reactions,
                timeout=60)
        except asyncio.TimeoutError:
            await ctx.client.delete_message(warning_msg)
            return

        await ctx.client.delete_message(warning_msg)
        if data["emoji"]["name"] != "✅":
            return

        guild = await ctx.get_full_guild()
        backup = BackupLoader(ctx.client,
                              guild,
                              template["data"],
                              reason="Template loaded by " + str(ctx.author))

        options = list(options)
        options.append("!settings")
        await backup.load(**utils.backup_options(options))
Exemplo n.º 2
0
    async def load(self, ctx, backup_id, *options):
        """
        Load a backup


        __Arguments__

        **backup_id**: The id of the backup or the guild id of the latest automated backup
        **options**: A list of options (See examples)


        __Examples__

        Default options: ```{b.prefix}backup load oj1xky11871fzrbu```
        Only roles: ```{b.prefix}backup load oj1xky11871fzrbu !* roles```
        Everything but bans: ```{b.prefix}backup load oj1xky11871fzrbu !bans```
        """
        backup_d = await ctx.client.db.backups.find_one({"_id": backup_id, "creator": ctx.author.id})
        if backup_d is None:
            raise ctx.f.ERROR(f"You have **no backup** with the id `{backup_id}`.")

        warning_msg = await ctx.f_send("Are you sure that you want to load this backup?\n"
                                       "__**All channels and roles will get replaced!**__", f=ctx.f.WARNING)
        reactions = ("✅", "❌")
        for reaction in reactions:
            await ctx.client.add_reaction(warning_msg, reaction)

        try:
            data, = await ctx.client.wait_for(
                "message_reaction_add",
                ctx.shard_id,
                check=lambda d: d["message_id"] == warning_msg.id and
                                d["user_id"] == ctx.author.id and
                                d["emoji"]["name"] in reactions,
                timeout=60
            )
        except asyncio.TimeoutError:
            await ctx.client.delete_message(warning_msg)
            return

        await ctx.client.delete_message(warning_msg)
        if data["emoji"]["name"] != "✅":
            return

        guild = await ctx.get_full_guild()
        backup = BackupLoader(ctx.client, guild, backup_d["data"], reason="Backup loaded by " + str(ctx.author))
        await backup.load(**utils.backup_options(options))
Exemplo n.º 3
0
    async def load(self, ctx, name, *options):
        """
        Load a template

        You can find more help on the [wiki](https://wiki.xenon.bot/templates#loading-a-template).


        __Arguments__

        **name**: The name of the template
        **options**: A list of options (See examples)


        __Examples__

        Default options: ```{b.prefix}template load starter```
        Only roles: ```{b.prefix}template load starter !* roles```
        Everything but bans: ```{b.prefix}template load starter !bans```
        """
        template = await ctx.client.mongo.dtpl.templates.find_one_and_update(
            {
                "internal": True,
                "$or": [{
                    "name": name
                }, {
                    "_id": name
                }]
            }, {"$inc": {
                "usage_count": 1
            }})
        if template is None:
            template = await self._crossload_template(name)

        if template is None:
            raise ctx.f.ERROR(
                f"There is **no template** with the name `{name}`.")

        warning_msg = await ctx.f_send(
            "Are you sure that you want to load this template?\n"
            f"Please put the managed role called `{ctx.bot.user.name}` above all other "
            f"roles before clicking the ✅ reaction.\n\n"
            "__**All channels and roles will get replaced!**__\n\n"
            "*Also keep in mind that you can only load up to 250 roles per day.*",
            f=ctx.f.WARNING)

        reactions = ("✅", "❌")
        for reaction in reactions:
            await ctx.client.add_reaction(warning_msg, reaction)

        try:
            data, = await ctx.client.wait_for(
                "message_reaction_add",
                ctx.shard_id,
                check=lambda d: d["message_id"] == warning_msg.id and d[
                    "user_id"] == ctx.author.id and d["emoji"]["name"
                                                               ] in reactions,
                timeout=60)
        except asyncio.TimeoutError:
            await ctx.client.delete_message(warning_msg)
            return

        await ctx.client.delete_message(warning_msg)
        if data["emoji"]["name"] != "✅":
            return

        guild = await ctx.get_full_guild()
        backup = BackupLoader(ctx.client,
                              guild,
                              template["data"],
                              reason="Template loaded by " + str(ctx.author))

        options = list(options)
        options.extend(["!settings", "!members"])
        await self.client.redis.publish(
            "loaders:start",
            msgpack.packb({
                "id": ctx.guild_id,
                "type": "template",
                "source_id": str(backup.data["id"]),
                "template_id": name.strip("/").split("/")[-1]
            }))
        try:
            await backup.load(**utils.backup_options(options))

            unpacked_ids = {
                f"ids.{s}": t
                for s, t in backup.id_translator.items()
            }
            await ctx.bot.db.id_translators.update_one(
                {
                    "target_id": ctx.guild_id,
                    "source_id": backup.data["id"],
                }, {
                    "$set": {
                        "target_id": ctx.guild_id,
                        "source_id": backup.data["id"],
                        **unpacked_ids
                    },
                    "$addToSet": {
                        "loaders": ctx.author.id
                    }
                },
                upsert=True)
        finally:
            await self.client.redis.publish(
                "loaders:done",
                msgpack.packb({
                    "id": ctx.guild_id,
                    "type": "template",
                    "source_id": str(backup.data["id"]),
                    "template_id": name.strip("/").split("/")[-1]
                }))
Exemplo n.º 4
0
    async def load(self, ctx, backup_id: str.lower, *options):
        """
        Load a backup
        
        Get more help on the [wiki](https://wiki.xenon.bot/backups#loading-a-backup).


        __Arguments__

        **backup_id**: The id of the backup
        **options**: A list of options (See examples)


        __Examples__

        Default options: ```{b.prefix}backup load oj1xky11871fzrbu```
        Only roles: ```{b.prefix}backup load oj1xky11871fzrbu !* roles```
        Everything but bans: ```{b.prefix}backup load oj1xky11871fzrbu !bans```
        """
        backup_d = await ctx.client.db.backups.find_one({
            "_id":
            backup_id,
            "creator":
            ctx.author.id
        })
        if backup_d is None:
            raise ctx.f.ERROR(
                f"You have **no backup** with the id `{backup_id.upper()}`.")

        warning_msg = await ctx.f_send(
            "Are you sure that you want to load this backup?\n"
            f"Please put the managed role called `{ctx.bot.user.name}` above all other "
            f"roles before clicking the ✅ reaction.\n\n"
            "__**All channels and roles will get replaced!**__\n\n"
            "*Also keep in mind that you can only load up to 250 roles per 48 hours.*",
            f=ctx.f.WARNING)
        reactions = ("✅", "❌")
        for reaction in reactions:
            await ctx.client.add_reaction(warning_msg, reaction)

        try:
            data, = await ctx.client.wait_for(
                "message_reaction_add",
                ctx.shard_id,
                check=lambda d: d["message_id"] == warning_msg.id and d[
                    "user_id"] == ctx.author.id and d["emoji"]["name"
                                                               ] in reactions,
                timeout=60)
        except asyncio.TimeoutError:
            await ctx.client.delete_message(warning_msg)
            return

        await ctx.client.delete_message(warning_msg)
        if data["emoji"]["name"] != "✅":
            return

        guild = await ctx.fetch_full_guild()
        backup = BackupLoader(ctx.client,
                              guild,
                              backup_d["data"],
                              reason="Backup loaded by " + str(ctx.author))

        await self.client.redis.publish(
            "loaders:start",
            msgpack.packb({
                "id": ctx.guild_id,
                "type": "backup",
                "user_id": ctx.author.id,
                "source_id": backup.data["id"],
                "backup_id": backup_id
            }))
        try:
            await backup.load(**utils.backup_options(options))

            unpacked_ids = {
                f"ids.{s}": t
                for s, t in backup.id_translator.items()
            }
            await ctx.bot.db.id_translators.update_one(
                {
                    "target_id": ctx.guild_id,
                    "source_id": backup.data["id"],
                }, {
                    "$set": {
                        "target_id": ctx.guild_id,
                        "source_id": backup.data["id"],
                        **unpacked_ids
                    },
                    "$addToSet": {
                        "loaders": ctx.author.id
                    }
                },
                upsert=True)
        finally:
            await self.client.redis.publish(
                "loaders:done",
                msgpack.packb({
                    "id": ctx.guild_id,
                    "type": "backup",
                    "user_id": ctx.author.id,
                    "source_id": backup.data["id"],
                    "backup_id": backup_id
                }))
            await ctx.bot.create_audit_log(utils.AuditLogType.BACKUP_LOAD,
                                           [ctx.guild_id], ctx.author.id)