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
|
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)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static WordPosition GetWordPosition(this ReadOnlySpan<char> str, in ReadOnlySpan<char> word)
|
public static WordPosition GetWordPosition(this ReadOnlySpan<char> str, in ReadOnlySpan<char> word)
|
||||||
{
|
{
|
||||||
|
|
|
@ -249,8 +249,9 @@ public sealed class EllieExpressionsService : IExecOnMessage, IReadyExecutor
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (guild is SocketGuild sg)
|
if (guild is not SocketGuild sg)
|
||||||
{
|
return false;
|
||||||
|
|
||||||
var result = await _permChecker.CheckPermsAsync(
|
var result = await _permChecker.CheckPermsAsync(
|
||||||
guild,
|
guild,
|
||||||
msg.Channel,
|
msg.Channel,
|
||||||
|
@ -286,9 +287,16 @@ public sealed class EllieExpressionsService : IExecOnMessage, IReadyExecutor
|
||||||
|
|
||||||
return true;
|
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();
|
var reactions = expr.GetReactions();
|
||||||
foreach (var reaction in reactions)
|
foreach (var reaction in reactions)
|
||||||
|
@ -336,6 +344,47 @@ public sealed class EllieExpressionsService : IExecOnMessage, IReadyExecutor
|
||||||
return false;
|
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)
|
public async Task ResetExprReactions(ulong? maybeGuildId, int id)
|
||||||
{
|
{
|
||||||
EllieExpression expr;
|
EllieExpression expr;
|
||||||
|
|
Loading…
Reference in a new issue