mirror of
https://onedev.fprog.nl/DiscordClient
synced 2025-12-28 16:58:37 +01:00
68 lines
2.3 KiB
C#
68 lines
2.3 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 limit = 50
|
|
)
|
|
{
|
|
string url = $"https://discord.com/api/v9/channels/{channelId}/messages?limit={limit}";
|
|
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);
|
|
}
|
|
|
|
public async Task<GuildMessageSearchResponse?> SearchGuildMessages(
|
|
string guildId,
|
|
string? authorId = null
|
|
)
|
|
{
|
|
string authorQuery = authorId is not null ? $"?author_id={authorId}" : "";
|
|
string url = $"https://discord.com/api/v9/guilds/{guildId}/messages/search{authorQuery}";
|
|
HttpResponseMessage response = await _httpClient.GetAsync(url);
|
|
return await response.Content.ReadFromJsonAsync<GuildMessageSearchResponse>();
|
|
}
|
|
}
|