-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
~ Create IUserMessageExtensions to add a method to reply with a file. ~ Create ModuleBaseExtensions to add functionality to reply with reference, reply with file, and reply with multiple files easily within module base.
- Loading branch information
Showing
2 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
services/grid-bot/lib/utility/Extensions/IUserMessageExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
namespace Grid.Bot.Extensions; | ||
|
||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
using Discord; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="IUserMessage"/> | ||
/// </summary> | ||
public static class IUserMessageExtensions | ||
{ | ||
/// <summary> | ||
/// Sends an inline reply that references a message. | ||
/// </summary> | ||
/// <param name="msg">The message that is being replied on.</param> | ||
/// <param name="fileStream">The stream of the file to send.</param> | ||
/// <param name="fileName">The name of the file to send.</param> | ||
/// <param name="text">The message to be sent.</param> | ||
/// <param name="isTTS">Determines whether the message should be read aloud by Discord or not.</param> | ||
/// <param name="embed">The <see cref="Discord.EmbedType.Rich"/> <see cref="Embed"/> to be sent.</param> | ||
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param> | ||
/// <param name="allowedMentions"> | ||
/// Specifies if notifications are sent for mentioned users and roles in the message <paramref name="text"/>. | ||
/// If <see langword="null" />, all mentioned roles and users will be notified. | ||
/// </param> | ||
/// <param name="options">The options to be used when sending the request.</param> | ||
/// <param name="isSpoiler">Determines whether the file should be sent as a spoiler or not.</param> | ||
/// <param name="components">The message components to be included with this message. Used for interactions.</param> | ||
/// <param name="stickers">A collection of stickers to send with the message.</param> | ||
/// <param name="flags">Message flags combined as a bitfield.</param> | ||
/// <returns> | ||
/// A task that represents an asynchronous send operation for delivering the message. The task result | ||
/// contains the sent message. | ||
/// </returns> | ||
public static Task<IUserMessage> ReplyWithFileAsync( | ||
this IUserMessage msg, | ||
Stream fileStream, | ||
string fileName, | ||
string text = null, | ||
bool isTTS = false, | ||
Embed embed = null, | ||
AllowedMentions allowedMentions = null, | ||
RequestOptions options = null, | ||
bool isSpoiler = false, | ||
MessageComponent components = null, | ||
ISticker[] stickers = null, | ||
Embed[] embeds = null, | ||
MessageFlags flags = MessageFlags.None | ||
) | ||
=> msg.Channel.SendFileAsync( | ||
fileStream, | ||
fileName, | ||
text, | ||
isTTS, | ||
embed, | ||
options, | ||
isSpoiler, | ||
allowedMentions, | ||
new MessageReference(messageId: msg.Id), | ||
components, | ||
stickers, | ||
embeds, | ||
flags | ||
); | ||
} |
163 changes: 163 additions & 0 deletions
163
services/grid-bot/lib/utility/Extensions/ModuleBaseExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
namespace Grid.Bot.Extensions; | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
using Discord; | ||
using Discord.Commands; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="IUserMessage"/> | ||
/// </summary> | ||
public static class ModuleBaseExtensions | ||
{ | ||
/// <summary> | ||
/// Sends an inline reply that references a message. | ||
/// </summary> | ||
/// <param name="module">The module in use.</param> | ||
/// <param name="text">The message to be sent.</param> | ||
/// <param name="isTTS">Determines whether the message should be read aloud by Discord or not.</param> | ||
/// <param name="embed">The <see cref="Discord.EmbedType.Rich"/> <see cref="Embed"/> to be sent.</param> | ||
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param> | ||
/// <param name="allowedMentions"> | ||
/// Specifies if notifications are sent for mentioned users and roles in the message <paramref name="text"/>. | ||
/// If <see langword="null" />, all mentioned roles and users will be notified. | ||
/// </param> | ||
/// <param name="options">The options to be used when sending the request.</param> | ||
/// <param name="components">The message components to be included with this message. Used for interactions.</param> | ||
/// <param name="stickers">A collection of stickers to send with the message.</param> | ||
/// <param name="flags">Message flags combined as a bitfield.</param> | ||
/// <returns> | ||
/// A task that represents an asynchronous send operation for delivering the message. The task result | ||
/// contains the sent message. | ||
/// </returns> | ||
public static Task<IUserMessage> ReplyWithReferenceAsync( | ||
this ModuleBase module, | ||
string text = null, | ||
bool isTTS = false, | ||
Embed embed = null, | ||
AllowedMentions allowedMentions = null, | ||
RequestOptions options = null, | ||
MessageComponent components = null, | ||
ISticker[] stickers = null, | ||
Embed[] embeds = null, | ||
MessageFlags flags = MessageFlags.None | ||
) | ||
=> module.Context.Channel.SendMessageAsync( | ||
text, | ||
isTTS, | ||
embed, | ||
options, | ||
allowedMentions, | ||
new MessageReference(messageId: module.Context.Message.Id), | ||
components, | ||
stickers, | ||
embeds, | ||
flags | ||
); | ||
|
||
/// <summary> | ||
/// Sends an inline reply that references a message. | ||
/// </summary> | ||
/// <param name="module">The module in use.</param> | ||
/// <param name="fileStream">The stream of the file to send.</param> | ||
/// <param name="fileName">The name of the file to send.</param> | ||
/// <param name="text">The message to be sent.</param> | ||
/// <param name="isTTS">Determines whether the message should be read aloud by Discord or not.</param> | ||
/// <param name="embed">The <see cref="Discord.EmbedType.Rich"/> <see cref="Embed"/> to be sent.</param> | ||
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param> | ||
/// <param name="allowedMentions"> | ||
/// Specifies if notifications are sent for mentioned users and roles in the message <paramref name="text"/>. | ||
/// If <see langword="null" />, all mentioned roles and users will be notified. | ||
/// </param> | ||
/// <param name="options">The options to be used when sending the request.</param> | ||
/// <param name="isSpoiler">Determines whether the file should be sent as a spoiler or not.</param> | ||
/// <param name="components">The message components to be included with this message. Used for interactions.</param> | ||
/// <param name="stickers">A collection of stickers to send with the message.</param> | ||
/// <param name="flags">Message flags combined as a bitfield.</param> | ||
/// <returns> | ||
/// A task that represents an asynchronous send operation for delivering the message. The task result | ||
/// contains the sent message. | ||
/// </returns> | ||
public static Task<IUserMessage> ReplyWithFileAsync( | ||
this ModuleBase module, | ||
Stream fileStream, | ||
string fileName, | ||
string text = null, | ||
bool isTTS = false, | ||
Embed embed = null, | ||
AllowedMentions allowedMentions = null, | ||
RequestOptions options = null, | ||
bool isSpoiler = false, | ||
MessageComponent components = null, | ||
ISticker[] stickers = null, | ||
Embed[] embeds = null, | ||
MessageFlags flags = MessageFlags.None | ||
) | ||
=> module.Context.Channel.SendFileAsync( | ||
fileStream, | ||
fileName, | ||
text, | ||
isTTS, | ||
embed, | ||
options, | ||
isSpoiler, | ||
allowedMentions, | ||
new MessageReference(messageId: module.Context.Message.Id), | ||
components, | ||
stickers, | ||
embeds, | ||
flags | ||
); | ||
|
||
/// <summary> | ||
/// Sends an inline reply that references a message. | ||
/// </summary> | ||
/// <param name="module">The module in use.</param> | ||
/// <param name="attachments">The file attachments send.</param> | ||
/// <param name="fileName">The name of the file to send.</param> | ||
/// <param name="text">The message to be sent.</param> | ||
/// <param name="isTTS">Determines whether the message should be read aloud by Discord or not.</param> | ||
/// <param name="embed">The <see cref="Discord.EmbedType.Rich"/> <see cref="Embed"/> to be sent.</param> | ||
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param> | ||
/// <param name="allowedMentions"> | ||
/// Specifies if notifications are sent for mentioned users and roles in the message <paramref name="text"/>. | ||
/// If <see langword="null" />, all mentioned roles and users will be notified. | ||
/// </param> | ||
/// <param name="options">The options to be used when sending the request.</param> | ||
/// <param name="components">The message components to be included with this message. Used for interactions.</param> | ||
/// <param name="stickers">A collection of stickers to send with the message.</param> | ||
/// <param name="flags">Message flags combined as a bitfield.</param> | ||
/// <returns> | ||
/// A task that represents an asynchronous send operation for delivering the message. The task result | ||
/// contains the sent message. | ||
/// </returns> | ||
public static Task<IUserMessage> ReplyWithFilesAsync( | ||
this ModuleBase module, | ||
IEnumerable<FileAttachment> attachments, | ||
string fileName, | ||
string text = null, | ||
bool isTTS = false, | ||
Embed embed = null, | ||
AllowedMentions allowedMentions = null, | ||
RequestOptions options = null, | ||
MessageComponent components = null, | ||
ISticker[] stickers = null, | ||
Embed[] embeds = null, | ||
MessageFlags flags = MessageFlags.None | ||
) | ||
=> module.Context.Channel.SendFilesAsync( | ||
attachments, | ||
text, | ||
isTTS, | ||
embed, | ||
options, | ||
allowedMentions, | ||
new MessageReference(messageId: module.Context.Message.Id), | ||
components, | ||
stickers, | ||
embeds, | ||
flags | ||
); | ||
} |