mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-10 07:47:53 +00:00
implement proxied message and permcheck commands
This commit is contained in:
parent
2b304457cc
commit
e4f38c76a9
19 changed files with 233 additions and 155 deletions
|
|
@ -13,10 +13,10 @@ public class Autoproxy
|
|||
|
||||
public abstract record Mode()
|
||||
{
|
||||
public record Off() : Mode;
|
||||
public record Latch() : Mode;
|
||||
public record Front() : Mode;
|
||||
public record Member(PKMember member) : Mode;
|
||||
public record Off(): Mode;
|
||||
public record Latch(): Mode;
|
||||
public record Front(): Mode;
|
||||
public record Member(PKMember member): Mode;
|
||||
}
|
||||
|
||||
public Autoproxy(IClock clock)
|
||||
|
|
|
|||
|
|
@ -36,37 +36,11 @@ public class Checks
|
|||
_cache = cache;
|
||||
}
|
||||
|
||||
public async Task PermCheckGuild(Context ctx)
|
||||
public async Task PermCheckGuild(Context ctx, Guild guild)
|
||||
{
|
||||
Guild guild;
|
||||
GuildMemberPartial senderGuildUser = null;
|
||||
|
||||
if (ctx.Guild != null && !ctx.HasNext())
|
||||
{
|
||||
guild = ctx.Guild;
|
||||
senderGuildUser = ctx.Member;
|
||||
}
|
||||
else
|
||||
{
|
||||
var guildIdStr = ctx.RemainderOrNull() ??
|
||||
throw new PKSyntaxError("You must pass a server ID or run this command in a server.");
|
||||
if (!ulong.TryParse(guildIdStr, out var guildId))
|
||||
throw new PKSyntaxError($"Could not parse {guildIdStr.AsCode()} as an ID.");
|
||||
|
||||
try
|
||||
{
|
||||
guild = await _rest.GetGuild(guildId);
|
||||
}
|
||||
catch (ForbiddenException)
|
||||
{
|
||||
throw Errors.GuildNotFound(guildId);
|
||||
}
|
||||
|
||||
if (guild != null)
|
||||
senderGuildUser = await _rest.GetGuildMember(guildId, ctx.Author.Id);
|
||||
if (guild == null || senderGuildUser == null)
|
||||
throw Errors.GuildNotFound(guildId);
|
||||
}
|
||||
var senderGuildUser = await _rest.GetGuildMember(guild.Id, ctx.Author.Id);
|
||||
if (senderGuildUser == null)
|
||||
throw Errors.GuildNotFound(guild.Id);
|
||||
|
||||
var guildMember = await _rest.GetGuildMember(guild.Id, _botConfig.ClientId);
|
||||
|
||||
|
|
@ -135,17 +109,13 @@ public class Checks
|
|||
await ctx.Reply(embed: eb.Build());
|
||||
}
|
||||
|
||||
public async Task PermCheckChannel(Context ctx)
|
||||
public async Task PermCheckChannel(Context ctx, Channel channel)
|
||||
{
|
||||
if (!ctx.HasNext())
|
||||
throw new PKSyntaxError("You need to specify a channel.");
|
||||
|
||||
var error = "Channel not found or you do not have permissions to access it.";
|
||||
|
||||
// todo: this breaks if channel is not in cache and bot does not have View Channel permissions
|
||||
// with new cache it breaks if channel is not in current guild
|
||||
var channel = await ctx.MatchChannel();
|
||||
if (channel == null || channel.GuildId == null)
|
||||
if (channel.GuildId == null)
|
||||
throw new PKError(error);
|
||||
|
||||
var guild = await _rest.GetGuildOrNull(channel.GuildId.Value);
|
||||
|
|
@ -189,15 +159,17 @@ public class Checks
|
|||
await ctx.Reply(embed: eb.Build());
|
||||
}
|
||||
|
||||
public async Task MessageProxyCheck(Context ctx)
|
||||
public async Task MessageProxyCheck(Context ctx, Message.Reference? messageReference)
|
||||
{
|
||||
if (!ctx.HasNext() && ctx.Message.MessageReference == null)
|
||||
if (messageReference == null && ctx.Message.MessageReference == null)
|
||||
throw new PKSyntaxError("You need to specify a message.");
|
||||
|
||||
var failedToGetMessage =
|
||||
"Could not find a valid message to check, was not able to fetch the message, or the message was not sent by you.";
|
||||
|
||||
var (messageId, channelId) = ctx.MatchMessage(false);
|
||||
var (messageId, channelId) = ctx.GetRepliedTo();
|
||||
if (messageReference != null)
|
||||
(messageId, channelId) = (messageReference.MessageId, messageReference.ChannelId);
|
||||
if (messageId == null || channelId == null)
|
||||
throw new PKError(failedToGetMessage);
|
||||
|
||||
|
|
|
|||
|
|
@ -58,9 +58,9 @@ public class ProxiedMessage
|
|||
_redisService = redisService;
|
||||
}
|
||||
|
||||
public async Task ReproxyMessage(Context ctx)
|
||||
public async Task ReproxyMessage(Context ctx, ulong? messageId)
|
||||
{
|
||||
var (msg, systemId) = await GetMessageToEdit(ctx, ReproxyTimeout, true);
|
||||
var (msg, systemId) = await GetMessageToEdit(ctx, messageId, ReproxyTimeout, true);
|
||||
|
||||
if (ctx.System.Id != systemId)
|
||||
throw new PKError("Can't reproxy a message sent by a different system.");
|
||||
|
|
@ -93,9 +93,9 @@ public class ProxiedMessage
|
|||
}
|
||||
}
|
||||
|
||||
public async Task EditMessage(Context ctx, bool useRegex)
|
||||
public async Task EditMessage(Context ctx, ulong? messageId, string newContent, bool useRegex, bool mutateSpace, bool append, bool prepend, bool clearEmbeds, bool clearAttachments)
|
||||
{
|
||||
var (msg, systemId) = await GetMessageToEdit(ctx, EditTimeout, false);
|
||||
var (msg, systemId) = await GetMessageToEdit(ctx, messageId, EditTimeout, false);
|
||||
|
||||
if (ctx.System.Id != systemId)
|
||||
throw new PKError("Can't edit a message sent by a different system.");
|
||||
|
|
@ -104,21 +104,10 @@ public class ProxiedMessage
|
|||
if (originalMsg == null)
|
||||
throw new PKError("Could not edit message.");
|
||||
|
||||
// Regex flag
|
||||
useRegex = useRegex || ctx.MatchFlag("regex", "x");
|
||||
|
||||
// Check if we should append or prepend
|
||||
var mutateSpace = ctx.MatchFlag("nospace", "ns") ? "" : " ";
|
||||
var append = ctx.MatchFlag("append", "a");
|
||||
var prepend = ctx.MatchFlag("prepend", "p");
|
||||
|
||||
// Grab the original message content and new message content
|
||||
var originalContent = originalMsg.Content;
|
||||
var newContent = ctx.RemainderOrNull()?.NormalizeLineEndSpacing();
|
||||
|
||||
// Should we clear embeds?
|
||||
var clearEmbeds = ctx.MatchFlag("clear-embed", "ce");
|
||||
var clearAttachments = ctx.MatchFlag("clear-attachments", "ca");
|
||||
if ((clearEmbeds || clearAttachments) && newContent == null)
|
||||
newContent = originalMsg.Content!;
|
||||
|
||||
|
|
@ -249,14 +238,13 @@ public class ProxiedMessage
|
|||
}
|
||||
}
|
||||
|
||||
private async Task<(PKMessage, SystemId)> GetMessageToEdit(Context ctx, Duration timeout, bool isReproxy)
|
||||
private async Task<(PKMessage, SystemId)> GetMessageToEdit(Context ctx, ulong? referencedMessage, Duration timeout, bool isReproxy)
|
||||
{
|
||||
var editType = isReproxy ? "reproxy" : "edit";
|
||||
var editTypeAction = isReproxy ? "reproxied" : "edited";
|
||||
|
||||
PKMessage? msg = null;
|
||||
|
||||
var (referencedMessage, _) = ctx.MatchMessage(false);
|
||||
if (referencedMessage != null)
|
||||
{
|
||||
await using var conn = await ctx.Database.Obtain();
|
||||
|
|
@ -332,9 +320,8 @@ public class ProxiedMessage
|
|||
return lastMessage;
|
||||
}
|
||||
|
||||
public async Task GetMessage(Context ctx)
|
||||
public async Task GetMessage(Context ctx, ulong? messageId, ReplyFormat format, bool isDelete, bool author)
|
||||
{
|
||||
var (messageId, _) = ctx.MatchMessage(true);
|
||||
if (messageId == null)
|
||||
{
|
||||
if (!ctx.HasNext())
|
||||
|
|
@ -342,8 +329,6 @@ public class ProxiedMessage
|
|||
throw new PKSyntaxError($"Could not parse {ctx.PeekArgument().AsCode()} as a message ID or link.");
|
||||
}
|
||||
|
||||
var isDelete = ctx.Match("delete") || ctx.MatchFlag("delete");
|
||||
|
||||
var message = await ctx.Repository.GetFullMessage(messageId.Value);
|
||||
if (message == null)
|
||||
{
|
||||
|
|
@ -360,8 +345,6 @@ public class ProxiedMessage
|
|||
else if (!await ctx.CheckPermissionsInGuildChannel(channel, PermissionSet.ViewChannel))
|
||||
showContent = false;
|
||||
|
||||
var format = ctx.MatchFormat();
|
||||
|
||||
if (format != ReplyFormat.Standard)
|
||||
{
|
||||
var discordMessage = await _rest.GetMessageOrNull(message.Message.Channel, message.Message.Mid);
|
||||
|
|
@ -423,7 +406,7 @@ public class ProxiedMessage
|
|||
return;
|
||||
}
|
||||
|
||||
if (ctx.Match("author") || ctx.MatchFlag("author"))
|
||||
if (author)
|
||||
{
|
||||
var user = await _rest.GetUser(message.Message.Sender);
|
||||
var eb = new EmbedBuilder()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue