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

@ -14,12 +14,15 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using System.Net;
using System.Text;
using System.Text.Json;
using Catalogger.Backend.Api.Middleware;
using Catalogger.Backend.Bot;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
using Dapper;
using Microsoft.AspNetCore.Mvc;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.Extensions.Embeds;
namespace Catalogger.Backend.Api;
@ -40,6 +43,8 @@ public partial class GuildsController
}
var guildConfig = await guildRepository.GetAsync(guildId);
var export = await ToExport(guildConfig);
var logChannelId =
webhookExecutor.GetLogChannel(guildConfig, LogChannelType.GuildUpdate)
?? webhookExecutor.GetLogChannel(guildConfig, LogChannelType.GuildMemberRemove);
@ -50,15 +55,25 @@ public partial class GuildsController
var embed = new EmbedBuilder()
.WithTitle("Catalogger is leaving this server")
.WithDescription(
$"A moderator in this server ({currentUser.Tag}, <@{currentUser.Id}>) requested that Catalogger leave. "
+ "All data related to this server will be deleted."
$"""
A moderator in this server ({currentUser.Tag}, <@{currentUser.Id}>) requested that Catalogger leave.
All data related to this server will be deleted.
A backup of this server's configuration is attached to this message,
in case you want to use the bot again later.
"""
)
.WithColour(DiscordUtils.Red)
.WithCurrentTimestamp()
.Build()
.GetOrThrow();
await webhookExecutor.SendLogAsync(logChannelId.Value, [embed], []);
var exportData = JsonSerializer.Serialize(export, JsonUtils.ApiJsonOptions);
var file = new FileData(
"config-backup.json",
new MemoryStream(Encoding.UTF8.GetBytes(exportData))
);
await webhookExecutor.SendLogAsync(logChannelId.Value, [embed], [file]);
}
else
{
@ -125,7 +140,7 @@ public partial class GuildsController
_logger.Information("Left guild {GuildId} and removed all data for it", guildId);
return NoContent();
return Ok(export);
}
public record LeaveGuildRequest(string Name);