mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-08 23:07:54 +00:00
feat(bot): add proxy error config (#544)
This commit is contained in:
parent
020a6f99fe
commit
8187aa05b7
10 changed files with 93 additions and 7 deletions
|
|
@ -524,6 +524,8 @@ public partial class CommandTree
|
|||
return ctx.Execute<Config>(null, m => m.ShowPrivateInfo(ctx));
|
||||
if (ctx.MatchMultiple(new[] { "proxy" }, new[] { "case" }))
|
||||
return ctx.Execute<Config>(null, m => m.CaseSensitiveProxyTags(ctx));
|
||||
if (ctx.MatchMultiple(new[] { "proxy" }, new[] { "error" }) || ctx.Match("pe"))
|
||||
return ctx.Execute<Config>(null, m => m.ProxyErrorMessageEnabled(ctx));
|
||||
|
||||
// todo: maybe add the list of configuration keys here?
|
||||
return ctx.Reply($"{Emojis.Error} Could not find a setting with that name. Please see `pk;commands config` for the list of possible config settings.");
|
||||
|
|
|
|||
|
|
@ -248,6 +248,13 @@ public class Checks
|
|||
_proxy.ShouldProxy(channel, msg, context);
|
||||
_matcher.TryMatch(context, autoproxySettings, members, out var match, msg.Content, msg.Attachments.Length > 0, true, ctx.Config.CaseSensitiveProxyTags);
|
||||
|
||||
var canProxy = await _proxy.CanProxy(channel, msg, context);
|
||||
if (canProxy != null)
|
||||
{
|
||||
await ctx.Reply(canProxy);
|
||||
return;
|
||||
}
|
||||
|
||||
await ctx.Reply("I'm not sure why this message was not proxied, sorry.");
|
||||
}
|
||||
catch (ProxyService.ProxyChecksFailedException e)
|
||||
|
|
|
|||
|
|
@ -95,6 +95,13 @@ public class Config
|
|||
"enabled"
|
||||
));
|
||||
|
||||
items.Add(new(
|
||||
"Proxy error",
|
||||
"Whether to send an error message when proxying fails.",
|
||||
EnabledDisabled(ctx.Config.ProxyErrorMessageEnabled),
|
||||
"enabled"
|
||||
));
|
||||
|
||||
await ctx.Paginate<PaginatedConfigItem>(
|
||||
items.ToAsyncEnumerable(),
|
||||
items.Count,
|
||||
|
|
@ -413,4 +420,27 @@ public class Config
|
|||
await ctx.Reply("Proxy tags are now case insensitive.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ProxyErrorMessageEnabled(Context ctx)
|
||||
{
|
||||
if (!ctx.HasNext())
|
||||
{
|
||||
if (ctx.Config.ProxyErrorMessageEnabled) { await ctx.Reply("Proxy error messages are currently **enabled**."); }
|
||||
else { await ctx.Reply("Proxy error messages are currently **disabled**. Messages that fail to proxy (due to message or attachment size) will not throw an error message."); }
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx.MatchToggle(true))
|
||||
{
|
||||
await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { ProxyErrorMessageEnabled = true });
|
||||
|
||||
await ctx.Reply("Proxy error messages are now enabled.");
|
||||
}
|
||||
else
|
||||
{
|
||||
await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { ProxyErrorMessageEnabled = false });
|
||||
|
||||
await ctx.Reply("Proxy error messages are now disabled. Messages that fail to proxy (due to message or attachment size) will not throw an error message.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -82,9 +82,14 @@ public class ProxyService
|
|||
if (!_matcher.TryMatch(ctx, autoproxySettings, members, out var match, message.Content, message.Attachments.Length > 0,
|
||||
allowAutoproxy, ctx.CaseSensitiveProxyTags)) return false;
|
||||
|
||||
// this is hopefully temporary, so not putting it into a separate method
|
||||
if (message.Content != null && message.Content.Length > 2000)
|
||||
throw new PKError("PluralKit cannot proxy messages over 2000 characters in length.");
|
||||
var canProxy = await CanProxy(channel, message, ctx);
|
||||
if (canProxy != null)
|
||||
{
|
||||
if (ctx.ProxyErrorMessageEnabled)
|
||||
throw new PKError(canProxy);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Permission check after proxy match so we don't get spammed when not actually proxying
|
||||
if (!CheckBotPermissionsOrError(botPermissions, rootChannel.Id))
|
||||
|
|
@ -104,6 +109,29 @@ public class ProxyService
|
|||
return true;
|
||||
}
|
||||
|
||||
public async Task<string> CanProxy(Channel channel, Message msg, MessageContext ctx)
|
||||
{
|
||||
// Check if the message does not go over any Discord Nitro limits
|
||||
if (msg.Content != null && msg.Content.Length > 2000)
|
||||
{
|
||||
return "PluralKit cannot proxy messages over 2000 characters in length.";
|
||||
}
|
||||
|
||||
var guild = await _cache.GetGuild(channel.GuildId.Value);
|
||||
var fileSizeLimit = guild.FileSizeLimit();
|
||||
var bytesThreshold = fileSizeLimit * 1024 * 1024;
|
||||
|
||||
foreach (var attachment in msg.Attachments)
|
||||
{
|
||||
if (attachment.Size > bytesThreshold)
|
||||
{
|
||||
return $"PluralKit cannot proxy attachments over {fileSizeLimit} megabytes in this server (as webhooks aren't considered as having Discord Nitro) :(";
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool ShouldProxy(Channel channel, Message msg, MessageContext ctx)
|
||||
{
|
||||
// Make sure author has a system
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue