forked from KarlOfDuty/SupportBoi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.cs
More file actions
234 lines (211 loc) · 7.3 KB
/
Utilities.cs
File metadata and controls
234 lines (211 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus.Entities;
namespace SupportBoi;
public static class Extensions
{
private static readonly Random rng = new();
private static readonly DateTimeOffset UnixEpoch =
new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
public static long ToUnixTimeMicroseconds(this DateTimeOffset timestamp)
{
TimeSpan duration = timestamp - UnixEpoch;
// There are 10 ticks per microsecond.
return duration.Ticks / 10;
}
public static bool ContainsAny(this string haystack, params string[] needles)
{
return needles.Any(haystack.Contains);
}
public static bool ContainsAny(this string haystack, params char[] needles)
{
return needles.Any(haystack.Contains);
}
public static string Truncate(this string value, int maxChars)
{
return value.Length <= maxChars ? value : string.Concat(value.AsSpan(0, maxChars - 3), "...");
}
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]);
}
}
}
public static class Utilities
{
public class File(string fileName, Stream contents)
{
public string fileName = fileName;
public Stream contents = contents;
}
public class ConcurrentSet<T>
{
// This seems like a strange way of doing things but is apparently recommended
private readonly ConcurrentDictionary<T, byte> dict = new();
public bool Add(T item) => dict.TryAdd(item, 0);
public bool Remove(T item) => dict.TryRemove(item, out _);
public bool Contains(T item) => dict.ContainsKey(item);
public IEnumerable<T> Items => dict.Keys;
}
public static LinkedList<string> ParseListIntoMessages(List<string> listItems)
{
LinkedList<string> messages = new LinkedList<string>();
foreach (string listItem in listItems)
{
if (messages.Last?.Value?.Length + listItem?.Length < 2048)
{
messages.Last.Value += listItem;
}
else
{
messages.AddLast(listItem);
}
}
return messages;
}
public static async Task<List<Database.Category>> GetVerifiedCategories()
{
List<Database.Category> verifiedCategories = new List<Database.Category>();
foreach (Database.Category category in Database.Category.GetAllCategories())
{
DiscordChannel channel = null;
try
{
channel = await SupportBoi.client.GetChannelAsync(category.id);
}
catch (Exception) { /*ignored*/ }
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, Encoding.UTF8);
return reader.ReadToEnd();
}
public static DiscordColor StringToColor(string color)
{
switch (color.ToLower(CultureInfo.InvariantCulture))
{
case "black":
return DiscordColor.Black;
case "white":
return DiscordColor.White;
case "gray":
return DiscordColor.Gray;
case "darkgray":
return DiscordColor.DarkGray;
case "lightgray":
return DiscordColor.LightGray;
case "verydarkgray":
return DiscordColor.VeryDarkGray;
case "blurple":
return DiscordColor.Blurple;
case "grayple":
return DiscordColor.Grayple;
case "darkbutnotblack":
return DiscordColor.DarkButNotBlack;
case "notquiteblack":
return DiscordColor.NotQuiteBlack;
case "red":
return DiscordColor.Red;
case "darkred":
return DiscordColor.DarkRed;
case "green":
return DiscordColor.Green;
case "darkgreen":
return DiscordColor.DarkGreen;
case "blue":
return DiscordColor.Blue;
case "darkblue":
return DiscordColor.DarkBlue;
case "yellow":
return DiscordColor.Yellow;
case "cyan":
return DiscordColor.Cyan;
case "magenta":
return DiscordColor.Magenta;
case "teal":
return DiscordColor.Teal;
case "aquamarine":
return DiscordColor.Aquamarine;
case "gold":
return DiscordColor.Gold;
case "goldenrod":
return DiscordColor.Goldenrod;
case "azure":
return DiscordColor.Azure;
case "rose":
return DiscordColor.Rose;
case "springgreen":
return DiscordColor.SpringGreen;
case "chartreuse":
return DiscordColor.Chartreuse;
case "orange":
return DiscordColor.Orange;
case "purple":
return DiscordColor.Purple;
case "violet":
return DiscordColor.Violet;
case "brown":
return DiscordColor.Brown;
case "hotpink":
return DiscordColor.HotPink;
case "lilac":
return DiscordColor.Lilac;
case "cornflowerblue":
return DiscordColor.CornflowerBlue;
case "midnightblue":
return DiscordColor.MidnightBlue;
case "wheat":
return DiscordColor.Wheat;
case "indianred":
return DiscordColor.IndianRed;
case "turquoise":
return DiscordColor.Turquoise;
case "sapgreen":
return DiscordColor.SapGreen;
case "phthaloblue":
return DiscordColor.PhthaloBlue;
case "phthalogreen":
return DiscordColor.PhthaloGreen;
case "sienna":
return DiscordColor.Sienna;
default:
try
{
return new DiscordColor(color);
}
catch (Exception)
{
return DiscordColor.None;
}
}
}
}