mirror of
https://onedev.fprog.nl/DiscordClient
synced 2025-12-30 16:08:37 +01:00
83 lines
2.9 KiB
C#
83 lines
2.9 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json.Serialization;
|
|
using DiscordClient.Data;
|
|
|
|
namespace DiscordClient;
|
|
|
|
public class DiscordUserClient
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
|
|
public DiscordUserClient(string authorizationToken)
|
|
{
|
|
_httpClient = new HttpClient();
|
|
_httpClient.DefaultRequestHeaders.Host = "discord.com";
|
|
_httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(
|
|
authorizationToken
|
|
);
|
|
}
|
|
|
|
public async Task<ChannelMessagesResponse[]?> GetChannelMessages(
|
|
string channelId,
|
|
int offset = 0,
|
|
string? authorId = null,
|
|
int limit = 50
|
|
)
|
|
{
|
|
string authorQuery = authorId is not null ? $"&author_id={authorId}" : "";
|
|
string url =
|
|
$"https://discord.com/api/v9/channels/{channelId}/messages?limit={limit}&offset={offset}{authorQuery}";
|
|
HttpResponseMessage response = await _httpClient.GetAsync(url);
|
|
return await response.Content.ReadFromJsonAsync<ChannelMessagesResponse[]>();
|
|
}
|
|
|
|
sealed class MessageContent
|
|
{
|
|
[JsonPropertyName("content")]
|
|
public string? Content { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// This method does NOT work yet.
|
|
/// </summary>
|
|
/// <param name="channelId"></param>
|
|
/// <param name="message"></param>
|
|
/// <returns></returns>
|
|
public async Task<HttpResponseMessage> SendMessage(string channelId, string message)
|
|
{
|
|
throw new NotImplementedException();
|
|
string url = $"https://discord.com/api/v9/channels/{channelId}/messages";
|
|
HttpContent httpContent = JsonContent.Create(new MessageContent { Content = message });
|
|
return await _httpClient.PostAsync(url, httpContent);
|
|
}
|
|
|
|
public async Task<HttpResponseMessage> DeleteMessage(string channelId, string messageId)
|
|
{
|
|
string url = $"https://discord.com/api/v9/channels/{channelId}/messages/{messageId}";
|
|
return await _httpClient.DeleteAsync(url);
|
|
}
|
|
|
|
/// <summary>
|
|
/// This method returns some of most recent messages, sent to a guild.
|
|
/// </summary>
|
|
/// <param name="guildId">Guild ID from which we're gathering messages.</param>
|
|
/// <param name="authorId">
|
|
/// Optional author ID. If specified, gather only
|
|
/// recent messages from this particular author.
|
|
/// </param>
|
|
/// <returns></returns>
|
|
public async Task<GuildMessageSearchResponse?> SearchGuildMessages(
|
|
string guildId,
|
|
ulong offset,
|
|
string? authorId = null
|
|
)
|
|
{
|
|
string authorQuery = authorId is not null ? $"&author_id={authorId}" : "";
|
|
string url =
|
|
$"https://discord.com/api/v9/guilds/{guildId}/messages/search?offset={offset}{authorQuery}";
|
|
HttpResponseMessage response = await _httpClient.GetAsync(url);
|
|
return await response.Content.ReadFromJsonAsync<GuildMessageSearchResponse>();
|
|
}
|
|
}
|