-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiscordService.cs
81 lines (71 loc) · 2.81 KB
/
DiscordService.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using WebhookSenderHub.Discord;
namespace WebhookSenderHub
{
/// <summary>
/// A webhook service implementation for sending messages to Discord.
/// </summary>
public sealed class DiscordService : WebhookService<MessageCard>, IMessagingService<MessageCard>
{
/// <summary>
/// Initializes a new instance of the <see cref="DiscordService"/> class using a URL.
/// </summary>
public DiscordService(string url) : base(url) { }
/// <summary>
/// Initializes a new instance of the <see cref="DiscordService"/> class using a URI.
/// </summary>
public DiscordService(Uri uri) : base(uri) { }
/// <inheritdoc/>
public async Task<bool> SendAsync(string text, string title, string summary, string themeColor, CancellationToken cancellationToken = default)
{
return await SendAsync(new MessageCard
{
Content = text,
Embeds = new List<Embed> { new Embed { Title = title, Description = summary, Color = HexToInt(themeColor) } }
}, cancellationToken);
}
/// <inheritdoc/>
public async Task<bool> SendAsync(string text, string title, string summary, CancellationToken cancellationToken = default)
{
return await SendAsync(new MessageCard
{
Content = text,
Embeds = new List<Embed> { new Embed { Title = title, Description = summary } }
}, cancellationToken);
}
/// <inheritdoc/>
public async Task<bool> SendAsync(string text, string title, CancellationToken cancellationToken = default)
{
return await SendAsync(new MessageCard
{
Content = text,
Embeds = new List<Embed> { new Embed { Title = title } }
}, cancellationToken);
}
/// <inheritdoc/>
public override async Task<bool> SendAsync(MessageCard message, CancellationToken cancellationToken = default)
{
return await base.SendAsync(message, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc/>
public override async Task<bool> SendAsync(string message, CancellationToken cancellationToken = default)
{
return await SendAsync(new MessageCard
{
Content = message
}, cancellationToken);
}
private int? HexToInt(string hex)
{
if (string.IsNullOrWhiteSpace(hex))
return null;
if (hex.StartsWith("#"))
hex = hex.TrimStart(new[] { '#' });
int result = Convert.ToInt32(hex, 16);
return result;
}
}
}