using System;
using System.Threading.Tasks;
using System.IO;
namespace Discord
{
/// An extension class for various Discord user objects.
public static class UserExtensions
{
///
/// Sends a message via DM.
///
///
/// This method attempts to send 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.
///
///
///
/// The user to send the DM to.
/// 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.
///
/// Specifies if notifications are sent for mentioned users and roles in the message .
/// If null, all mentioned roles and users will be notified.
///
///
/// A task that represents the asynchronous send operation. The task result contains the sent message.
///
public static async Task SendMessageAsync(this IUser user,
string text = null,
bool isTTS = false,
Embed embed = null,
RequestOptions options = null,
AllowedMentions allowedMentions = null)
{
return await (await user.GetOrCreateDMChannelAsync().ConfigureAwait(false)).SendMessageAsync(text, isTTS, embed, options, allowedMentions).ConfigureAwait(false);
}
///
/// Sends a file to this message channel with an optional caption.
///
///
/// The following example uploads a streamed image that will be called b1nzy.jpg embedded inside a
/// rich embed to the channel.
///
/// await channel.SendFileAsync(b1nzyStream, "b1nzy.jpg",
/// embed: new EmbedBuilder {ImageUrl = "attachment://b1nzy.jpg"}.Build());
///
///
///
/// 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 of the file to be sent.
/// The name of the attachment.
/// 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.
///
/// 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,
Stream stream,
string filename,
string text = null,
bool isTTS = false,
Embed embed = null,
RequestOptions options = null
)
{
return await (await user.GetOrCreateDMChannelAsync().ConfigureAwait(false)).SendFileAsync(stream, filename, text, isTTS, embed, options).ConfigureAwait(false);
}
///
/// Sends a file via DM with an optional caption.
///
///
/// The following example uploads a local file called wumpus.txt along with the text
/// good discord boi to the channel.
///
/// await channel.SendFileAsync("wumpus.txt", "good discord boi");
///
///
/// The following example uploads a local image called b1nzy.jpg embedded inside a rich embed to the
/// channel.
///
/// await channel.SendFileAsync("b1nzy.jpg",
/// embed: new EmbedBuilder {ImageUrl = "attachment://b1nzy.jpg"}.Build());
///
///
///
/// 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 file path of the file.
/// 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.
///
/// 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,
string filePath,
string text = null,
bool isTTS = false,
Embed embed = null,
RequestOptions options = null)
{
return await (await user.GetOrCreateDMChannelAsync().ConfigureAwait(false)).SendFileAsync(filePath, text, isTTS, embed, options).ConfigureAwait(false);
}
///
/// Bans the user from the guild and optionally prunes their recent messages.
///
/// The user to ban.
/// The number of days to remove messages from this for - must be between [0, 7]
/// The reason of the ban to be written in the audit log.
/// The options to be used when sending the request.
/// is not between 0 to 7.
///
/// A task that represents the asynchronous operation for banning a user.
///
public static Task BanAsync(this IGuildUser user, int pruneDays = 0, string reason = null, RequestOptions options = null)
=> user.Guild.AddBanAsync(user, pruneDays, reason, options);
}
}