diff --git a/PluralKit.Bot/Commands/ModCommands.cs b/PluralKit.Bot/Commands/ModCommands.cs index 24355393..996886da 100644 --- a/PluralKit.Bot/Commands/ModCommands.cs +++ b/PluralKit.Bot/Commands/ModCommands.cs @@ -22,14 +22,16 @@ namespace PluralKit.Bot.Commands public async Task SetLogChannel(Context ctx) { - ctx.CheckAuthorPermission(GuildPermission.ManageGuild, "Manage Server").CheckGuildContext(); + ctx.CheckGuildContext().CheckAuthorPermission(GuildPermission.ManageGuild, "Manage Server"); ITextChannel channel = null; if (ctx.HasNext()) channel = ctx.MatchChannel() ?? throw new PKSyntaxError("You must pass a #channel to set."); - - await _logChannels.SetLogChannel(ctx.Guild, channel); + var cfg = await _data.GetGuildConfig(ctx.Guild.Id); + cfg.LogChannel = channel?.Id; + await _data.SaveGuildConfig(cfg); + if (channel != null) await ctx.Reply($"{Emojis.Success} Proxy logging channel set to #{channel.Name.SanitizeMentions()}."); else diff --git a/PluralKit.Bot/Services/LogChannelService.cs b/PluralKit.Bot/Services/LogChannelService.cs index 1fb53943..9faf81f6 100644 --- a/PluralKit.Bot/Services/LogChannelService.cs +++ b/PluralKit.Bot/Services/LogChannelService.cs @@ -4,22 +4,17 @@ using Discord; using Serilog; namespace PluralKit.Bot { - public class ServerDefinition { - public ulong Id { get; set; } - public ulong? LogChannel { get; set; } - } - public class LogChannelService { private IDiscordClient _client; - private DbConnectionFactory _conn; private EmbedService _embed; + private IDataStore _data; private ILogger _logger; - public LogChannelService(IDiscordClient client, DbConnectionFactory conn, EmbedService embed, ILogger logger) + public LogChannelService(IDiscordClient client, EmbedService embed, ILogger logger, IDataStore data) { - this._client = client; - this._conn = conn; - this._embed = embed; + _client = client; + _embed = embed; + _data = data; _logger = logger.ForContext(); } @@ -33,31 +28,11 @@ namespace PluralKit.Bot { await logChannel.SendMessageAsync(text: url, embed: embed); } - public async Task GetLogChannel(IGuild guild) { - using (var conn = await _conn.Obtain()) - { - var server = - await conn.QueryFirstOrDefaultAsync("select * from servers where id = @Id", - new {Id = guild.Id}); - if (server?.LogChannel == null) return null; - return await _client.GetChannelAsync(server.LogChannel.Value) as ITextChannel; - } - } - - public async Task SetLogChannel(IGuild guild, ITextChannel newLogChannel) { - var def = new ServerDefinition { - Id = guild.Id, - LogChannel = newLogChannel?.Id - }; - - using (var conn = await _conn.Obtain()) - { - await conn.QueryAsync( - "insert into servers (id, log_channel) values (@Id, @LogChannel) on conflict (id) do update set log_channel = @LogChannel", - def); - } - - _logger.Information("Set guild {Guild} log channel to {Channel}", guild.Id, newLogChannel?.Id); + private async Task GetLogChannel(IGuild guild) + { + var guildCfg = await _data.GetGuildConfig(guild.Id); + if (guildCfg.LogChannel == null) return null; + return await _client.GetChannelAsync(guildCfg.LogChannel.Value) as ITextChannel; } } } \ No newline at end of file diff --git a/PluralKit.Core/Stores.cs b/PluralKit.Core/Stores.cs index d97cc84a..f91ae756 100644 --- a/PluralKit.Core/Stores.cs +++ b/PluralKit.Core/Stores.cs @@ -58,6 +58,12 @@ namespace PluralKit { public Instant Timestamp; } + public struct GuildConfig + { + public ulong Id { get; set; } + public ulong? LogChannel { get; set; } + } + public interface IDataStore { /// @@ -316,6 +322,17 @@ namespace PluralKit { /// Gets the total amount of messages in the data store. /// Task GetTotalMessages(); + + /// + /// Gets the guild configuration struct for a given guild. + /// + /// The guild's configuration struct, or a default struct if no guild was found in the data store. + Task GetGuildConfig(ulong guild); + + /// + /// Saves the given guild configuration struct to the data store. + /// + Task SaveGuildConfig(GuildConfig cfg); } public class PostgresDataStore: IDataStore { @@ -568,6 +585,28 @@ namespace PluralKit { return await conn.ExecuteScalarAsync("select count(mid) from messages"); } + public async Task GetGuildConfig(ulong guild) + { + using (var conn = await _conn.Obtain()) + { + var cfg = await conn.QuerySingleOrDefaultAsync("select * from servers where id = @Id", + new {Id = guild}); + + if (cfg.Id == 0) + // No entry was found in the db, this is the default entry returned + cfg.Id = guild; + + return cfg; + } + } + + public async Task SaveGuildConfig(GuildConfig cfg) + { + using (var conn = await _conn.Obtain()) + await conn.ExecuteAsync("insert into servers (id, log_channel) values (@Id, @LogChannel) on conflict (id) do update set log_channel = @LogChannel", cfg); + _logger.Information("Updated guild configuration {@GuildCfg}", cfg); + } + public async Task AddSwitch(PKSystem system, IEnumerable members) { // Use a transaction here since we're doing multiple executed commands in one