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>();
|
||||||
|
foreach (var breaker in breakOn)
|
||||||
|
{
|
||||||
|
if (splitItems.Count == 0)
|
||||||
|
{
|
||||||
|
splitItems = content.Split(new[] { breaker }, StringSplitOptions.RemoveEmptyEntries).Select(x => x += breaker).ToList();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int i = 0; i < splitItems.Count; i++)
|
||||||
|
{
|
||||||
|
|
||||||
var temp = Regex.Split(content, breakOn).Select(x => x += breakOn).ToList();
|
var temp = splitItems[i];
|
||||||
string toolong;
|
if (temp.Length > characterLimit)
|
||||||
//while ((toolong = temp.FirstOrDefault(x => x.Length > 2000)) != null)
|
{
|
||||||
//{
|
var splitDeep = temp.Split(new[] { breaker }, StringSplitOptions.RemoveEmptyEntries).Select(x => x += breaker);
|
||||||
// more desperate measures == split on whitespace?
|
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();
|
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);
|
while (buildItems.Count > 0)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
if (i + 1 < temp.Count) builder.Append(addToEnd);
|
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)
|
||||||
|
{
|
||||||
|
list.Add(await msg.Channel.SendMessageAsync(builder.ToString()));
|
||||||
|
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()));
|
list.Add(await msg.Channel.SendMessageAsync(builder.ToString()));
|
||||||
builder.Clear();
|
builder.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return list.ToArray();
|
return list.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user