SupportChild/Utilities.cs

84 lines
2.4 KiB
C#
Raw Normal View History

2022-04-18 10:52:03 +00:00
using System;
2022-02-21 08:40:09 +00:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
2022-08-21 07:33:27 +00:00
using System.Threading.Tasks;
2022-02-21 08:40:09 +00:00
using DSharpPlus.Entities;
2022-08-21 07:33:27 +00:00
namespace SupportChild;
2022-02-21 08:40:09 +00:00
2022-08-21 07:33:27 +00:00
public static class Utilities
{
private static readonly Random rng = new Random();
2022-02-21 08:40:09 +00:00
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
(list[k], list[n]) = (list[n], list[k]);
}
}
2022-02-21 08:40:09 +00:00
public static LinkedList<string> ParseListIntoMessages(List<string> listItems)
{
LinkedList<string> messages = new LinkedList<string>();
2022-02-21 08:40:09 +00:00
foreach (string listItem in listItems)
{
if (messages.Last?.Value?.Length + listItem?.Length < 2048)
{
messages.Last.Value += listItem;
}
else
{
messages.AddLast(listItem);
}
}
2022-02-21 08:40:09 +00:00
return messages;
}
2022-02-21 08:40:09 +00:00
public static async Task<List<Database.Category>> GetVerifiedChannels()
{
List<Database.Category> verifiedCategories = new List<Database.Category>();
foreach (Database.Category category in Database.GetAllCategories())
{
DiscordChannel channel = null;
try
{
channel = await SupportChild.discordClient.GetChannelAsync(category.id);
}
catch (Exception) { /*ignored*/ }
2022-02-21 08:40:09 +00:00
if (channel != null)
{
verifiedCategories.Add(category);
}
else
{
Logger.Warn("Category '" + category.name + "' (" + category.id + ") no longer exists! Ignoring...");
}
}
return verifiedCategories;
}
public static string ReadManifestData(string embeddedFileName)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string resourceName = assembly.GetManifestResourceNames().First(s => s.EndsWith(embeddedFileName, StringComparison.CurrentCultureIgnoreCase));
using Stream stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null)
{
throw new InvalidOperationException("Could not load manifest resource stream.");
}
using StreamReader reader = new StreamReader(stream);
return reader.ReadToEnd();
}
2022-05-17 13:11:57 +00:00
}