-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathUtilities.cs
195 lines (185 loc) · 7.43 KB
/
Utilities.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using RestoreCord.Schema.DiscordModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace RestoreCord.Miscellaneous
{
public static class Extensions
{
public static string getHeader(this WebHeaderCollection header, string key)
{
for (int i = 0; i < header.Count; i++)
{
if (header.GetKey(i) == key)
return header.Get(i);
}
return null;
}
public static async Task<HttpStatusCode> AddUserToGuild(this SocketSlashCommand cmd, Schema.Member user, Schema.Server server)
{
try
{
var web = new WebClient();
web.Headers.Add("Authorization", $"Bot {Properties.Resources.Token}");
web.Headers.Add("X-RateLimit-Precision", "millisecond");
web.Headers.Add("User-Agent", "RestoreCord (public release, 1.0.0.0)");
web.Headers.Add("Content-Type", $"application/json");
string data = null;
if (server.roleid is null)
{
data = JsonConvert.SerializeObject(new { access_token = user.access_token });
}
else
{
data = JsonConvert.SerializeObject(new
{
access_token = user.access_token,
roles = new ulong[]
{
(ulong)server.roleid
},
});
}
if (JsonConvert.DeserializeObject<API.JoinGuildInfo>
(web.UploadString($"https://discord.com/api/guilds/{server.guildid}/members/{user.userid}", WebRequestMethods.Http.Put, data)) is null)
return HttpStatusCode.BadRequest;
return HttpStatusCode.OK;
}
catch (WebException webex)
{
var response = (HttpWebResponse)webex.Response;
switch (response.StatusCode)
{
case HttpStatusCode.TooManyRequests:
var headervalue = webex.Response.Headers.getHeader("Retry-After");
if (headervalue is not null)
{
Thread.Sleep(Convert.ToInt32(headervalue));
if (await cmd.AddUserToGuild(user, server) == HttpStatusCode.OK)
return HttpStatusCode.OK;
}
return response.StatusCode;
case HttpStatusCode.NoContent://in the guild already
return HttpStatusCode.OK;
default:
//await cmd.SendEmbedAsync("add user exception", $"{response.StatusCode}\n{webex}\n{response}");
return response.StatusCode;
}
}
}
public static async Task LogErrorAsync(this Exception e)
{
try
{
var db = new Services.Database();
await db.AddAsync(new Schema.Log.Errors
{
ErrorTime = DateTime.Now,
Location = e.Source,
Reason = e.Message,
Name = e.TargetSite.Name
});
await db.SaveChangesAsync();
}
catch (DbUpdateException updateError)
{
//error saving the db, txt/email me the error and/or log to discord
}
catch (Exception error)
{
}
}
public static async Task LogErrorAsync(this Exception e, SocketSlashCommand context)
{
try
{
var db = new Services.Database();
db.Add(new Schema.Log.Errors
{
ErrorTime = DateTime.Now,
Location = e.Source,
Reason = e.Message,
Name = e.TargetSite.Name
});
await db.SaveChangesAsync();
await context.SendEmbedAsync("Application Error", $"Error has be logged to the database.\nMessage: {e.Message}");
}
catch (DbUpdateException updateError)
{
//error saving the db, txt/email me the error and/or log to discord
}
catch (Exception error)
{
}
}
public static async Task SendEmbedAsync(this SocketSlashCommand context, string title, string description, int? deleteTimer = null)
{
var embed = new EmbedBuilder()
{
Title = title,
Color = Utilities.RandomDiscordColour(),
Author = new EmbedAuthorBuilder
{
Url = "https://restorecord.com",
Name = "RestoreCord",
IconUrl = "https://i.imgur.com/Nfy4OoG.png"
},
Footer = new EmbedFooterBuilder
{
Text = $"Issued by: {context.User.Username} | {context.User.Id}",
IconUrl = context.User.GetAvatarUrl()
},
Description = description,
}.WithCurrentTimestamp().Build();
IUserMessage msg = null;
msg = await context.Channel.SendMessageAsync(embed: embed);
if (msg is not null && deleteTimer is not null)
{
_ = Task.Run(async () => { await Task.Delay((int)deleteTimer); await msg.DeleteAsync(); });
}
}
public static async Task ReplyWithEmbedAsync(this SocketSlashCommand context, string title, string description, int? deleteTimer = null)
{
var embed = new EmbedBuilder()
{
Title = title,
Color = Utilities.RandomDiscordColour(),
Author = new EmbedAuthorBuilder
{
Url = "https://restorecord.com",
Name = "RestoreCord",
IconUrl = "https://i.imgur.com/Nfy4OoG.png"
},
Footer = new EmbedFooterBuilder
{
Text = $"Issued by: {context.User.Username} | {context.User.Id}",
IconUrl = context.User.GetAvatarUrl()
},
Description = description,
}.WithCurrentTimestamp().Build();
IUserMessage msg = null;
await context.RespondAsync(embed: embed);
if (msg is not null && deleteTimer is not null)
{
_ = Task.Run(async () => { await Task.Delay((int)deleteTimer); await msg.DeleteAsync(); });
}
}
}
public static class Utilities
{
public static Color RandomDiscordColour()
{
return new Color(new Random().Next(0, 255), new Random().Next(0, 255), new Random().Next(0, 255));
}
}
}