Create forum posts with DiscordWebhookClient (#2603)
This commit is contained in:
@@ -42,6 +42,9 @@ namespace Discord.API.Rest
|
|||||||
[JsonProperty("file")]
|
[JsonProperty("file")]
|
||||||
public Optional<MultipartFile> File { get; set; }
|
public Optional<MultipartFile> File { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("thread_name")]
|
||||||
|
public Optional<string> ThreadName { get; set; }
|
||||||
|
|
||||||
public IReadOnlyDictionary<string, object> ToDictionary()
|
public IReadOnlyDictionary<string, object> ToDictionary()
|
||||||
{
|
{
|
||||||
var d = new Dictionary<string, object>();
|
var d = new Dictionary<string, object>();
|
||||||
@@ -70,6 +73,8 @@ namespace Discord.API.Rest
|
|||||||
payload["allowed_mentions"] = AllowedMentions.Value;
|
payload["allowed_mentions"] = AllowedMentions.Value;
|
||||||
if (Components.IsSpecified)
|
if (Components.IsSpecified)
|
||||||
payload["components"] = Components.Value;
|
payload["components"] = Components.Value;
|
||||||
|
if (ThreadName.IsSpecified)
|
||||||
|
payload["thread_name"] = ThreadName.Value;
|
||||||
|
|
||||||
var json = new StringBuilder();
|
var json = new StringBuilder();
|
||||||
using (var text = new StringWriter(json))
|
using (var text = new StringWriter(json))
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ namespace Discord.API.Rest
|
|||||||
public Optional<AllowedMentions> AllowedMentions { get; set; }
|
public Optional<AllowedMentions> AllowedMentions { get; set; }
|
||||||
public Optional<ActionRowComponent[]> MessageComponents { get; set; }
|
public Optional<ActionRowComponent[]> MessageComponents { get; set; }
|
||||||
public Optional<MessageFlags> Flags { get; set; }
|
public Optional<MessageFlags> Flags { get; set; }
|
||||||
|
public Optional<string> ThreadName { get; set; }
|
||||||
|
|
||||||
public UploadWebhookFileParams(params FileAttachment[] files)
|
public UploadWebhookFileParams(params FileAttachment[] files)
|
||||||
{
|
{
|
||||||
@@ -51,6 +52,8 @@ namespace Discord.API.Rest
|
|||||||
payload["allowed_mentions"] = AllowedMentions.Value;
|
payload["allowed_mentions"] = AllowedMentions.Value;
|
||||||
if (Flags.IsSpecified)
|
if (Flags.IsSpecified)
|
||||||
payload["flags"] = Flags.Value;
|
payload["flags"] = Flags.Value;
|
||||||
|
if (ThreadName.IsSpecified)
|
||||||
|
payload["thread_name"] = ThreadName.Value;
|
||||||
|
|
||||||
List<object> attachments = new();
|
List<object> attachments = new();
|
||||||
|
|
||||||
|
|||||||
@@ -88,8 +88,8 @@ namespace Discord.Webhook
|
|||||||
/// <returns> Returns the ID of the created message. </returns>
|
/// <returns> Returns the ID of the created message. </returns>
|
||||||
public Task<ulong> SendMessageAsync(string text = null, bool isTTS = false, IEnumerable<Embed> embeds = null,
|
public Task<ulong> SendMessageAsync(string text = null, bool isTTS = false, IEnumerable<Embed> embeds = null,
|
||||||
string username = null, string avatarUrl = null, RequestOptions options = null, AllowedMentions allowedMentions = null,
|
string username = null, string avatarUrl = null, RequestOptions options = null, AllowedMentions allowedMentions = null,
|
||||||
MessageComponent components = null, MessageFlags flags = MessageFlags.None, ulong? threadId = null)
|
MessageComponent components = null, MessageFlags flags = MessageFlags.None, ulong? threadId = null, string threadName = null)
|
||||||
=> WebhookClientHelper.SendMessageAsync(this, text, isTTS, embeds, username, avatarUrl, allowedMentions, options, components, flags, threadId);
|
=> WebhookClientHelper.SendMessageAsync(this, text, isTTS, embeds, username, avatarUrl, allowedMentions, options, components, flags, threadId, threadName);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Modifies a message posted using this webhook.
|
/// Modifies a message posted using this webhook.
|
||||||
@@ -125,35 +125,35 @@ namespace Discord.Webhook
|
|||||||
public Task<ulong> SendFileAsync(string filePath, string text, bool isTTS = false,
|
public Task<ulong> SendFileAsync(string filePath, string text, bool isTTS = false,
|
||||||
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null,
|
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null,
|
||||||
RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null,
|
RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null,
|
||||||
MessageComponent components = null, MessageFlags flags = MessageFlags.None, ulong? threadId = null)
|
MessageComponent components = null, MessageFlags flags = MessageFlags.None, ulong? threadId = null, string threadName = null)
|
||||||
=> WebhookClientHelper.SendFileAsync(this, filePath, text, isTTS, embeds, username, avatarUrl,
|
=> WebhookClientHelper.SendFileAsync(this, filePath, text, isTTS, embeds, username, avatarUrl,
|
||||||
allowedMentions, options, isSpoiler, components, flags, threadId);
|
allowedMentions, options, isSpoiler, components, flags, threadId, threadName);
|
||||||
/// <summary> Sends a message to the channel for this webhook with an attachment. </summary>
|
/// <summary> Sends a message to the channel for this webhook with an attachment. </summary>
|
||||||
/// <returns> Returns the ID of the created message. </returns>
|
/// <returns> Returns the ID of the created message. </returns>
|
||||||
public Task<ulong> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false,
|
public Task<ulong> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false,
|
||||||
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null,
|
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null,
|
||||||
RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null,
|
RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null,
|
||||||
MessageComponent components = null, MessageFlags flags = MessageFlags.None, ulong? threadId = null)
|
MessageComponent components = null, MessageFlags flags = MessageFlags.None, ulong? threadId = null, string threadName = null)
|
||||||
=> WebhookClientHelper.SendFileAsync(this, stream, filename, text, isTTS, embeds, username,
|
=> WebhookClientHelper.SendFileAsync(this, stream, filename, text, isTTS, embeds, username,
|
||||||
avatarUrl, allowedMentions, options, isSpoiler, components, flags, threadId);
|
avatarUrl, allowedMentions, options, isSpoiler, components, flags, threadId, threadName);
|
||||||
|
|
||||||
/// <summary> Sends a message to the channel for this webhook with an attachment. </summary>
|
/// <summary> Sends a message to the channel for this webhook with an attachment. </summary>
|
||||||
/// <returns> Returns the ID of the created message. </returns>
|
/// <returns> Returns the ID of the created message. </returns>
|
||||||
public Task<ulong> SendFileAsync(FileAttachment attachment, string text, bool isTTS = false,
|
public Task<ulong> SendFileAsync(FileAttachment attachment, string text, bool isTTS = false,
|
||||||
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null,
|
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null,
|
||||||
RequestOptions options = null, AllowedMentions allowedMentions = null, MessageComponent components = null,
|
RequestOptions options = null, AllowedMentions allowedMentions = null, MessageComponent components = null,
|
||||||
MessageFlags flags = MessageFlags.None, ulong? threadId = null)
|
MessageFlags flags = MessageFlags.None, ulong? threadId = null, string threadName = null)
|
||||||
=> WebhookClientHelper.SendFileAsync(this, attachment, text, isTTS, embeds, username,
|
=> WebhookClientHelper.SendFileAsync(this, attachment, text, isTTS, embeds, username,
|
||||||
avatarUrl, allowedMentions, components, options, flags, threadId);
|
avatarUrl, allowedMentions, components, options, flags, threadId, threadName);
|
||||||
|
|
||||||
/// <summary> Sends a message to the channel for this webhook with an attachment. </summary>
|
/// <summary> Sends a message to the channel for this webhook with an attachment. </summary>
|
||||||
/// <returns> Returns the ID of the created message. </returns>
|
/// <returns> Returns the ID of the created message. </returns>
|
||||||
public Task<ulong> SendFilesAsync(IEnumerable<FileAttachment> attachments, string text, bool isTTS = false,
|
public Task<ulong> SendFilesAsync(IEnumerable<FileAttachment> attachments, string text, bool isTTS = false,
|
||||||
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null,
|
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null,
|
||||||
RequestOptions options = null, AllowedMentions allowedMentions = null, MessageComponent components = null,
|
RequestOptions options = null, AllowedMentions allowedMentions = null, MessageComponent components = null,
|
||||||
MessageFlags flags = MessageFlags.None, ulong? threadId = null)
|
MessageFlags flags = MessageFlags.None, ulong? threadId = null, string threadName = null)
|
||||||
=> WebhookClientHelper.SendFilesAsync(this, attachments, text, isTTS, embeds, username, avatarUrl,
|
=> WebhookClientHelper.SendFilesAsync(this, attachments, text, isTTS, embeds, username, avatarUrl,
|
||||||
allowedMentions, components, options, flags, threadId);
|
allowedMentions, components, options, flags, threadId, threadName);
|
||||||
|
|
||||||
|
|
||||||
/// <summary> Modifies the properties of this webhook. </summary>
|
/// <summary> Modifies the properties of this webhook. </summary>
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
using Discord.API.Rest;
|
using Discord.API.Rest;
|
||||||
using Discord.Rest;
|
using Discord.Rest;
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using ImageModel = Discord.API.Image;
|
using ImageModel = Discord.API.Image;
|
||||||
using WebhookModel = Discord.API.Webhook;
|
using WebhookModel = Discord.API.Webhook;
|
||||||
|
|
||||||
@@ -22,7 +24,7 @@ namespace Discord.Webhook
|
|||||||
}
|
}
|
||||||
public static async Task<ulong> SendMessageAsync(DiscordWebhookClient client,
|
public static async Task<ulong> SendMessageAsync(DiscordWebhookClient client,
|
||||||
string text, bool isTTS, IEnumerable<Embed> embeds, string username, string avatarUrl,
|
string text, bool isTTS, IEnumerable<Embed> embeds, string username, string avatarUrl,
|
||||||
AllowedMentions allowedMentions, RequestOptions options, MessageComponent components, MessageFlags flags, ulong? threadId = null)
|
AllowedMentions allowedMentions, RequestOptions options, MessageComponent components, MessageFlags flags, ulong? threadId = null, string threadName = null)
|
||||||
{
|
{
|
||||||
var args = new CreateWebhookMessageParams
|
var args = new CreateWebhookMessageParams
|
||||||
{
|
{
|
||||||
@@ -41,6 +43,8 @@ namespace Discord.Webhook
|
|||||||
args.AllowedMentions = allowedMentions.ToModel();
|
args.AllowedMentions = allowedMentions.ToModel();
|
||||||
if (components != null)
|
if (components != null)
|
||||||
args.Components = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray();
|
args.Components = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray();
|
||||||
|
if (threadName is not null)
|
||||||
|
args.ThreadName = threadName;
|
||||||
|
|
||||||
if (flags is not MessageFlags.None and not MessageFlags.SuppressEmbeds)
|
if (flags is not MessageFlags.None and not MessageFlags.SuppressEmbeds)
|
||||||
throw new ArgumentException("The only valid MessageFlags are SuppressEmbeds and none.", nameof(flags));
|
throw new ArgumentException("The only valid MessageFlags are SuppressEmbeds and none.", nameof(flags));
|
||||||
@@ -98,32 +102,35 @@ namespace Discord.Webhook
|
|||||||
await client.ApiClient.ModifyWebhookMessageAsync(client.Webhook.Id, messageId, apiArgs, options, threadId)
|
await client.ApiClient.ModifyWebhookMessageAsync(client.Webhook.Id, messageId, apiArgs, options, threadId)
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task DeleteMessageAsync(DiscordWebhookClient client, ulong messageId, RequestOptions options, ulong? threadId)
|
public static async Task DeleteMessageAsync(DiscordWebhookClient client, ulong messageId, RequestOptions options, ulong? threadId)
|
||||||
{
|
{
|
||||||
await client.ApiClient.DeleteWebhookMessageAsync(client.Webhook.Id, messageId, options, threadId).ConfigureAwait(false);
|
await client.ApiClient.DeleteWebhookMessageAsync(client.Webhook.Id, messageId, options, threadId).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, string filePath, string text, bool isTTS,
|
public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, string filePath, string text, bool isTTS,
|
||||||
IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options,
|
IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options,
|
||||||
bool isSpoiler, MessageComponent components, MessageFlags flags = MessageFlags.None, ulong? threadId = null)
|
bool isSpoiler, MessageComponent components, MessageFlags flags = MessageFlags.None, ulong? threadId = null, string threadName = null)
|
||||||
{
|
{
|
||||||
string filename = Path.GetFileName(filePath);
|
string filename = Path.GetFileName(filePath);
|
||||||
using (var file = File.OpenRead(filePath))
|
using (var file = File.OpenRead(filePath))
|
||||||
return await SendFileAsync(client, file, filename, text, isTTS, embeds, username, avatarUrl, allowedMentions, options, isSpoiler, components, flags, threadId).ConfigureAwait(false);
|
return await SendFileAsync(client, file, filename, text, isTTS, embeds, username, avatarUrl, allowedMentions, options, isSpoiler, components, flags, threadId, threadName).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS,
|
public static Task<ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS,
|
||||||
IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler,
|
IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler,
|
||||||
MessageComponent components, MessageFlags flags, ulong? threadId)
|
MessageComponent components, MessageFlags flags, ulong? threadId, string threadName = null)
|
||||||
=> SendFileAsync(client, new FileAttachment(stream, filename, isSpoiler: isSpoiler), text, isTTS, embeds, username, avatarUrl, allowedMentions, components, options, flags, threadId);
|
=> SendFileAsync(client, new FileAttachment(stream, filename, isSpoiler: isSpoiler), text, isTTS, embeds, username, avatarUrl, allowedMentions, components, options, flags, threadId, threadName);
|
||||||
|
|
||||||
public static Task<ulong> SendFileAsync(DiscordWebhookClient client, FileAttachment attachment, string text, bool isTTS,
|
public static Task<ulong> SendFileAsync(DiscordWebhookClient client, FileAttachment attachment, string text, bool isTTS,
|
||||||
IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions,
|
IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions,
|
||||||
MessageComponent components, RequestOptions options, MessageFlags flags, ulong? threadId)
|
MessageComponent components, RequestOptions options, MessageFlags flags, ulong? threadId, string threadName = null)
|
||||||
=> SendFilesAsync(client, new FileAttachment[] { attachment }, text, isTTS, embeds, username, avatarUrl, allowedMentions, components, options, flags, threadId);
|
=> SendFilesAsync(client, new FileAttachment[] { attachment }, text, isTTS, embeds, username, avatarUrl, allowedMentions, components, options, flags, threadId, threadName);
|
||||||
|
|
||||||
public static async Task<ulong> SendFilesAsync(DiscordWebhookClient client,
|
public static async Task<ulong> SendFilesAsync(DiscordWebhookClient client,
|
||||||
IEnumerable<FileAttachment> attachments, string text, bool isTTS, IEnumerable<Embed> embeds, string username,
|
IEnumerable<FileAttachment> attachments, string text, bool isTTS, IEnumerable<Embed> embeds, string username,
|
||||||
string avatarUrl, AllowedMentions allowedMentions, MessageComponent components, RequestOptions options,
|
string avatarUrl, AllowedMentions allowedMentions, MessageComponent components, RequestOptions options,
|
||||||
MessageFlags flags, ulong? threadId)
|
MessageFlags flags, ulong? threadId, string threadName = null)
|
||||||
{
|
{
|
||||||
embeds ??= Array.Empty<Embed>();
|
embeds ??= Array.Empty<Embed>();
|
||||||
|
|
||||||
@@ -164,7 +171,8 @@ namespace Discord.Webhook
|
|||||||
Embeds = embeds.Any() ? embeds.Select(x => x.ToModel()).ToArray() : Optional<API.Embed[]>.Unspecified,
|
Embeds = embeds.Any() ? embeds.Select(x => x.ToModel()).ToArray() : Optional<API.Embed[]>.Unspecified,
|
||||||
AllowedMentions = allowedMentions?.ToModel() ?? Optional<API.AllowedMentions>.Unspecified,
|
AllowedMentions = allowedMentions?.ToModel() ?? Optional<API.AllowedMentions>.Unspecified,
|
||||||
MessageComponents = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional<API.ActionRowComponent[]>.Unspecified,
|
MessageComponents = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional<API.ActionRowComponent[]>.Unspecified,
|
||||||
Flags = flags
|
Flags = flags,
|
||||||
|
ThreadName = threadName
|
||||||
};
|
};
|
||||||
var msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options, threadId).ConfigureAwait(false);
|
var msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options, threadId).ConfigureAwait(false);
|
||||||
return msg.Id;
|
return msg.Id;
|
||||||
|
|||||||
Reference in New Issue
Block a user