backup.djs

Discord.JS V12 | Backup Module

Usage no npm install needed!

<script type="module">
  import backupDjs from 'https://cdn.skypack.dev/backup.djs';
</script>

README

Image Image

Image">

News

- TypeScript Errors Fixed.

Warn

- Discord.JS Version V12 Required.
- Node.JS Version 14 or Higher is Required.

Backup Use

const Discord = require("discord.js");
const client = new Discord.Client();
const {
    createBackup,
    loadBackup,
    deleteBackup,
    fetchBackup,
    backupHas,
    fetchUserBackups,
    setStorageFolder
} = require("backup.djs");

setStorageFolder("./backups"); //set backup storage folder

client.on("ready", () => console.log("Bot Online!"));

client.on("message", async (message) => {
    let prefix = "!";

    if (!message.content.startsWith(prefix) || message.channel.type === "dm" || message.author.bot)
        return;

    let args = message.content.slice(prefix.length).trim().split(/ +/);
    let commandName = args.shift().toLocaleLowerCase();

    if (commandName === "backup") {
        let option = args[0];
        if (!option)
            return message.reply(
                "You must specify an option. **(create, load, info, delete, control)**"
            );

        //backup list
        if (option === "list") {
            const embed = new Discord.MessageEmbed().setColor("RANDOM").setTitle("Your Backups");

            let data = fetchUserBackups(message.author.id);
            //data: [{ ID: Backup ID, data: Backup Data }, {}, {}...]

            data.forEach((backup) => {
                embed.addField(
                    `ID: ${backup.ID}`,
                    `Guild: **${backup.data.guild.name}**\nCreated: **${backup.data.created}**`
                );
            });

            return message.channel.send(embed);
        }

        //backup create
        else if (option === "create") {
            if (!message.member.hasPermission("ADMINISTRATOR"))
                return message.reply("You are not authorized for this.");
            await createBackup(message.guild, message.author.id, {
                doNotBackup: ["bans"] //options: ["bans", "channels", "roles", "emojis"]
            }).then((backupData) => {
                //backupData: { id: backupID, author: backupAuthorID }
                return message.reply(
                    `Backup successfuly created, Backup ID: ${backupData.id}, Backup Author: <@${backupData.author}>`
                );
            });
        }

        //backup load
        else if (option === "load") {
            if (message.author.id !== message.guild.ownerID)
                return message.reply("You are not authorized for this.");
            let backupid = args[1];
            if (!backupid) return message.reply("You must specify a backup ID.");

            await loadBackup(backupid, message.guild, {
                clearGuild: true //true/false
            }).then((data) => {
                if (data === null)
                    return message.reply(
                        "The specified Backup ID could not be found in the database."
                    );
                else if (data !== null) return message.author.send("Backup successfuly loaded.");
            });
        }

        //backup info
        else if (option === "info") {
            let backupid = args[1];
            if (!backupid) return message.reply("You must specify a backup ID.");

            let data = fetchBackup(backupid);
            if (data === null)
                return message.reply("The specified Backup ID could not be found in the database.");
            else if (data !== null) {
                let roles;
                roles = data.roles.map((r) => r.name).join("\n");

                let others;
                others = data.channels.others.map((c) => c.name).join("\n  ") || "\n";

                let categories;
                categories = data.channels.categories
                    .map((c) => `• ${c.name}\n  ${c.children.map((c) => c.name).join("\n  ")}`)
                    .join("\n\n");

                if (roles.length > 1024) {
                    roles = `${roles.slice(0, 300)} ...`;
                }

                if (others.length > 1024) {
                    others = `${others.slice(0, 300)} ...`;
                }

                if (categories.length > 1024) {
                    categories = `${categories.slice(0, 300)} ...`;
                }

                return message.channel
                    .send(
                        new Discord.MessageEmbed()
                            .setColor("RANDOM")
                            .setTitle("Backup Info")
                            .addField("Author", `<@${data.author}>`)
                            .addField("Created", data.created)
                            .addField("Channels", `\`\`\`${others} \n\n${categories}\`\`\``, true)
                            .addField("Roles", `\`\`\`${roles}\`\`\``, true)
                    )
                    .catch((err) => {});
            }
        }

        //backup delete
        else if (option === "delete") {
            let backupid = args[1];
            if (!backupid) return message.reply("You must specify a backup ID.");

            let data = deleteBackup(backupid, message.author.id);
            if (data === null)
                return message.reply("The specified Backup ID could not be found in the database.");
            else if (data === false)
                return message.reply(
                    "You can't delete it because you're not the one creating the backup."
                );
            else if (data === true) return message.reply("Backup successfuly deleted.");
        }

        //backup control
        else if (option === "control") {
            let backupid = args[1];
            if (!backupid) return message.reply("You must specify a backup ID.");

            let data = backupHas(backupid);

            if (data === true) return message.reply("There is such a backup in the database.");
            else if (data === false)
                return message.reply("Such a backup does not exist in the database.");
        } else {
            return message.reply("Invalid option! **(create, load, info, delete, control)**");
        }
    }
});

client.login("BOT TOKEN");

To Report a Bug: Emirhan77#0001 Via Discord