-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cs
174 lines (155 loc) · 5.56 KB
/
Util.cs
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
using DisCatSharp.Entities;
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Threading.Tasks;
using DisCatSharp;
namespace BotName
{
public class Util
{
public static string FormatBytes(long bytes)
{
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
int i;
double dblSByte = bytes;
for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}
return string.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
}
public static DateTime ConvertStringToTime(string time, DateTime now)
{
DateTime until = now;
string builder = "";
foreach (char c in time)
{
switch (c)
{
case 's':
until = until.AddSeconds(int.Parse(builder));
builder = "";
break;
case 'm':
until = until.AddMinutes(int.Parse(builder));
builder = "";
break;
case 'h':
until = until.AddHours(double.Parse(builder));
builder = "";
break;
case 'd':
until = until.AddDays(double.Parse(builder));
builder = "";
break;
case 'w':
until = until.AddDays(double.Parse(builder) * 7);
builder = "";
break;
default:
builder += c;
break;
}
}
return until;
}
public static int WeekDiff(DateTime d1, DateTime d2, DayOfWeek startOfWeek = DayOfWeek.Monday)
{
var diff = d2.Subtract(d1);
var weeks = (int)diff.Days / 7;
// need to check if there's an extra week to count
var remainingDays = diff.Days % 7;
var cal = CultureInfo.InvariantCulture.Calendar;
var d1WeekNo = cal.GetWeekOfYear(d1, CalendarWeekRule.FirstFullWeek, startOfWeek);
var d1PlusRemainingWeekNo = cal.GetWeekOfYear(d1.AddDays(remainingDays), CalendarWeekRule.FirstFullWeek, startOfWeek);
if (d1WeekNo != d1PlusRemainingWeekNo)
weeks++;
return weeks;
}
public static string AddLint(string text) => "```ldif\n" + text + "```";
public static void Print(object obj)
{
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj))
{
string name = descriptor.Name;
object value = descriptor.GetValue(obj);
Console.WriteLine("{0} = {1}", name, value);
}
}
public static async Task<DiscordAuditLogEntry> TryGetAuditLogEntry(DiscordGuild guild, AuditLogActionType type)
{
try
{
var audits = await guild.GetAuditLogsAsync(1, actionType: type);
return (DiscordAuditLogEntry)(IReadOnlyList<DiscordAuditLogEntry>)audits.FirstOrDefault(e => DateTimeOffset.UtcNow - e.CreationTimestamp < TimeSpan.FromSeconds(30));
}
catch (Exception)
{
return null;
}
}
}
public static class Extensions
{
public static async void LogException(this Exception ex, DiscordClient client)
{
await client.GetGuildAsync(869686963828035644).Result.GetChannel(954735094231339099).GetWebhooksAsync().Result[0].ExecuteAsync(new DiscordWebhookBuilder()
{
Content = $"<@724702329693274114>, Error happened! {DateTime.Now}",
}.AddEmbed(new DiscordEmbedBuilder()
{
Title = ex.Message,
Description = ex.StackTrace
}));
}
public static List<Variance> CompareRoleDifference(this DiscordRole role, DiscordRole second)
{
return role.Compare(second);
}
}
public class Variance
{
public string PropertyName { get; set; }
public object ValA { get; set; }
public object ValB { get; set; }
}
public static class Comparision
{
public static List<Variance> Compare<T>(this T val1, T val2)
{
var variances = new List<Variance>();
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
var v = new Variance
{
PropertyName = property.Name,
ValA = property.GetValue(val1),
ValB = property.GetValue(val2)
};
if (v.ValA == null && v.ValB == null)
{
continue;
}
if (
(v.ValA == null && v.ValB != null)
||
(v.ValA != null && v.ValB == null)
)
{
variances.Add(v);
continue;
}
if (!v.ValA.Equals(v.ValB))
{
variances.Add(v);
}
}
return variances;
}
}
}