feat: misc pk;log channel changes

- show currently set log channel if no arguments are present
- throw error if someone tries to set the log channel to an invalid channel type
- throw error if bot is missing permissions to log in the new channel
This commit is contained in:
spiral 2021-10-29 13:28:27 -04:00
parent 13fa78987c
commit be5bf0cfb7
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
2 changed files with 24 additions and 4 deletions

View File

@ -18,18 +18,20 @@ namespace PluralKit.Bot
private readonly ModelRepository _repo; private readonly ModelRepository _repo;
private readonly IDiscordCache _cache; private readonly IDiscordCache _cache;
private readonly LoggerCleanService _cleanService; private readonly LoggerCleanService _cleanService;
public ServerConfig(LoggerCleanService cleanService, IDatabase db, ModelRepository repo, IDiscordCache cache) private readonly Bot _bot;
public ServerConfig(LoggerCleanService cleanService, IDatabase db, ModelRepository repo, IDiscordCache cache, Bot bot)
{ {
_cleanService = cleanService; _cleanService = cleanService;
_db = db; _db = db;
_repo = repo; _repo = repo;
_cache = cache; _cache = cache;
_bot = bot;
} }
public async Task SetLogChannel(Context ctx) public async Task SetLogChannel(Context ctx)
{ {
ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server"); ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");
await _repo.GetGuild(ctx.Guild.Id); var settings = await _repo.GetGuild(ctx.Guild.Id);
if (await ctx.MatchClear("the server log channel")) if (await ctx.MatchClear("the server log channel"))
{ {
@ -39,15 +41,32 @@ namespace PluralKit.Bot
} }
if (!ctx.HasNext()) if (!ctx.HasNext())
throw new PKSyntaxError("You must pass a #channel to set, or `clear` to clear it."); {
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;
}
Channel channel = null; Channel channel = null;
var channelString = ctx.PeekArgument(); var channelString = ctx.PeekArgument();
channel = await ctx.MatchChannel(); channel = await ctx.MatchChannel();
if (channel == null || channel.GuildId != ctx.Guild.Id) throw Errors.ChannelNotFound(channelString); if (channel == null || channel.GuildId != ctx.Guild.Id) throw Errors.ChannelNotFound(channelString);
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.");
await _repo.UpdateGuild(ctx.Guild.Id, new() { LogChannel = channel.Id }); await _repo.UpdateGuild(ctx.Guild.Id, new() { LogChannel = channel.Id });
await ctx.Reply($"{Emojis.Success} Proxy logging channel set to #{channel.Name}."); await ctx.Reply($"{Emojis.Success} Proxy logging channel set to <#{channel.Id}>.");
} }
public async Task SetLogEnabled(Context ctx, bool enable) public async Task SetLogEnabled(Context ctx, bool enable)

View File

@ -112,6 +112,7 @@ Some arguments indicate the use of specific Discord features. These include:
## Server owner commands ## Server owner commands
*(all commands here require Manage Server permission)* *(all commands here require Manage Server permission)*
- `pk;log channel` - Shows the currently set log channel
- `pk;log channel <channel>` - Sets the given channel to log all proxied messages. - `pk;log channel <channel>` - Sets the given channel to log all proxied messages.
- `pk;log channel -clear` - Clears the currently set log channel. - `pk;log channel -clear` - Clears the currently set log channel.
- `pk;log disable <#channel> [#channel...]` - Disables logging messages posted in the given channel(s) (useful for staff channels and such). - `pk;log disable <#channel> [#channel...]` - Disables logging messages posted in the given channel(s) (useful for staff channels and such).