You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserExtensions.cs 5.2 kB

7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.IO;
  4. namespace Discord
  5. {
  6. /// <summary> An extension class for various Discord user objects. </summary>
  7. public static class UserExtensions
  8. {
  9. /// <summary>
  10. /// Sends a message via DM.
  11. /// </summary>
  12. /// <param name="user">The user to send the DM to.</param>
  13. /// <param name="text">The message to be sent.</param>
  14. /// <param name="isTTS">Whether the message should be read aloud by Discord or not.</param>
  15. /// <param name="embed">The <see cref="EmbedType.Rich"/> <see cref="Embed"/> to be sent.</param>
  16. /// <param name="options">The options to be used when sending the request.</param>
  17. /// <returns>
  18. /// An awaitable Task containing the message sent to the channel.
  19. /// </returns>
  20. public static async Task<IUserMessage> SendMessageAsync(this IUser user,
  21. string text = null,
  22. bool isTTS = false,
  23. Embed embed = null,
  24. RequestOptions options = null)
  25. {
  26. return await (await user.GetOrCreateDMChannelAsync().ConfigureAwait(false)).SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
  27. }
  28. /// <summary>
  29. /// Sends a file to this message channel with an optional caption.
  30. /// </summary>
  31. /// <param name="user">The user to send the DM to.</param>
  32. /// <param name="stream">The <see cref="Stream"/> of the file to be sent.</param>
  33. /// <param name="filename">The name of the attachment.</param>
  34. /// <param name="text">The message to be sent.</param>
  35. /// <param name="isTTS">Whether the message should be read aloud by Discord or not.</param>
  36. /// <param name="embed">The <see cref="EmbedType.Rich"/> <see cref="Embed"/> to be sent.</param>
  37. /// <param name="options">The options to be used when sending the request.</param>
  38. /// <remarks>
  39. /// If you wish to upload an image and have it embedded in a <see cref="EmbedType.Rich"/> embed, you may
  40. /// upload the file and refer to the file with "attachment://filename.ext" in the
  41. /// <see cref="Discord.EmbedBuilder.ImageUrl"/>.
  42. /// </remarks>
  43. /// <returns>
  44. /// An awaitable Task containing the message sent to the channel.
  45. /// </returns>
  46. public static async Task<IUserMessage> SendFileAsync(this IUser user,
  47. Stream stream,
  48. string filename,
  49. string text = null,
  50. bool isTTS = false,
  51. Embed embed = null,
  52. RequestOptions options = null
  53. )
  54. {
  55. return await (await user.GetOrCreateDMChannelAsync().ConfigureAwait(false)).SendFileAsync(stream, filename, text, isTTS, embed, options).ConfigureAwait(false);
  56. }
  57. /// <summary>
  58. /// Sends a file via DM with an optional caption.
  59. /// </summary>
  60. /// <param name="user">The user to send the DM to.</param>
  61. /// <param name="filePath">The file path of the file.</param>
  62. /// <param name="text">The message to be sent.</param>
  63. /// <param name="isTTS">Whether the message should be read aloud by Discord or not.</param>
  64. /// <param name="embed">The <see cref="EmbedType.Rich"/> <see cref="Embed"/> to be sent.</param>
  65. /// <param name="options">The options to be used when sending the request.</param>
  66. /// <remarks>
  67. /// If you wish to upload an image and have it embedded in a <see cref="EmbedType.Rich"/> embed, you may
  68. /// upload the file and refer to the file with "attachment://filename.ext" in the
  69. /// <see cref="Discord.EmbedBuilder.ImageUrl"/>.
  70. /// </remarks>
  71. /// <returns>
  72. /// An awaitable Task containing the message sent to the channel.
  73. /// </returns>
  74. public static async Task<IUserMessage> SendFileAsync(this IUser user,
  75. string filePath,
  76. string text = null,
  77. bool isTTS = false,
  78. Embed embed = null,
  79. RequestOptions options = null)
  80. {
  81. return await (await user.GetOrCreateDMChannelAsync().ConfigureAwait(false)).SendFileAsync(filePath, text, isTTS, embed, options).ConfigureAwait(false);
  82. }
  83. /// <summary>
  84. /// Bans the provided user from the guild and optionally prunes their recent messages.
  85. /// </summary>
  86. /// <param name="user">The user to ban.</param>
  87. /// <param name="pruneDays">
  88. /// The number of days to remove messages from this user for - must be between [0, 7]
  89. /// </param>
  90. /// <param name="reason">The reason of the ban to be written in the audit log.</param>
  91. /// <param name="options">The options to be used when sending the request.</param>
  92. /// <exception cref="ArgumentException"><paramref name="pruneDays" /> is not between 0 to 7.</exception>
  93. public static Task BanAsync(this IGuildUser user, int pruneDays = 0, string reason = null, RequestOptions options = null)
  94. => user.Guild.AddBanAsync(user, pruneDays, reason, options);
  95. }
  96. }