# discord-backup-bot

> A complete framework to facilitate server backup using discord.js v12

Latest version **1.1.2** (published 2019-11-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install discord-backup-bot
pnpm add discord-backup-bot
yarn add discord-backup-bot
bun add discord-backup-bot
```

## Health

**Score 5/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: insecure dependencies; abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2019-11-23 |
| First published | 2019-11-23 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 31.5 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Ney |
| Maintainers | ney_xz |
| Keywords | discord, discord.js, backup, clone, bot, discord-backup |

## Links

- npm: https://www.npmjs.com/package/discord-backup-bot
- npm.io page: https://npm.io/package/discord-backup-bot

## Dependencies (3)

- [fs](https://npm.io/package/fs.md) 0.0.1-security
- [discord.js](https://npm.io/package/discord.js.md) github:discordjs/discord.js#405bdb5b558eff478d0bde739cee1ee8e40c330b
- [randomstring](https://npm.io/package/randomstring.md) ^1.1.5

## Recent versions

- 1.1.2 (latest) — 2019-11-23

## README

# Discord Backup

**Note**: this module uses recent discordjs features and requires discord.js version 12.

Discord Backup is a powerful [Node.js](https://nodejs.org) module that allows you to easily manage discord server backups.

*   Unlimited backups!
*   Backup creation takes less than 10 seconds!
*   Even restores messages with webhooks!
*   And restores everything that is possible to restore (channels, roles, permissions, bans, emojis, name, icon, and more!)

## Installation

```js
npm i discord-backup-bot
```

## Functions

### Create

Create a backup for the server specified in the parameters!

```js
/**
 * @param {object} [Guild] - The discord server you want to backup
 */

const backup = require("discord-backup-bot");
backup.create(Guild).then((backupID) => {
    console.log(backupID); // NSJH2
});
```

### Load

Allows you to load a backup on a Discord server!

```js
/**
 * @param {string} [backupID] - The ID of the backup that you want to load
 * @param {object} [Guild] - The discord server on which you want to load the backup
 */

const backup = require("discord-backup");
backup.load(backupID, Guild).then(() => {
    backup.delete(backupID); // When the backup is loaded, it's recommended to delete it
});
```

### Fetch

Fetches information from a backup

```js
/**
 * @param {string} [backupID] - The ID of the backup to fetch
 */

const backup = require("discord-backup");
backup.fetch(backupID).then((backupInfos) => {
    console.log(backupInfos);
    /*
    {
        ID: "BC5qo",
        guildID: "573098923984551952",
        createdTimestamp: 1559329309168,
        size: "0.05MB",
        data: {backupData}
    }
    */
});
```

### Delete

**Warn**: once the backup is deleted, it is impossible to recover it!

```js
/**
 * @param {string} [backupID] - The ID of the backup to delete
 */

const backup = require("discord-backup-bot");
backup.delete(backupID);
```

### List

**Note**: `backup#list()` simply returns an array of IDs, you must fetch the ID to get complete information.

```js
const backup = require("discord-backup-bot");
backup.list().then((backups) => {
    console.log(backups); // Expected Output [ "BC5qo", "Jdo91", ...]
});
```

### Update storage

```js
const backup = require("discord-backup-bot");
backup.setStorageFolder(__dirname+"/backups/");
await backup.create(guild); // Backup created in ./backups/
backup.setStorageFolder(__dirname+"/my-backups/");
await backup.create(guild); // Backup created in ./my-backups/
```

## Example Bot

```js
// Load modules
const Discord = require("discord.js"),
backup = require("discord-backup-bot"),
client = new Discord.Client(),
settings = {
    prefix: "b!",
    token: "YOURTOKEN"
};

client.on("ready", () => {
    console.log("I'm ready !");
});

client.on("message", async message => {

    // This reads the first part of your message behind your prefix to see which command you want to use.
    let command = message.content.toLowerCase().slice(settings.prefix.length).split(" ")[0];

    // These are the arguments behind the commands.
    let args = message.content.split(' ').slice(1);

    // If the message does not start with your prefix return.
    // If the user that types a message is a bot account return.
    // If the command comes from DM return.
    if (!message.content.startsWith(settings.prefix) || message.author.bot || !message.guild) return;

    if(command === "create"){
        // Check member permissions
        if(!message.member.hasPermission("ADMINISTRATOR")){
            return message.channel.send(":x: | You must be an administrator of this server to request a backup!");
        }
        // Create the backup
        backup.create(message.guild).then((backupID) => {
            // And send informations to the backup owner
            message.author.send("The backup has been created! To load it, type this command on the server of your choice: `"+settings.prefix+"load "+backupID+"`!");
        });
    }

    if(command === "load"){
        // Check member permissions
        if(!message.member.hasPermission("ADMINISTRATOR")){
            return message.channel.send(":x: | You must be an administrator of this server to load a backup!");
        }
        let backupID = args[0];
        if(!backupID){
            return message.channel.send(":x: | You must specify a valid backup ID!");
        }
        // Fetching the backup to know if it exists
        backup.fetch(backupID).then(async () => {
            // If the backup exists, request for confirmation
            message.channel.send(":warning: | When the backup is loaded, all the channels, roles, etc. will be replaced! Type `-confirm` to confirm!");
                await message.channel.awaitMessages(m => (m.author.id === message.author.id) && (m.content === "-confirm"), {
                    max: 1,
                    time: 20000,
                    errors: ["time"]
                }).catch((err) => {
                    // if the author of the commands does not confirm the backup loading
                    return message.channel.send(":x: | Time's up! Cancelled backup loading!");
                });
                // When the author of the command has confirmed that he wants to load the backup on his server
                message.author.send(":white_check_mark: | Start loading the backup!");
                // Load the backup
                backup.load(backupID, message.guild).then(() => {
                    // When the backup is loaded, delete them from the server
                    backup.delete(backupID);
                }).catch((err) => {
                    // If an error occurenced
                    return message.author.send(":x: | Sorry, an error occurenced... Please check that I have administrator permissions!");
                });
        }).catch((err) => {
            // if the backup wasn't found
            return message.channel.send(":x: | No backup found for `"+backupID+"`!");
        });
    }

    if(command === "infos"){
        let backupID = args[0];
        if(!backupID){
            return message.channel.send(":x: | You must specify a valid backup ID!");
        }
        // Fetch the backup
        backup.fetch(backupID).then((backupInfos) => {
            const date = new Date(backupInfos.createdTimestamp);
            const yyyy = date.getFullYear().toString(), mm = (date.getMonth()+1).toString(), dd = date.getDate().toString();
            const formatedDate = `${yyyy}/${(mm[1]?mm:"0"+mm[0])}-${(dd[1]?dd:"0"+dd[0])}`;
            let embed = new Discord.MessageEmbed()
                .setAuthor("Backup Informations")
                // Display the backup ID
                .addField("ID", backupInfos.ID, true)
                // Displays the server from which this backup comes
                .addField("Server", backupInfos.guildID, true)
                // Display the size (in mb) of the backup
                .addField("Size", backupInfos.size, true)
                // Display when the backup was created
                .addField("Created at", formatedDate, true)
                .setColor("#FF0000");
            message.channel.send(embed);
        }).catch((err) => {
            // if the backup wasn't found
            return message.channel.send(":x: | No backup found for `"+backupID+"`!");
        });
    }

});

//Your secret token to log the bot in. (never share this to anyone!)
client.login(settings.token);

function timeConverter(t) {
    var a = new Date(t);
    var today = new Date();
    var yesterday = new Date(Date.now() - 86400000);
    var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var year = a.getFullYear();
    var month = months[a.getMonth()];
    var date = a.getDate();
    var hour = a.getHours();
    var min = a.getMinutes();
    if (a.setHours(0,0,0,0) == today.setHours(0,0,0,0))
        return "today, " + hour + ":" + min;
    else if (a.setHours(0,0,0,0) == yesterday.setHours(0,0,0,0))
        return "yesterday, " + hour + ":" + min;
    else if (year == today.getFullYear())
        return date + " " + month + ", " + hour + ":" + min;
    else
        return date + " " + month + " " + year + ", " + hour + ":" + min;
}
```

---
_Source: https://npm.io/package/discord-backup-bot · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
