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

@ -137,6 +137,23 @@ public class GuildRepository(ILogger logger, DatabaseConnection conn)
new { Id = id.Value, Channels = config }
);
public async Task ImportConfigAsync(
ulong id,
Guild.ChannelConfig channels,
string[] bannedSystems,
ulong[] keyRoles
) =>
await conn.ExecuteAsync(
"update guilds set channels = @channels::jsonb, banned_systems = @bannedSystems, key_roles = @keyRoles where id = @id",
new
{
id,
channels,
bannedSystems,
keyRoles,
}
);
public void Dispose()
{
conn.Dispose();

View file

@ -65,6 +65,34 @@ public class InviteRepository(ILogger logger, DatabaseConnection conn)
new { GuildId = guildId.Value, Code = code }
);
/// <summary>
/// Bulk imports an array of invite codes and names.
/// The GuildId property in the Invite object is ignored.
/// </summary>
public async Task ImportInvitesAsync(Snowflake guildId, IEnumerable<Invite> invites)
{
await using var tx = await conn.BeginTransactionAsync();
foreach (var invite in invites)
{
await conn.ExecuteAsync(
"""
insert into invites (code, guild_id, name)
values (@Code, @GuildId, @Name) on conflict (code)
do update set name = @Name
""",
new
{
GuildId = guildId.Value,
invite.Code,
invite.Name,
},
transaction: tx
);
}
await tx.CommitAsync();
}
public void Dispose()
{
conn.Dispose();

View file

@ -70,6 +70,39 @@ public class WatchlistRepository(ILogger logger, DatabaseConnection conn)
)
) != 0;
/// <summary>
/// Bulk imports an array of watchlist entries.
/// The GuildId property in the Watchlist object is ignored.
/// </summary>
public async Task ImportWatchlistAsync(Snowflake guildId, IEnumerable<Watchlist> watchlist)
{
await using var tx = await conn.BeginTransactionAsync();
foreach (var entry in watchlist)
{
await conn.ExecuteAsync(
"""
insert into watchlists (guild_id, user_id, added_at, moderator_id, reason)
values (@GuildId, @UserId, @AddedAt, @ModeratorId, @Reason)
on conflict (guild_id, user_id) do update
set added_at = @AddedAt,
moderator_id = @ModeratorId,
reason = @Reason
""",
new
{
GuildId = guildId.Value,
entry.UserId,
entry.AddedAt,
entry.ModeratorId,
entry.Reason,
},
transaction: tx
);
}
await tx.CommitAsync();
}
public void Dispose()
{
conn.Dispose();