From 25cfb8822fe63adfff4cb493326ad8915e634dbc Mon Sep 17 00:00:00 2001
From: BokuNoPasya <49203428+1NieR@users.noreply.github.com>
Date: Wed, 14 Dec 2022 12:00:59 +0500
Subject: [PATCH] Add SendFiles to UserExtensions (#2509)
* Add SendFiles to UserExtensions
* fix Build
---
.../Extensions/UserExtensions.cs | 91 +++++++++++++++++++
.../Entities/Channels/IRestMessageChannel.cs | 6 ++
.../Channels/ISocketMessageChannel.cs | 13 +++
3 files changed, 110 insertions(+)
diff --git a/src/Discord.Net.Core/Extensions/UserExtensions.cs b/src/Discord.Net.Core/Extensions/UserExtensions.cs
index ce914170d..469957c42 100644
--- a/src/Discord.Net.Core/Extensions/UserExtensions.cs
+++ b/src/Discord.Net.Core/Extensions/UserExtensions.cs
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
+using System.Collections.Generic;
using System.IO;
namespace Discord
@@ -163,6 +164,96 @@ namespace Discord
return await (await user.CreateDMChannelAsync().ConfigureAwait(false)).SendFileAsync(filePath, text, isTTS, embed, options, components: components, embeds: embeds).ConfigureAwait(false);
}
+ ///
+ /// Sends a file via DM with an optional caption.
+ ///
+ ///
+ /// This method attempts to send an attachment as a direct-message to the user.
+ ///
+ ///
+ /// Please note that this method will throw an
+ /// if the user cannot receive DMs due to privacy reasons or if the user has the sender blocked.
+ ///
+ ///
+ /// You may want to consider catching for
+ /// 50007 when using this method.
+ ///
+ ///
+ ///
+ /// If you wish to upload an image and have it embedded in a embed,
+ /// you may upload the file and refer to the file with "attachment://filename.ext" in the
+ /// . See the example section for its usage.
+ ///
+ ///
+ /// The user to send the DM to.
+ /// The attachment containing the file and description.
+ /// The message to be sent.
+ /// Whether the message should be read aloud by Discord or not.
+ /// The to be sent.
+ /// The options to be used when sending the request.
+ /// The message component to be included with this message. Used for interactions.
+ /// A array of s to send with this response. Max 10.
+ ///
+ /// A task that represents an asynchronous send operation for delivering the message. The task result
+ /// contains the sent message.
+ ///
+ public static async Task SendFileAsync(this IUser user,
+ FileAttachment attachment,
+ string text = null,
+ bool isTTS = false,
+ Embed embed = null,
+ RequestOptions options = null,
+ MessageComponent components = null,
+ Embed[] embeds = null)
+ {
+ return await (await user.CreateDMChannelAsync().ConfigureAwait(false)).SendFileAsync(attachment, text, isTTS, embed, options, components: components, embeds: embeds).ConfigureAwait(false);
+ }
+
+ ///
+ /// Sends a collection of files via DM.
+ ///
+ ///
+ /// This method attempts to send an attachments as a direct-message to the user.
+ ///
+ ///
+ /// Please note that this method will throw an
+ /// if the user cannot receive DMs due to privacy reasons or if the user has the sender blocked.
+ ///
+ ///
+ /// You may want to consider catching for
+ /// 50007 when using this method.
+ ///
+ ///
+ ///
+ /// If you wish to upload an image and have it embedded in a embed,
+ /// you may upload the file and refer to the file with "attachment://filename.ext" in the
+ /// . See the example section for its usage.
+ ///
+ ///
+ /// The user to send the DM to.
+ /// A collection of attachments to upload.
+ /// The message to be sent.
+ /// Whether the message should be read aloud by Discord or not.
+ /// The to be sent.
+ /// The options to be used when sending the request.
+ /// The message component to be included with this message. Used for interactions.
+ /// A array of s to send with this response. Max 10.
+ ///
+ /// A task that represents an asynchronous send operation for delivering the message. The task result
+ /// contains the sent message.
+ ///
+ public static async Task SendFilesAsync(this IUser user,
+ IEnumerable attachments,
+ string text = null,
+ bool isTTS = false,
+ Embed embed = null,
+ RequestOptions options = null,
+ MessageComponent components = null,
+ Embed[] embeds = null)
+ {
+ return await (await user.CreateDMChannelAsync().ConfigureAwait(false)).SendFilesAsync(attachments, text, isTTS, embed, options, components: components, embeds: embeds).ConfigureAwait(false);
+ }
+
///
/// Bans the user from the guild and optionally prunes their recent messages.
///
diff --git a/src/Discord.Net.Rest/Entities/Channels/IRestMessageChannel.cs b/src/Discord.Net.Rest/Entities/Channels/IRestMessageChannel.cs
index 0cf92bb04..678db093f 100644
--- a/src/Discord.Net.Rest/Entities/Channels/IRestMessageChannel.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/IRestMessageChannel.cs
@@ -17,6 +17,12 @@ namespace Discord.Rest
///
new Task SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None);
+
+ ///
+ new Task SendFileAsync(FileAttachment attachment, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None);
+
+ ///
+ new Task SendFilesAsync(IEnumerable attachments, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None);
///
/// Gets a message from this message channel.
diff --git a/src/Discord.Net.WebSocket/Entities/Channels/ISocketMessageChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/ISocketMessageChannel.cs
index b632bcb60..ea4249cf0 100644
--- a/src/Discord.Net.WebSocket/Entities/Channels/ISocketMessageChannel.cs
+++ b/src/Discord.Net.WebSocket/Entities/Channels/ISocketMessageChannel.cs
@@ -35,6 +35,19 @@ namespace Discord.WebSocket
MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null,
Embed[] embeds = null, MessageFlags flags = MessageFlags.None);
+ ///
+ new Task SendFileAsync(FileAttachment attachment, string text = null, bool isTTS = false,
+ Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null,
+ MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null,
+ Embed[] embeds = null, MessageFlags flags = MessageFlags.None);
+
+ ///
+ new Task SendFilesAsync(IEnumerable attachments, string text = null,
+ bool isTTS = false, Embed embed = null, RequestOptions options = null,
+ AllowedMentions allowedMentions = null, MessageReference messageReference = null,
+ MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null,
+ MessageFlags flags = MessageFlags.None);
+
///
/// Gets a cached message from this channel.
///