2019-11-03 18:15:50 +00:00
using System.Collections.Generic ;
using System.Linq ;
2019-06-21 11:49:58 +00:00
using System.Threading.Tasks ;
2020-02-12 14:16:19 +00:00
2020-06-13 11:58:27 +00:00
using Dapper ;
2020-04-24 19:50:28 +00:00
using DSharpPlus ;
using DSharpPlus.Entities ;
2019-10-05 05:41:00 +00:00
2020-02-12 14:16:19 +00:00
using PluralKit.Core ;
2019-06-21 11:49:58 +00:00
2020-02-12 14:16:19 +00:00
namespace PluralKit.Bot
2019-06-21 11:49:58 +00:00
{
2020-02-01 12:03:02 +00:00
public class ServerConfig
2019-06-21 11:49:58 +00:00
{
2020-06-13 16:31:20 +00:00
private Database _db ;
2020-02-14 23:12:03 +00:00
private LoggerCleanService _cleanService ;
2020-06-13 16:31:20 +00:00
public ServerConfig ( LoggerCleanService cleanService , Database db )
2019-06-21 11:49:58 +00:00
{
2020-02-14 23:12:03 +00:00
_cleanService = cleanService ;
2020-06-13 11:58:27 +00:00
_db = db ;
2019-10-05 05:41:00 +00:00
}
public async Task SetLogChannel ( Context ctx )
{
2020-04-24 19:50:28 +00:00
ctx . CheckGuildContext ( ) . CheckAuthorPermission ( Permissions . ManageGuild , "Manage Server" ) ;
2019-10-05 05:41:00 +00:00
2020-04-24 19:50:28 +00:00
DiscordChannel channel = null ;
2019-10-05 05:41:00 +00:00
if ( ctx . HasNext ( ) )
2020-05-02 14:25:17 +00:00
channel = await ctx . MatchChannel ( ) ? ? throw new PKSyntaxError ( "You must pass a #channel to set." ) ;
2020-04-24 19:50:28 +00:00
if ( channel ! = null & & channel . GuildId ! = ctx . Guild . Id ) throw new PKError ( "That channel is not in this server!" ) ;
2019-06-21 11:49:58 +00:00
2020-06-13 11:58:27 +00:00
await _db . Execute ( c = > c . ExecuteAsync ( QueryBuilder . Upsert ( "servers" , "id" )
. Constant ( "id" , "@Id" )
. Variable ( "log_channel" , "@LogChannel" )
. Build ( ) , new { Id = ctx . Guild . Id , LogChannel = channel ? . Id } ) ) ;
2019-06-21 11:49:58 +00:00
if ( channel ! = null )
2019-10-18 11:14:36 +00:00
await ctx . Reply ( $"{Emojis.Success} Proxy logging channel set to #{channel.Name.SanitizeMentions()}." ) ;
2019-06-21 11:49:58 +00:00
else
2019-10-05 05:41:00 +00:00
await ctx . Reply ( $"{Emojis.Success} Proxy logging channel cleared." ) ;
2019-06-21 11:49:58 +00:00
}
2019-11-03 18:15:50 +00:00
public async Task SetLogEnabled ( Context ctx , bool enable )
{
2020-04-24 19:50:28 +00:00
ctx . CheckGuildContext ( ) . CheckAuthorPermission ( Permissions . ManageGuild , "Manage Server" ) ;
2019-11-03 18:15:50 +00:00
2020-04-24 19:50:28 +00:00
var affectedChannels = new List < DiscordChannel > ( ) ;
2019-11-03 18:15:50 +00:00
if ( ctx . Match ( "all" ) )
2020-04-24 19:50:28 +00:00
affectedChannels = ( await ctx . Guild . GetChannelsAsync ( ) ) . Where ( x = > x . Type = = ChannelType . Text ) . ToList ( ) ;
2019-11-03 18:15:50 +00:00
else if ( ! ctx . HasNext ( ) ) throw new PKSyntaxError ( "You must pass one or more #channels." ) ;
else while ( ctx . HasNext ( ) )
{
2020-05-02 14:25:17 +00:00
var channel = await ctx . MatchChannel ( ) ? ? throw new PKSyntaxError ( $"Channel \" { ctx . PopArgument ( ) . SanitizeMentions ( ) } \ " not found." ) ;
2019-11-03 18:15:50 +00:00
if ( channel . GuildId ! = ctx . Guild . Id ) throw new PKError ( $"Channel {ctx.Guild.Id} is not in this server." ) ;
affectedChannels . Add ( channel ) ;
}
2020-06-13 11:58:27 +00:00
ulong? logChannel = null ;
await using ( var conn = await _db . Obtain ( ) )
{
var config = await conn . QueryOrInsertGuildConfig ( ctx . Guild . Id ) ;
logChannel = config . LogChannel ;
var blacklist = config . LogBlacklist . ToHashSet ( ) ;
if ( enable )
blacklist . ExceptWith ( affectedChannels . Select ( c = > c . Id ) ) ;
else
blacklist . UnionWith ( affectedChannels . Select ( c = > c . Id ) ) ;
await conn . ExecuteAsync ( "update servers set log_blacklist = @LogBlacklist where id = @Id" ,
new { ctx . Guild . Id , LogBlacklist = blacklist } ) ;
}
2019-11-03 18:15:50 +00:00
await ctx . Reply (
$"{Emojis.Success} Message logging for the given channels {(enable ? " enabled " : " disabled ")}." +
2020-06-13 11:58:27 +00:00
( logChannel = = null ? $"\n{Emojis.Warn} Please note that no logging channel is set, so there is nowhere to log messages to. You can set a logging channel using `pk;log channel #your-log-channel`." : "" ) ) ;
2019-11-03 18:15:50 +00:00
}
public async Task SetBlacklisted ( Context ctx , bool onBlacklist )
{
2020-04-24 19:50:28 +00:00
ctx . CheckGuildContext ( ) . CheckAuthorPermission ( Permissions . ManageGuild , "Manage Server" ) ;
2019-11-03 18:15:50 +00:00
2020-04-24 19:50:28 +00:00
var affectedChannels = new List < DiscordChannel > ( ) ;
2019-11-03 18:15:50 +00:00
if ( ctx . Match ( "all" ) )
2020-04-24 19:50:28 +00:00
affectedChannels = ( await ctx . Guild . GetChannelsAsync ( ) ) . Where ( x = > x . Type = = ChannelType . Text ) . ToList ( ) ;
2019-11-03 18:15:50 +00:00
else if ( ! ctx . HasNext ( ) ) throw new PKSyntaxError ( "You must pass one or more #channels." ) ;
else while ( ctx . HasNext ( ) )
{
2020-05-02 14:25:17 +00:00
var channel = await ctx . MatchChannel ( ) ? ? throw new PKSyntaxError ( $"Channel \" { ctx . PopArgument ( ) . SanitizeMentions ( ) } \ " not found." ) ;
2019-11-03 18:15:50 +00:00
if ( channel . GuildId ! = ctx . Guild . Id ) throw new PKError ( $"Channel {ctx.Guild.Id} is not in this server." ) ;
affectedChannels . Add ( channel ) ;
}
2020-06-13 11:58:27 +00:00
await using ( var conn = await _db . Obtain ( ) )
{
var guild = await conn . QueryOrInsertGuildConfig ( ctx . Guild . Id ) ;
var blacklist = guild . Blacklist . ToHashSet ( ) ;
if ( onBlacklist )
blacklist . ExceptWith ( affectedChannels . Select ( c = > c . Id ) ) ;
else
blacklist . UnionWith ( affectedChannels . Select ( c = > c . Id ) ) ;
await conn . ExecuteAsync ( "update servers set blacklist = @Blacklist where id = @Id" ,
new { ctx . Guild . Id , Blacklist = blacklist } ) ;
}
2019-11-03 18:15:50 +00:00
await ctx . Reply ( $"{Emojis.Success} Channels {(onBlacklist ? " added to " : " removed from ")} the proxy blacklist." ) ;
2019-06-21 11:49:58 +00:00
}
2020-02-14 23:12:03 +00:00
public async Task SetLogCleanup ( Context ctx )
{
2020-04-24 19:50:28 +00:00
ctx . CheckGuildContext ( ) . CheckAuthorPermission ( Permissions . ManageGuild , "Manage Server" ) ;
2020-02-14 23:12:03 +00:00
var botList = string . Join ( ", " , _cleanService . Bots . Select ( b = > b . Name ) . OrderBy ( x = > x . ToLowerInvariant ( ) ) ) ;
2020-06-13 11:58:27 +00:00
bool newValue ;
2020-02-14 23:12:03 +00:00
if ( ctx . Match ( "enable" , "on" , "yes" ) )
2020-06-13 11:58:27 +00:00
newValue = true ;
2020-02-14 23:12:03 +00:00
else if ( ctx . Match ( "disable" , "off" , "no" ) )
2020-06-13 11:58:27 +00:00
newValue = false ;
2020-02-14 23:12:03 +00:00
else
{
2020-04-24 19:50:28 +00:00
var eb = new DiscordEmbedBuilder ( )
2020-02-14 23:12:03 +00:00
. WithTitle ( "Log cleanup settings" )
. AddField ( "Supported bots" , botList ) ;
2020-06-13 11:58:27 +00:00
var guildCfg = await _db . Execute ( c = > c . QueryOrInsertGuildConfig ( ctx . Guild . Id ) ) ;
2020-02-14 23:12:03 +00:00
if ( guildCfg . LogCleanupEnabled )
eb . WithDescription ( "Log cleanup is currently **on** for this server. To disable it, type `pk;logclean off`." ) ;
else
eb . WithDescription ( "Log cleanup is currently **off** for this server. To enable it, type `pk;logclean on`." ) ;
await ctx . Reply ( embed : eb . Build ( ) ) ;
2020-06-13 11:58:27 +00:00
return ;
2020-02-14 23:12:03 +00:00
}
2020-06-13 11:58:27 +00:00
await _db . Execute ( c = > c . ExecuteAsync ( "update servers set log_cleanup_enabled = @Value where id = @Id" ,
new { ctx . Guild . Id , Value = newValue } ) ) ;
if ( newValue )
await ctx . Reply ( $"{Emojis.Success} Log cleanup has been **enabled** for this server. Messages deleted by PluralKit will now be cleaned up from logging channels managed by the following bots:\n- **{botList}**\n\n{Emojis.Note} Make sure PluralKit has the **Manage Messages** permission in the channels in question.\n{Emojis.Note} Also, make sure to blacklist the logging channel itself from the bots in question to prevent conflicts." ) ;
else
await ctx . Reply ( $"{Emojis.Success} Log cleanup has been **disabled** for this server." ) ;
2020-02-14 23:12:03 +00:00
}
2019-06-21 11:49:58 +00:00
}
}