-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectMessage.cs
30 lines (27 loc) · 1021 Bytes
/
DirectMessage.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
using System.Text.Json;
namespace Discord;
public class DirectMessage
{
readonly DiscordBot bot;
public DirectMessage(DiscordBot bot)
=> this.bot = bot;
public async Task Send(string recipientId, string message)
{
string dmId = await InitDm(recipientId);
await SendDm(dmId, message);
}
private Task SendDm(string dmId, string message)
=> bot.RequestAsync(HttpMethod.Post, $"/channels/{dmId}/messages"
, new
{
content = message
});
private async Task<string> InitDm(string recipientId)
{
var httpResponse = await bot.RequestAsync(HttpMethod.Post, "/users/@me/channels"
, new { recipient_id = recipientId });
string response = await httpResponse.Content.ReadAsStringAsync();
JsonDocument document = JsonDocument.Parse(response);
return document.RootElement.GetProperty("id").GetString() ?? "";
}
}