2019-11-03 18:15:50 +00:00
using System.Collections.Generic ;
using System.Linq ;
2020-07-05 11:16:21 +00:00
using System.Text ;
2019-06-21 11:49:58 +00:00
using System.Threading.Tasks ;
2020-02-12 14:16:19 +00:00
2020-12-25 11:56:46 +00:00
using Myriad.Builders ;
2020-12-22 15:55:13 +00:00
using Myriad.Cache ;
using Myriad.Extensions ;
using Myriad.Types ;
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-08-29 11:46:27 +00:00
private readonly IDatabase _db ;
private readonly ModelRepository _repo ;
2020-12-22 15:55:13 +00:00
private readonly IDiscordCache _cache ;
2020-08-29 11:46:27 +00:00
private readonly LoggerCleanService _cleanService ;
2021-10-29 17:28:27 +00:00
private readonly Bot _bot ;
public ServerConfig ( LoggerCleanService cleanService , IDatabase db , ModelRepository repo , IDiscordCache cache , Bot bot )
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 ;
2020-08-29 11:46:27 +00:00
_repo = repo ;
2020-12-22 15:55:13 +00:00
_cache = cache ;
2021-10-29 17:28:27 +00:00
_bot = bot ;
2019-10-05 05:41:00 +00:00
}
public async Task SetLogChannel ( Context ctx )
{
2020-12-22 15:55:13 +00:00
ctx . CheckGuildContext ( ) . CheckAuthorPermission ( PermissionSet . ManageGuild , "Manage Server" ) ;
2021-10-29 17:28:27 +00:00
var settings = await _repo . GetGuild ( ctx . Guild . Id ) ;
2021-08-27 15:03:47 +00:00
2020-10-23 10:18:40 +00:00
if ( await ctx . MatchClear ( "the server log channel" ) )
2020-10-17 21:43:59 +00:00
{
2021-09-30 01:51:38 +00:00
await _repo . UpdateGuild ( ctx . Guild . Id , new ( ) { LogChannel = null } ) ;
2020-10-17 21:43:59 +00:00
await ctx . Reply ( $"{Emojis.Success} Proxy logging channel cleared." ) ;
return ;
}
2021-08-27 15:03:47 +00:00
2020-08-01 18:31:46 +00:00
if ( ! ctx . HasNext ( ) )
2021-10-29 17:28:27 +00:00
{
if ( settings . LogChannel = = null )
{
await ctx . Reply ( "This server does not have a log channel set." ) ;
return ;
}
await ctx . Reply ( $"This server's log channel is currently set to <#{settings.LogChannel}>." ) ;
return ;
}
2021-08-27 15:03:47 +00:00
2020-12-22 15:55:13 +00:00
Channel channel = null ;
2020-08-01 18:31:46 +00:00
var channelString = ctx . PeekArgument ( ) ;
channel = await ctx . MatchChannel ( ) ;
2021-01-31 15:16:52 +00:00
if ( channel = = null | | channel . GuildId ! = ctx . Guild . Id ) throw Errors . ChannelNotFound ( channelString ) ;
2021-10-29 17:28:27 +00:00
if ( channel . Type ! = Channel . ChannelType . GuildText )
throw new PKError ( "PluralKit cannot log messages to this type of channel." ) ;
var perms = _bot . PermissionsIn ( channel . Id ) ;
if ( ! perms . HasFlag ( PermissionSet . SendMessages ) )
throw new PKError ( "PluralKit is missing **Send Messages** permissions in the new log channel." ) ;
if ( ! perms . HasFlag ( PermissionSet . EmbedLinks ) )
throw new PKError ( "PluralKit is missing **Embed Links** permissions in the new log channel." ) ;
2019-06-21 11:49:58 +00:00
2021-09-30 01:51:38 +00:00
await _repo . UpdateGuild ( ctx . Guild . Id , new ( ) { LogChannel = channel . Id } ) ;
2021-10-29 17:28:27 +00:00
await ctx . Reply ( $"{Emojis.Success} Proxy logging channel set to <#{channel.Id}>." ) ;
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-12-22 15:55:13 +00:00
ctx . CheckGuildContext ( ) . CheckAuthorPermission ( PermissionSet . ManageGuild , "Manage Server" ) ;
2019-11-03 18:15:50 +00:00
2020-12-22 15:55:13 +00:00
var affectedChannels = new List < Channel > ( ) ;
2019-11-03 18:15:50 +00:00
if ( ctx . Match ( "all" ) )
2021-01-31 15:16:52 +00:00
affectedChannels = _cache . GetGuildChannels ( ctx . Guild . Id ) . Where ( x = > x . Type = = Channel . ChannelType . GuildText ) . 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 ( ) )
2021-08-27 15:03:47 +00:00
{
var channelString = ctx . PeekArgument ( ) ;
var channel = await ctx . MatchChannel ( ) ;
if ( channel = = null | | channel . GuildId ! = ctx . Guild . Id ) throw Errors . ChannelNotFound ( channelString ) ;
affectedChannels . Add ( channel ) ;
}
2019-11-03 18:15:50 +00:00
2020-06-13 11:58:27 +00:00
ulong? logChannel = null ;
2021-09-30 01:51:38 +00:00
var config = await _repo . GetGuild ( ctx . Guild . Id ) ;
logChannel = config . LogChannel ;
2021-08-27 15:03:47 +00:00
2021-09-30 01:51:38 +00:00
var blacklist = config . LogBlacklist . ToHashSet ( ) ;
if ( enable )
blacklist . ExceptWith ( affectedChannels . Select ( c = > c . Id ) ) ;
else
blacklist . UnionWith ( affectedChannels . Select ( c = > c . Id ) ) ;
await _repo . UpdateGuild ( ctx . Guild . Id , new ( ) { LogBlacklist = blacklist . ToArray ( ) } ) ;
2020-06-13 11:58:27 +00:00
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
}
2020-07-05 10:54:27 +00:00
public async Task ShowBlacklisted ( Context ctx )
{
2020-12-22 15:55:13 +00:00
ctx . CheckGuildContext ( ) . CheckAuthorPermission ( PermissionSet . ManageGuild , "Manage Server" ) ;
2020-07-05 10:54:27 +00:00
2021-09-30 01:51:38 +00:00
var blacklist = await _repo . GetGuild ( ctx . Guild . Id ) ;
2021-08-27 15:03:47 +00:00
2020-07-05 11:08:18 +00:00
// Resolve all channels from the cache and order by position
var channels = blacklist . Blacklist
2020-12-22 15:55:13 +00:00
. Select ( id = > _cache . GetChannelOrNull ( id ) )
2020-08-28 21:37:27 +00:00
. Where ( c = > c ! = null )
2020-07-05 11:08:18 +00:00
. OrderBy ( c = > c . Position )
. ToList ( ) ;
2020-07-05 10:54:27 +00:00
2020-07-05 11:08:18 +00:00
if ( channels . Count = = 0 )
{
await ctx . Reply ( $"This server has no blacklisted channels." ) ;
return ;
2020-07-05 10:54:27 +00:00
}
2020-07-05 11:08:18 +00:00
await ctx . Paginate ( channels . ToAsyncEnumerable ( ) , channels . Count , 25 ,
2021-01-31 15:16:52 +00:00
$"Blacklisted channels for {ctx.Guild.Name}" ,
2021-03-28 17:22:31 +00:00
null ,
2020-07-05 11:08:18 +00:00
( eb , l ) = >
{
2020-12-22 15:55:13 +00:00
string CategoryName ( ulong? id ) = >
id ! = null ? _cache . GetChannel ( id . Value ) . Name : "(no category)" ;
2021-08-27 15:03:47 +00:00
2020-12-22 15:55:13 +00:00
ulong? lastCategory = null ;
2020-07-05 11:16:21 +00:00
var fieldValue = new StringBuilder ( ) ;
foreach ( var channel in l )
{
2020-12-22 15:55:13 +00:00
if ( lastCategory ! = channel ! . ParentId & & fieldValue . Length > 0 )
2020-07-05 11:16:21 +00:00
{
2020-12-24 13:52:44 +00:00
eb . Field ( new ( CategoryName ( lastCategory ) , fieldValue . ToString ( ) ) ) ;
2020-07-05 11:16:21 +00:00
fieldValue . Clear ( ) ;
}
else fieldValue . Append ( "\n" ) ;
2020-12-22 15:55:13 +00:00
fieldValue . Append ( channel . Mention ( ) ) ;
lastCategory = channel . ParentId ;
2020-07-05 11:16:21 +00:00
}
2020-12-24 13:52:44 +00:00
eb . Field ( new ( CategoryName ( lastCategory ) , fieldValue . ToString ( ) ) ) ;
2020-07-05 11:16:21 +00:00
2020-07-05 11:08:18 +00:00
return Task . CompletedTask ;
} ) ;
2020-07-05 10:54:27 +00:00
}
2020-06-13 17:19:13 +00:00
public async Task SetBlacklisted ( Context ctx , bool shouldAdd )
2019-11-03 18:15:50 +00:00
{
2020-12-22 15:55:13 +00:00
ctx . CheckGuildContext ( ) . CheckAuthorPermission ( PermissionSet . ManageGuild , "Manage Server" ) ;
2019-11-03 18:15:50 +00:00
2020-12-22 15:55:13 +00:00
var affectedChannels = new List < Channel > ( ) ;
2019-11-03 18:15:50 +00:00
if ( ctx . Match ( "all" ) )
2021-01-31 15:16:52 +00:00
affectedChannels = _cache . GetGuildChannels ( ctx . Guild . Id ) . Where ( x = > x . Type = = Channel . ChannelType . GuildText ) . 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 ( ) )
2021-08-27 15:03:47 +00:00
{
var channelString = ctx . PeekArgument ( ) ;
var channel = await ctx . MatchChannel ( ) ;
if ( channel = = null | | channel . GuildId ! = ctx . Guild . Id ) throw Errors . ChannelNotFound ( channelString ) ;
affectedChannels . Add ( channel ) ;
}
2021-09-30 01:51:38 +00:00
var guild = await _repo . GetGuild ( ctx . Guild . Id ) ;
2021-08-27 15:03:47 +00:00
2021-09-30 01:51:38 +00:00
var blacklist = guild . Blacklist . ToHashSet ( ) ;
if ( shouldAdd )
blacklist . UnionWith ( affectedChannels . Select ( c = > c . Id ) ) ;
else
blacklist . ExceptWith ( affectedChannels . Select ( c = > c . Id ) ) ;
await _repo . UpdateGuild ( ctx . Guild . Id , new ( ) { Blacklist = blacklist . ToArray ( ) } ) ;
2019-11-03 18:15:50 +00:00
2020-06-13 17:19:13 +00:00
await ctx . Reply ( $"{Emojis.Success} Channels {(shouldAdd ? " 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-12-22 15:55:13 +00:00
ctx . CheckGuildContext ( ) . CheckAuthorPermission ( PermissionSet . 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-12-25 11:56:46 +00:00
var eb = new EmbedBuilder ( )
. Title ( "Log cleanup settings" )
. Field ( new ( "Supported bots" , botList ) ) ;
2020-06-13 11:58:27 +00:00
2021-09-30 01:51:38 +00:00
var guildCfg = await _repo . GetGuild ( ctx . Guild . Id ) ;
2020-02-14 23:12:03 +00:00
if ( guildCfg . LogCleanupEnabled )
2021-08-27 15:03:47 +00:00
eb . Description ( "Log cleanup is currently **on** for this server. To disable it, type `pk;logclean off`." ) ;
else
2020-12-25 11:56:46 +00:00
eb . Description ( "Log cleanup is currently **off** for this server. To enable it, type `pk;logclean on`." ) ;
2020-02-14 23:12:03 +00:00
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
2021-09-30 01:51:38 +00:00
await _repo . UpdateGuild ( ctx . Guild . Id , new ( ) { LogCleanupEnabled = newValue } ) ;
2020-06-29 13:20:28 +00:00
2020-06-13 11:58:27 +00:00
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
}
}