Skip to content

Commit

Permalink
#336: Extension methods
Browse files Browse the repository at this point in the history
~ 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
jf-06 committed Oct 1, 2024
1 parent 4e5438c commit 5772194
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 0 deletions.
66 changes: 66 additions & 0 deletions services/grid-bot/lib/utility/Extensions/IUserMessageExtensions.cs
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 services/grid-bot/lib/utility/Extensions/ModuleBaseExtensions.cs
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
);
}

0 comments on commit 5772194

Please sign in to comment.