RegexBot/Module/AutoMod/Responses/Report.cs

111 lines
3.8 KiB
C#
Raw Normal View History

2017-08-26 17:24:37 +00:00
using Discord;
using Discord.WebSocket;
using Noikoio.RegexBot.ConfigItem;
using System;
using System.Text;
using System.Threading.Tasks;
2017-11-12 03:12:24 +00:00
namespace Noikoio.RegexBot.Module.AutoMod.Responses
2017-08-26 17:24:37 +00:00
{
/// <summary>
/// Sends a summary of the invoking message, along with information
/// about the rule making use of this command, to the given target.
/// Parameters: report (target)
/// </summary>
2017-09-05 17:18:07 +00:00
class Report : ResponseBase
2017-08-26 17:24:37 +00:00
{
readonly string _target;
2017-09-05 17:18:07 +00:00
public Report(ConfigItem rule, string cmdline) : base(rule, cmdline)
2017-08-26 17:24:37 +00:00
{
var line = cmdline.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (line.Length != 2) throw new RuleImportException("Incorrect number of parameters");
_target = line[1];
}
public override async Task Invoke(SocketMessage msg)
{
var target = await GetMessageTargetAsync(_target, msg);
if (target == null)
{
await Log("Error: Unable to resolve the given target.");
}
await target.SendMessageAsync("", embed: BuildReportEmbed(msg));
}
private EmbedBuilder BuildReportEmbed(SocketMessage msg)
{
string invokeLine = msg.Content;
// Discord has a 2000 character limit per single message.
// Priority is to show as much of the offending line as possible, to a point.
const int DescriptionLengthMax = 1700; // leaving 300 buffer for embed formatting data
bool showResponseBody = true;
2017-08-26 17:24:37 +00:00
if (invokeLine.Length > DescriptionLengthMax)
{
// Do not attempt to show response body.
showResponseBody = false;
2017-08-26 17:24:37 +00:00
invokeLine = $"**Message length too long; showing first {DescriptionLengthMax} characters.**\n\n"
+ invokeLine.Substring(0, DescriptionLengthMax);
}
string responsebody = null;
if (showResponseBody)
2017-08-26 17:24:37 +00:00
{
// Write a summary of responses defined
var frb = new StringBuilder();
foreach (var item in Rule.Response)
{
frb.AppendLine("`" + item.CmdLine.Replace("\r", "").Replace("\n", "\\n") + "`");
}
responsebody = frb.ToString();
if (invokeLine.Length + responsebody.Length > DescriptionLengthMax)
{
// Still can't do it, so just don't.
responsebody = null;
}
2017-08-26 17:24:37 +00:00
}
var finalem = new EmbedBuilder()
2017-08-26 17:24:37 +00:00
{
Color = new Color(0xEDCE00), // configurable later?
Author = new EmbedAuthorBuilder()
{
Name = $"{msg.Author.Username}#{msg.Author.Discriminator} said:",
IconUrl = msg.Author.GetAvatarUrl()
},
Description = invokeLine,
Footer = new EmbedFooterBuilder()
{
Text = $"Rule '{Rule.Label}'",
IconUrl = Rule.Discord.CurrentUser.GetAvatarUrl()
},
Timestamp = msg.EditedTimestamp ?? msg.Timestamp
}.AddField(new EmbedFieldBuilder()
{
Name = "Context",
2017-08-26 17:24:37 +00:00
Value = $"Username: {msg.Author.Mention}\n"
2017-11-14 23:51:29 +00:00
+ $"Channel: <#{msg.Channel.Id}> #{msg.Channel.Name}\n"
2017-08-26 17:24:37 +00:00
+ $"Message ID: {msg.Id}"
});
if (responsebody != null)
{
finalem = finalem.AddField(new EmbedFieldBuilder()
{
Name = "Response:",
Value = responsebody.ToString()
});
}
return finalem;
2017-08-26 17:24:37 +00:00
}
}
}