Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve some formatting from original ping. #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
bin/
obj/
logs/
database.db
database.db
.vs/
33 changes: 32 additions & 1 deletion src/Events/Handlers/MessageCreatedEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
Expand Down Expand Up @@ -45,6 +46,8 @@ public async Task ExecuteAsync(DiscordClient _, MessageCreateEventArgs eventArgs
mentionedUsers = mentionedUsers.Prepend(reply.Author!);
}

List<(string Formatting, string Message, bool IsUppercase)>? formattings = null;

// Only mention the users that the message intended to mention.
foreach (DiscordUser user in mentionedUsers)
{
Expand Down Expand Up @@ -87,7 +90,7 @@ public async Task ExecuteAsync(DiscordClient _, MessageCreateEventArgs eventArgs
try
{
DiscordMessageBuilder builder = new DiscordMessageBuilder()
.WithContent($"You were pinged by {eventArgs.Message.Author!.Mention} in {eventArgs.Channel.Mention}. [Jump! \u2197]({eventArgs.Message.JumpLink})");
.WithContent(GetDirectMessageContent(eventArgs, member, ref formattings));

if (shouldSilence)
{
Expand All @@ -108,4 +111,32 @@ public async Task ExecuteAsync(DiscordClient _, MessageCreateEventArgs eventArgs
}
}
}

[GeneratedRegex(@"^(?<FORMAT>((#{1,3}\s+)|(>{1}\s+)|(>{3}\s+)|(-\s+)|(\*\s+)|(\d+\.\s+))+)(?<MESSAGE>.*<\@!?(\d+?)>.*)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.ExplicitCapture)]
private static partial Regex FormattingRegex();

/// <summary>
/// Gets the content of the direct message to send to the user.
/// </summary>
/// <param name="eventArgs">The message event arguements to base formatting off of.</param>
/// <param name="member">The discord member being direct messaged.</param>
/// <param name="formattings">Cached formattings.</param>
/// <returns></returns>
private static string GetDirectMessageContent(MessageCreateEventArgs eventArgs, DiscordMember member, ref List<(string Formatting, string Message, bool IsUppercase)>? formattings)
{
// Setup the formattings lookup if they haven't been cached yet.
formattings ??= FormattingRegex()
.Matches(eventArgs.Message.Content)
.Select(match => (match.Groups[1].Value, match.Groups[2].Value, match.Groups[2].Value.Any(x => char.IsLetter(x) && !char.IsUpper(x)) == false))
.ToList();

string userMention = member.Mention;

// Grab formatting of the message where the user is mentioned.
(string Formatting, string Message, bool IsUppercase)? match = formattings.FirstOrDefault(x => x.Message.Contains(userMention));

return match?.IsUppercase == true
? $"{match?.Formatting}YOU WERE PINGED BY {eventArgs.Message.Author!.Mention} IN {eventArgs.Channel.Mention}!! [JUMP!!! \u2197]({eventArgs.Message.JumpLink})"
: $"{match?.Formatting}You were pinged by {eventArgs.Message.Author!.Mention} in {eventArgs.Channel.Mention}. [Jump! \u2197]({eventArgs.Message.JumpLink})";
}
}