Merge pull request #623 from appelemac/Reply-improv

Reply-long improvements
This commit is contained in:
Master Kwoth 2016-09-10 23:30:57 +02:00 committed by GitHub
commit ec2ca03272
2 changed files with 65 additions and 27 deletions

View File

@ -113,7 +113,7 @@ namespace NadekoBot.Modules.Searches
}
sb.AppendLine("```");
}
await msg.ReplyLong(sb.ToString(), "```xl", "```", "```xl");
await msg.ReplyLong(sb.ToString(), breakOn: new[] { "```xl", "\n" });
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)]
@ -127,7 +127,7 @@ namespace NadekoBot.Modules.Searches
sb.AppendLine(string.Join(",", group.Select(x => x.Triggers.FirstOrDefault()).OrderBy(x => x)));
sb.AppendLine("```");
}
await msg.ReplyLong(sb.ToString(), "```xl", "```", "```xl");
await msg.ReplyLong(sb.ToString(), breakOn: new[] { "```xl\n", "\n" });
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
public async Task Convert(IUserMessage msg, string origin, string target, decimal value)

View File

@ -42,38 +42,76 @@ namespace NadekoBot.Extensions
public static IEnumerable<IUser> Members(this IRole role) =>
NadekoBot.Client.GetGuild(role.GuildId)?.GetUsers().Where(u => u.Roles.Contains(role)) ?? Enumerable.Empty<IUser>();
public static async Task<IUserMessage[]> ReplyLong(this IUserMessage msg, string content, string breakOn = "\n", string addToEnd = "", string addToStart = "")
public static async Task<IUserMessage[]> ReplyLong(this IUserMessage msg, string content, string[] breakOn = null, string addToPartialEnd = "", string addToPartialStart = "")
{
if (content.Length < 2000) return new[] { await msg.Channel.SendMessageAsync(content).ConfigureAwait(false) };
if (content.Length == 0) return null;
var characterLimit = 1750;
if (content.Length < characterLimit) return new[] { await msg.Channel.SendMessageAsync(content).ConfigureAwait(false) };
if (breakOn == null) breakOn = new[] { "\n", " ", " " };
var list = new List<IUserMessage>();
var temp = Regex.Split(content, breakOn).Select(x => x += breakOn).ToList();
string toolong;
//while ((toolong = temp.FirstOrDefault(x => x.Length > 2000)) != null)
//{
// more desperate measures == split on whitespace?
//}
var splitItems = new List<string>();
foreach (var breaker in breakOn)
{
if (splitItems.Count == 0)
{
splitItems = Regex.Split(content, $"(?={breaker})").Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
}
else
{
for (int i = 0; i < splitItems.Count; i++)
{
var temp = splitItems[i];
if (temp.Length > characterLimit)
{
var splitDeep = Regex.Split(temp, $"(?={breaker})").Where(s => !string.IsNullOrWhiteSpace(s));
splitItems.RemoveAt(i);
splitItems.InsertRange(i, splitDeep);
}
}
}
if (splitItems.All(s => s.Length < characterLimit)) break;
}
//We remove any entries that are larger than 2000 chars
if (splitItems.Any(s => s.Length >= characterLimit))
{
splitItems = splitItems.Where(s => s.Length < characterLimit).ToList();
}
//ensured every item can be sent (if individually)
var firstItem = true;
Queue<string> buildItems = new Queue<string>(splitItems);
StringBuilder builder = new StringBuilder();
//make this less crappy to look at, maybe it's bugged
for (int i = 0; i < temp.Count; i++)
{
var addition = temp[i];
//we append
if (builder.Length == 0 && i != 0) builder.Append(addToStart + addition);
else builder.Append(addition);
//Check if the next would have room
if (i + 1 >= temp.Count || temp[i + 1].Length + builder.Length + addToEnd.Length > 2000)
while (buildItems.Count > 0)
{
if (i + 1 < temp.Count) builder.Append(addToEnd);
if (builder.Length == 0)
{
//first item to add
if (!firstItem)
builder.Append(addToPartialStart);
else
firstItem = false;
builder.Append(buildItems.Dequeue());
}
else
{
builder.Append(buildItems.Dequeue());
}
if (buildItems.Count == 0)
{
list.Add(await msg.Channel.SendMessageAsync(builder.ToString()));
builder.Clear();
}
else
{
var peeked = buildItems.Peek();
if (builder.Length + peeked.Length + addToPartialEnd.Length > characterLimit)
{
builder.Append(addToPartialEnd);
list.Add(await msg.Channel.SendMessageAsync(builder.ToString()));
builder.Clear();
}
}
}
return list.ToArray();
}