expressions will no longer cause exceptions if the bot doesn't have perms to write in the target channel
Cleaned up expr code a little bit
This commit is contained in:
parent
4338df0b38
commit
3ba1d06fd0
2 changed files with 80 additions and 74 deletions
|
@ -6,49 +6,6 @@ namespace EllieBot.Modules.EllieExpressions;
|
|||
|
||||
public static class EllieExpressionExtensions
|
||||
{
|
||||
private static string ResolveTriggerString(this string str, DiscordSocketClient client)
|
||||
=> str.Replace("%bot.mention%", client.CurrentUser.Mention, StringComparison.Ordinal);
|
||||
|
||||
public static async Task<IUserMessage> Send(
|
||||
this EllieExpression cr,
|
||||
IUserMessage ctx,
|
||||
IReplacementService repSvc,
|
||||
DiscordSocketClient client,
|
||||
IMessageSenderService sender)
|
||||
{
|
||||
var channel = cr.DmResponse ? await ctx.Author.CreateDMChannelAsync() : ctx.Channel;
|
||||
|
||||
var trigger = cr.Trigger.ResolveTriggerString(client);
|
||||
var substringIndex = trigger.Length;
|
||||
if (cr.ContainsAnywhere)
|
||||
{
|
||||
var pos = ctx.Content.AsSpan().GetWordPosition(trigger);
|
||||
if (pos == WordPosition.Start)
|
||||
substringIndex += 1;
|
||||
else if (pos == WordPosition.End)
|
||||
substringIndex = ctx.Content.Length;
|
||||
else if (pos == WordPosition.Middle)
|
||||
substringIndex += ctx.Content.IndexOf(trigger, StringComparison.InvariantCulture);
|
||||
}
|
||||
|
||||
var canMentionEveryone = (ctx.Author as IGuildUser)?.GuildPermissions.MentionEveryone ?? true;
|
||||
|
||||
var repCtx = new ReplacementContext(client: client,
|
||||
guild: (ctx.Channel as ITextChannel)?.Guild as SocketGuild,
|
||||
channel: ctx.Channel,
|
||||
user: ctx.Author
|
||||
)
|
||||
.WithOverride("%target%",
|
||||
() => canMentionEveryone
|
||||
? ctx.Content[substringIndex..].Trim()
|
||||
: ctx.Content[substringIndex..].Trim().SanitizeMentions(true));
|
||||
|
||||
var text = SmartText.CreateFrom(cr.Response);
|
||||
text = await repSvc.ReplaceAsync(text, repCtx);
|
||||
|
||||
return await sender.Response(channel).Text(text).Sanitize(false).SendAsync();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static WordPosition GetWordPosition(this ReadOnlySpan<char> str, in ReadOnlySpan<char> word)
|
||||
{
|
||||
|
|
|
@ -249,46 +249,54 @@ public sealed class EllieExpressionsService : IExecOnMessage, IReadyExecutor
|
|||
|
||||
try
|
||||
{
|
||||
if (guild is SocketGuild sg)
|
||||
if (guild is not SocketGuild sg)
|
||||
return false;
|
||||
|
||||
var result = await _permChecker.CheckPermsAsync(
|
||||
guild,
|
||||
msg.Channel,
|
||||
msg.Author,
|
||||
"ACTUALEXPRESSIONS",
|
||||
expr.Trigger
|
||||
);
|
||||
|
||||
if (!result.IsAllowed)
|
||||
{
|
||||
var result = await _permChecker.CheckPermsAsync(
|
||||
guild,
|
||||
msg.Channel,
|
||||
msg.Author,
|
||||
"ACTUALEXPRESSIONS",
|
||||
expr.Trigger
|
||||
);
|
||||
|
||||
if (!result.IsAllowed)
|
||||
var cache = _pc.GetCacheFor(guild.Id);
|
||||
if (cache.Verbose)
|
||||
{
|
||||
var cache = _pc.GetCacheFor(guild.Id);
|
||||
if (cache.Verbose)
|
||||
if (result.TryPickT3(out var disallowed, out _))
|
||||
{
|
||||
if (result.TryPickT3(out var disallowed, out _))
|
||||
var permissionMessage = _strings.GetText(strs.perm_prevent(disallowed.PermIndex + 1,
|
||||
Format.Bold(disallowed.PermText)),
|
||||
sg.Id);
|
||||
|
||||
try
|
||||
{
|
||||
await _sender.Response(msg.Channel)
|
||||
.Error(permissionMessage)
|
||||
.SendAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
var permissionMessage = _strings.GetText(strs.perm_prevent(disallowed.PermIndex + 1,
|
||||
Format.Bold(disallowed.PermText)),
|
||||
sg.Id);
|
||||
|
||||
try
|
||||
{
|
||||
await _sender.Response(msg.Channel)
|
||||
.Error(permissionMessage)
|
||||
.SendAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
Log.Information("{PermissionMessage}", permissionMessage);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
Log.Information("{PermissionMessage}", permissionMessage);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
var sentMsg = await expr.Send(msg, _repSvc, _client, _sender);
|
||||
var cu = sg.CurrentUser;
|
||||
|
||||
var channel = expr.DmResponse ? await msg.Author.CreateDMChannelAsync() : msg.Channel;
|
||||
|
||||
// have no perms to speak in that channel
|
||||
if (channel is ITextChannel tc && !cu.GetPermissions(tc).SendMessages)
|
||||
return false;
|
||||
|
||||
var sentMsg = await Send(expr, msg, channel);
|
||||
|
||||
var reactions = expr.GetReactions();
|
||||
foreach (var reaction in reactions)
|
||||
|
@ -336,6 +344,47 @@ public sealed class EllieExpressionsService : IExecOnMessage, IReadyExecutor
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
public string ResolveTriggerString(string str)
|
||||
=> str.Replace("%bot.mention%", _client.CurrentUser.Mention, StringComparison.Ordinal);
|
||||
|
||||
public async Task<IUserMessage> Send(
|
||||
EllieExpression cr,
|
||||
IUserMessage ctx,
|
||||
IMessageChannel channel
|
||||
)
|
||||
{
|
||||
var trigger = ResolveTriggerString(cr.Trigger);
|
||||
var substringIndex = trigger.Length;
|
||||
if (cr.ContainsAnywhere)
|
||||
{
|
||||
var pos = ctx.Content.AsSpan().GetWordPosition(trigger);
|
||||
if (pos == WordPosition.Start)
|
||||
substringIndex += 1;
|
||||
else if (pos == WordPosition.End)
|
||||
substringIndex = ctx.Content.Length;
|
||||
else if (pos == WordPosition.Middle)
|
||||
substringIndex += ctx.Content.IndexOf(trigger, StringComparison.InvariantCulture);
|
||||
}
|
||||
|
||||
var canMentionEveryone = (ctx.Author as IGuildUser)?.GuildPermissions.MentionEveryone ?? true;
|
||||
|
||||
var repCtx = new ReplacementContext(client: _client,
|
||||
guild: (ctx.Channel as ITextChannel)?.Guild as SocketGuild,
|
||||
channel: ctx.Channel,
|
||||
user: ctx.Author
|
||||
)
|
||||
.WithOverride("%target%",
|
||||
() => canMentionEveryone
|
||||
? ctx.Content[substringIndex..].Trim()
|
||||
: ctx.Content[substringIndex..].Trim().SanitizeMentions(true));
|
||||
|
||||
var text = SmartText.CreateFrom(cr.Response);
|
||||
text = await _repSvc.ReplaceAsync(text, repCtx);
|
||||
|
||||
return await _sender.Response(channel).Text(text).Sanitize(false).SendAsync();
|
||||
}
|
||||
|
||||
public async Task ResetExprReactions(ulong? maybeGuildId, int id)
|
||||
{
|
||||
EllieExpression expr;
|
||||
|
|
Loading…
Reference in a new issue