-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathWebhook.cs
More file actions
59 lines (52 loc) · 1.8 KB
/
Webhook.cs
File metadata and controls
59 lines (52 loc) · 1.8 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
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace DiscordWebhook
{
[JsonObject]
public class Webhook
{
private readonly HttpClient _httpClient;
private readonly string _webhookUrl;
[JsonProperty("content")]
public string Content { get; set; }
[JsonProperty("username")]
public string Username { get; set; }
[JsonProperty("avatar_url")]
public string AvatarUrl { get; set; }
// ReSharper disable once InconsistentNaming
[JsonProperty("tts")]
public bool IsTTS { get; set; }
[JsonProperty("embeds")]
public List<Embed> Embeds { get; set; } = new List<Embed>();
public Webhook(string webhookUrl)
{
_httpClient = new HttpClient();
_webhookUrl = webhookUrl;
}
public Webhook(ulong id, string token) : this($"https://discordapp.com/api/webhooks/{id}/{token}")
{
}
public async Task<HttpResponseMessage> Send()
{
var content = new StringContent(JsonConvert.SerializeObject(this), Encoding.UTF8, "application/json");
return await _httpClient.PostAsync(_webhookUrl, content);
}
// ReSharper disable once InconsistentNaming
public async Task<HttpResponseMessage> Send(string content, string username = null, string avatarUrl = null, bool isTTS = false, IEnumerable<Embed> embeds = null)
{
Content = content;
Username = username;
AvatarUrl = avatarUrl;
IsTTS = isTTS;
Embeds.Clear();
if (embeds != null)
{
Embeds.AddRange(embeds);
}
return await Send();
}
}
}