fix channel mention on adding a new warning

This commit is contained in:
13xforever
2018-07-19 19:46:44 +05:00
committed by Roberto Anić Banić
parent 14462d7bed
commit a1ecb592dc
4 changed files with 47 additions and 29 deletions

View File

@@ -40,7 +40,7 @@ namespace CompatBot.Commands
}
}
await BotDb.Instance.SaveChangesAsync().ConfigureAwait(false);
ctx.RespondAsync($"Fixed {@fixed} records").ConfigureAwait(false);
await ctx.RespondAsync($"Fixed {@fixed} records").ConfigureAwait(false);
}
catch (Exception e)
{
@@ -58,33 +58,16 @@ namespace CompatBot.Commands
{
var @fixed = 0;
foreach (var warning in BotDb.Instance.Warning)
if (!string.IsNullOrEmpty(warning.Reason) && warning.Reason.Contains('#'))
{
var newReason = await FixChannelMentionAsync(ctx, warning.Reason).ConfigureAwait(false);
if (newReason != warning.Reason)
{
var reasonParts = warning.Reason.Split(' ');
var rebuiltMsg = new List<string>(reasonParts.Length);
var changed = false;
foreach (var p in reasonParts)
{
if (p.Contains('#'))
{
var ch = await new CustomDiscordChannelConverter().ConvertAsync(p, ctx).ConfigureAwait(false);
if (ch.HasValue)
{
rebuiltMsg.Add("#" + ch.Value.Name);
changed = true;
continue;
}
}
rebuiltMsg.Add(p);
}
if (changed)
{
warning.Reason = string.Join(' ', rebuiltMsg);
@fixed++;
}
warning.Reason = newReason;
@fixed++;
}
}
await BotDb.Instance.SaveChangesAsync().ConfigureAwait(false);
ctx.RespondAsync($"Fixed {@fixed} records").ConfigureAwait(false);
await ctx.RespondAsync($"Fixed {@fixed} records").ConfigureAwait(false);
}
catch (Exception e)
{
@@ -92,6 +75,33 @@ namespace CompatBot.Commands
await ctx.RespondAsync("Failed to fix warning timestamps").ConfigureAwait(false);
}
}
public static async Task<string> FixChannelMentionAsync(CommandContext ctx, string msg)
{
if (!string.IsNullOrEmpty(msg) && msg.Contains('#'))
{
var reasonParts = msg.Split(' ');
var rebuiltMsg = new List<string>(reasonParts.Length);
var changed = false;
foreach (var p in reasonParts)
{
if (p.Contains('#'))
{
var ch = await new CustomDiscordChannelConverter().ConvertAsync(p, ctx).ConfigureAwait(false);
if (ch.HasValue)
{
rebuiltMsg.Add("#" + ch.Value.Name);
changed = true;
continue;
}
}
rebuiltMsg.Add(p);
}
if (changed)
return string.Join(' ', rebuiltMsg);
}
return msg;
}
}
}
}

View File

@@ -27,7 +27,7 @@ namespace CompatBot.Commands
return;
var typingTask = ctx.TriggerTypingAsync();
if (await AddAsync(ctx.Client, ctx.Message, user.Id, user.Username.Sanitize(), ctx.Message.Author, reason).ConfigureAwait(false))
if (await AddAsync(ctx, user.Id, user.Username.Sanitize(), ctx.Message.Author, reason).ConfigureAwait(false))
await ctx.Message.CreateReactionAsync(Config.Reactions.Success).ConfigureAwait(false);
else
await ctx.Message.CreateReactionAsync(Config.Reactions.Failure).ConfigureAwait(false);
@@ -41,7 +41,7 @@ namespace CompatBot.Commands
return;
var typingTask = ctx.TriggerTypingAsync();
if (await AddAsync(ctx.Client, ctx.Message, userId, $"<@{userId}>", ctx.Message.Author, reason).ConfigureAwait(false))
if (await AddAsync(ctx, userId, $"<@{userId}>", ctx.Message.Author, reason).ConfigureAwait(false))
await ctx.Message.CreateReactionAsync(Config.Reactions.Success).ConfigureAwait(false);
else
await ctx.Message.CreateReactionAsync(Config.Reactions.Failure).ConfigureAwait(false);
@@ -96,6 +96,12 @@ namespace CompatBot.Commands
}
internal static async Task<bool> AddAsync(CommandContext ctx, ulong userId, string userName, DiscordUser issuer, string reason, string fullReason = null)
{
reason = await Sudo.Fix.FixChannelMentionAsync(ctx, reason).ConfigureAwait(false);
return await AddAsync(ctx.Client, ctx.Message, userId, userName, issuer, reason, fullReason);
}
internal static async Task<bool> AddAsync(DiscordClient client, DiscordMessage message, ulong userId, string userName, DiscordUser issuer, string reason, string fullReason = null)
{
if (string.IsNullOrEmpty(reason))

View File

@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using CompatApiClient;
using CompatBot.Commands;
using CompatBot.Providers;
using CompatBot.Utils;
@@ -50,7 +51,7 @@ namespace CompatBot.EventHandlers
await Task.WhenAll(
message.Channel.SendMessageAsync($"{message.Author.Mention} Please follow the {rules.Mention} and do not discuss piracy on this server. Repeated offence may result in a ban."),
client.ReportAsync("Mention of piracy", message, trigger, message.Content, needsAttention),
Warnings.AddAsync(client, message, message.Author.Id, message.Author.Username, client.CurrentUser, "Mention of piracy", message.Content)
Warnings.AddAsync(client, message, message.Author.Id, message.Author.Username, client.CurrentUser, "Mention of piracy", message.Content.Sanitize())
).ConfigureAwait(false);
}
catch (Exception e)

View File

@@ -2,6 +2,7 @@
using System.IO.Pipelines;
using System.Linq;
using System.Threading.Tasks;
using CompatApiClient;
using CompatBot.Commands;
using CompatBot.LogParsing;
using CompatBot.LogParsing.SourceHandlers;
@@ -69,7 +70,7 @@ namespace CompatBot.EventHandlers
await Task.WhenAll(
args.Client.ReportAsync("Pirated Release", args.Message, result.PiracyTrigger, result.PiracyContext, needsAttention),
Warnings.AddAsync(args.Client, args.Message, args.Message.Author.Id, args.Message.Author.Username, args.Client.CurrentUser,
"Pirated Release", $"{message.CreationTimestamp:O} - {message.Content} - {result.PiracyTrigger}")
"Pirated Release", $"{message.Content.Sanitize()} - {result.PiracyTrigger}")
);
}
}