feat: import/export settings, send backup of settings when leaving guild

This commit is contained in:
sam 2024-11-08 17:12:00 +01:00
parent e6d68338db
commit db5d7bb4f8
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
18 changed files with 392 additions and 39 deletions

View file

@ -68,6 +68,12 @@
>
Key roles
</NavLink>
<NavLink
href="/dash/{data.guild.id}/import"
active={$page.url.pathname === `/dash/${data.guild.id}/import`}
>
Import/export settings
</NavLink>
<NavLink
href="/dash/{data.guild.id}/delete"
active={$page.url.pathname === `/dash/${data.guild.id}/delete`}

View file

@ -1,9 +1,10 @@
<script lang="ts">
import { Alert, Button, Input, InputGroup } from "@sveltestrap/sveltestrap";
import { Button, Input, InputGroup } from "@sveltestrap/sveltestrap";
import type { PageData } from "./$types";
import { fastFetch, type ApiError } from "$lib/api";
import apiFetch, { type ApiError } from "$lib/api";
import { addToast } from "$lib/toast";
import { goto } from "$app/navigation";
import { saveAs } from "file-saver";
export let data: PageData;
@ -11,9 +12,16 @@
const deleteData = async () => {
try {
await fastFetch("POST", `/api/guilds/${data.guild.id}/leave`, {
name: guildName,
});
const backup = await apiFetch<any>(
"POST",
`/api/guilds/${data.guild.id}/leave`,
{
name: guildName,
},
);
downloadBackup(backup);
addToast({
header: "Left server",
body: `Successfully left ${data.guild.name} and deleted all data related to it.`,
@ -27,28 +35,31 @@
});
}
};
const downloadBackup = (data: any) => {
const backup = JSON.stringify(data);
const blob = new Blob([backup], { type: "text/plain;charset=utf-8" });
saveAs(blob, "server-backup.json");
};
</script>
<h3>Delete this server's data</h3>
<p>
To make Catalogger leave your server and delete all data from your server,
fill its name in below and press "Delete".
fill its name (<strong>{data.guild.name}</strong>) in below and press
"Delete".
<br />
<strong>
This is irreversible. If you change your mind later, your data cannot be
restored.
</strong>
<br />
If you just want to make Catalogger leave your server but not delete data, simply
kick it via Discord.
You will get a backup of your server's settings which you can restore later if
you change your mind.
</p>
<Alert color="danger">
<h4 class="alert-heading">This is irreversible!</h4>
We <strong>cannot</strong> help you recover data deleted in this way.
</Alert>
<p>
<strong>
Message data is not backed up. If you change your mind, we cannot restore it
for you.
</strong>
</p>
<p>
<InputGroup>

View file

@ -0,0 +1,83 @@
<script lang="ts">
import { Button, ButtonGroup, Input } from "@sveltestrap/sveltestrap";
import type { PageData } from "./$types";
import apiFetch, { fastFetch, type ApiError } from "$lib/api";
import saveAs from "file-saver";
import { addToast } from "$lib/toast";
import { goto } from "$app/navigation";
export let data: PageData;
const exportData = async () => {
try {
const config = await apiFetch<any>(
"GET",
`/api/guilds/${data.guild.id}/config`,
);
downloadBackup(config);
} catch (e) {
addToast({
header: "Error downloading export",
body: (e as ApiError).message || "Unknown error",
});
}
};
let importFiles: FileList | undefined;
const importData = async () => {
if (!importFiles || importFiles.length === 0) return;
const fileData = await importFiles[0].text();
const body = JSON.parse(fileData);
try {
await fastFetch("POST", `/api/guilds/${data.guild.id}/config`, body);
addToast({
header: "Imported data",
body: `Successfully imported the settings for ${data.guild.name}!`,
});
importFiles = undefined;
await goto(`/dash/${data.guild.id}`, { invalidateAll: true });
} catch (e) {
addToast({
header: "Error importing data",
body: (e as ApiError).message || "Unknown error",
});
}
};
const downloadBackup = (data: any) => {
const backup = JSON.stringify(data);
const blob = new Blob([backup], { type: "text/plain;charset=utf-8" });
saveAs(blob, "server-backup.json");
};
</script>
<h3>Import and export settings</h3>
<p>
You can create a backup of your server's configuration here. If you removed
the bot from your server before, you can import the backup you got then here
too.
</p>
<p>
<Input
type="file"
id="import"
bind:files={importFiles}
accept="text/plain, application/json"
/>
</p>
<ButtonGroup>
<Button color="primary" on:click={exportData}>Export settings</Button>
<Button
color="secondary"
on:click={importData}
disabled={!importFiles || importFiles.length === 0}>Import settings</Button
>
</ButtonGroup>