Updated ReplyLong
This commit is contained in:
parent
f871ec339b
commit
9952cc3bee
@ -42,38 +42,75 @@ namespace NadekoBot.Extensions
|
|||||||
public static IEnumerable<IUser> Members(this IRole role) =>
|
public static IEnumerable<IUser> Members(this IRole role) =>
|
||||||
NadekoBot.Client.GetGuild(role.GuildId)?.GetUsers().Where(u => u.Roles.Contains(role)) ?? Enumerable.Empty<IUser>();
|
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 addToEnd = "", string addToStart = "")
|
||||||
{
|
{
|
||||||
|
if (content.Length == 0) return null;
|
||||||
if (content.Length < 2000) return new[] { await msg.Channel.SendMessageAsync(content).ConfigureAwait(false) };
|
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 list = new List<IUserMessage>();
|
||||||
|
var splitItems = new List<string>();
|
||||||
var temp = Regex.Split(content, breakOn).Select(x => x += breakOn).ToList();
|
foreach (var breaker in breakOn)
|
||||||
string toolong;
|
|
||||||
//while ((toolong = temp.FirstOrDefault(x => x.Length > 2000)) != null)
|
|
||||||
//{
|
|
||||||
// more desperate measures == split on whitespace?
|
|
||||||
//}
|
|
||||||
|
|
||||||
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];
|
if (splitItems.Count == 0)
|
||||||
//we append
|
{
|
||||||
|
splitItems = content.Split(new[] { breaker }, StringSplitOptions.RemoveEmptyEntries).Select(x => x += breaker).ToList();
|
||||||
if (builder.Length == 0 && i != 0) builder.Append(addToStart + addition);
|
}
|
||||||
else builder.Append(addition);
|
else
|
||||||
|
{
|
||||||
//Check if the next would have room
|
for (int i = 0; i < splitItems.Count; i++)
|
||||||
if (i + 1 >= temp.Count || temp[i + 1].Length + builder.Length + addToEnd.Length > 2000)
|
{
|
||||||
|
|
||||||
|
var temp = splitItems[i];
|
||||||
|
if (temp.Length > characterLimit)
|
||||||
|
{
|
||||||
|
var splitDeep = temp.Split(new[] { breaker }, StringSplitOptions.RemoveEmptyEntries).Select(x => x += breaker);
|
||||||
|
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>();
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
|
while (buildItems.Count > 0)
|
||||||
|
{
|
||||||
|
if (builder.Length == 0)
|
||||||
|
{
|
||||||
|
//first item to add
|
||||||
|
if (!firstItem)
|
||||||
|
builder.Append(addToStart);
|
||||||
|
else
|
||||||
|
firstItem = false;
|
||||||
|
builder.Append(buildItems.Dequeue());
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
builder.Append(buildItems.Dequeue());
|
||||||
|
}
|
||||||
|
if (buildItems.Count == 0)
|
||||||
{
|
{
|
||||||
if (i + 1 < temp.Count) builder.Append(addToEnd);
|
|
||||||
list.Add(await msg.Channel.SendMessageAsync(builder.ToString()));
|
list.Add(await msg.Channel.SendMessageAsync(builder.ToString()));
|
||||||
builder.Clear();
|
builder.Clear();
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
var peeked = buildItems.Peek();
|
||||||
|
if (builder.Length + peeked.Length + addToEnd.Length > characterLimit)
|
||||||
|
{
|
||||||
|
builder.Append(addToEnd);
|
||||||
|
list.Add(await msg.Channel.SendMessageAsync(builder.ToString()));
|
||||||
|
builder.Clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return list.ToArray();
|
return list.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user