Added Discord.Net.Webhook
This commit is contained in:
28
src/Discord.Net.Rest/API/Rest/CreateWebhookMessageParams.cs
Normal file
28
src/Discord.Net.Rest/API/Rest/CreateWebhookMessageParams.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma warning disable CS1591
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Discord.API.Rest
|
||||
{
|
||||
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
|
||||
internal class CreateWebhookMessageParams
|
||||
{
|
||||
[JsonProperty("content")]
|
||||
public string Content { get; }
|
||||
|
||||
[JsonProperty("nonce")]
|
||||
public Optional<string> Nonce { get; set; }
|
||||
[JsonProperty("tts")]
|
||||
public Optional<bool> IsTTS { get; set; }
|
||||
[JsonProperty("embeds")]
|
||||
public Optional<Embed[]> Embeds { get; set; }
|
||||
[JsonProperty("username")]
|
||||
public Optional<string> Username { get; set; }
|
||||
[JsonProperty("avatar_url")]
|
||||
public Optional<string> AvatarUrl { get; set; }
|
||||
|
||||
public CreateWebhookMessageParams(string content)
|
||||
{
|
||||
Content = content;
|
||||
}
|
||||
}
|
||||
}
|
||||
41
src/Discord.Net.Rest/API/Rest/UploadWebhookFileParams.cs
Normal file
41
src/Discord.Net.Rest/API/Rest/UploadWebhookFileParams.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma warning disable CS1591
|
||||
using Discord.Net.Rest;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace Discord.API.Rest
|
||||
{
|
||||
internal class UploadWebhookFileParams
|
||||
{
|
||||
public Stream File { get; }
|
||||
|
||||
public Optional<string> Filename { get; set; }
|
||||
public Optional<string> Content { get; set; }
|
||||
public Optional<string> Nonce { get; set; }
|
||||
public Optional<bool> IsTTS { get; set; }
|
||||
public Optional<string> Username { get; set; }
|
||||
public Optional<string> AvatarUrl { get; set; }
|
||||
|
||||
public UploadWebhookFileParams(Stream file)
|
||||
{
|
||||
File = file;
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<string, object> ToDictionary()
|
||||
{
|
||||
var d = new Dictionary<string, object>();
|
||||
d["file"] = new MultipartFile(File, Filename.GetValueOrDefault("unknown.dat"));
|
||||
if (Content.IsSpecified)
|
||||
d["content"] = Content.Value;
|
||||
if (IsTTS.IsSpecified)
|
||||
d["tts"] = IsTTS.Value.ToString();
|
||||
if (Nonce.IsSpecified)
|
||||
d["nonce"] = Nonce.Value;
|
||||
if (Username.IsSpecified)
|
||||
d["username"] = Username.Value;
|
||||
if (AvatarUrl.IsSpecified)
|
||||
d["avatar_url"] = AvatarUrl.Value;
|
||||
return d;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,5 +2,6 @@
|
||||
|
||||
[assembly: InternalsVisibleTo("Discord.Net.Rpc")]
|
||||
[assembly: InternalsVisibleTo("Discord.Net.WebSocket")]
|
||||
[assembly: InternalsVisibleTo("Discord.Net.Webhook")]
|
||||
[assembly: InternalsVisibleTo("Discord.Net.Commands")]
|
||||
[assembly: InternalsVisibleTo("Discord.Net.Tests")]
|
||||
@@ -120,7 +120,8 @@ namespace Discord.API
|
||||
|
||||
AuthTokenType = tokenType;
|
||||
AuthToken = token;
|
||||
RestClient.SetHeader("authorization", GetPrefixedToken(AuthTokenType, AuthToken));
|
||||
if (tokenType != TokenType.Webhook)
|
||||
RestClient.SetHeader("authorization", GetPrefixedToken(AuthTokenType, AuthToken));
|
||||
|
||||
LoginState = LoginState.LoggedIn;
|
||||
}
|
||||
@@ -438,8 +439,8 @@ namespace Discord.API
|
||||
}
|
||||
public async Task<Message> CreateMessageAsync(ulong channelId, CreateMessageParams args, RequestOptions options = null)
|
||||
{
|
||||
Preconditions.NotEqual(channelId, 0, nameof(channelId));
|
||||
Preconditions.NotNull(args, nameof(args));
|
||||
Preconditions.NotEqual(channelId, 0, nameof(channelId));
|
||||
if (!args.Embed.IsSpecified || args.Embed.Value == null)
|
||||
Preconditions.NotNullOrEmpty(args.Content, nameof(args.Content));
|
||||
|
||||
@@ -450,6 +451,22 @@ namespace Discord.API
|
||||
var ids = new BucketIds(channelId: channelId);
|
||||
return await SendJsonAsync<Message>("POST", () => $"channels/{channelId}/messages", args, ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
|
||||
}
|
||||
public async Task CreateWebhookMessageAsync(ulong webhookId, CreateWebhookMessageParams args, RequestOptions options = null)
|
||||
{
|
||||
if (AuthTokenType != TokenType.Webhook)
|
||||
throw new InvalidOperationException($"This operation may only be called with a {nameof(TokenType.Webhook)} token.");
|
||||
|
||||
Preconditions.NotNull(args, nameof(args));
|
||||
Preconditions.NotEqual(webhookId, 0, nameof(webhookId));
|
||||
if (!args.Embeds.IsSpecified || args.Embeds.Value == null || args.Embeds.Value.Length == 0)
|
||||
Preconditions.NotNullOrEmpty(args.Content, nameof(args.Content));
|
||||
|
||||
if (args.Content.Length > DiscordConfig.MaxMessageSize)
|
||||
throw new ArgumentException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content));
|
||||
options = RequestOptions.CreateOrClone(options);
|
||||
|
||||
await SendJsonAsync("POST", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
|
||||
}
|
||||
public async Task<Message> UploadFileAsync(ulong channelId, UploadFileParams args, RequestOptions options = null)
|
||||
{
|
||||
Preconditions.NotNull(args, nameof(args));
|
||||
@@ -469,6 +486,27 @@ namespace Discord.API
|
||||
var ids = new BucketIds(channelId: channelId);
|
||||
return await SendMultipartAsync<Message>("POST", () => $"channels/{channelId}/messages", args.ToDictionary(), ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
|
||||
}
|
||||
public async Task UploadWebhookFileAsync(ulong webhookId, UploadWebhookFileParams args, RequestOptions options = null)
|
||||
{
|
||||
if (AuthTokenType != TokenType.Webhook)
|
||||
throw new InvalidOperationException($"This operation may only be called with a {nameof(TokenType.Webhook)} token.");
|
||||
|
||||
Preconditions.NotNull(args, nameof(args));
|
||||
Preconditions.NotEqual(webhookId, 0, nameof(webhookId));
|
||||
options = RequestOptions.CreateOrClone(options);
|
||||
|
||||
if (args.Content.GetValueOrDefault(null) == null)
|
||||
args.Content = "";
|
||||
else if (args.Content.IsSpecified)
|
||||
{
|
||||
if (args.Content.Value == null)
|
||||
args.Content = "";
|
||||
if (args.Content.Value?.Length > DiscordConfig.MaxMessageSize)
|
||||
throw new ArgumentOutOfRangeException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content));
|
||||
}
|
||||
|
||||
await SendMultipartAsync("POST", () => $"webhooks/{webhookId}/{AuthToken}", args.ToDictionary(), new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
|
||||
}
|
||||
public async Task DeleteMessageAsync(ulong channelId, ulong messageId, RequestOptions options = null)
|
||||
{
|
||||
Preconditions.NotEqual(channelId, 0, nameof(channelId));
|
||||
|
||||
Reference in New Issue
Block a user