Browse Source

Added Clean Content Function (#174)

* Added Clean Content Function

* Fixed Spelling problems and bad var handling

* Add StripMarkDown Method
pull/1923/head
Emily GitHub 3 years ago
parent
commit
2cf9896f78
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 0 deletions
  1. +7
    -0
      src/Discord.Net.Core/Discord.Net.Core.xml
  2. +13
    -0
      src/Discord.Net.Core/Format.cs
  3. +3
    -0
      src/Discord.Net.WebSocket/Discord.Net.WebSocket.xml
  4. +14
    -0
      src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs
  5. +15
    -0
      test/Discord.Net.Tests.Unit/FormatTests.cs

+ 7
- 0
src/Discord.Net.Core/Discord.Net.Core.xml View File

@@ -11423,6 +11423,13 @@
<param name="text">The text to format.</param> <param name="text">The text to format.</param>
<returns>Gets the formatted block quote text.</returns> <returns>Gets the formatted block quote text.</returns>
</member> </member>
<member name="M:Discord.Format.StripMarkDown(System.String)">
<summary>
Remove discord supported markdown from text.
</summary>
<param name="text">The to remove markdown from.</param>
<returns>Gets the unformatted text.</returns>
</member>
<member name="F:Discord.GatewayIntents.None"> <member name="F:Discord.GatewayIntents.None">
<summary> This intent includes no events </summary> <summary> This intent includes no events </summary>
</member> </member>


+ 13
- 0
src/Discord.Net.Core/Format.cs View File

@@ -1,4 +1,5 @@
using System.Text; using System.Text;
using System.Text.RegularExpressions;


namespace Discord namespace Discord
{ {
@@ -91,5 +92,17 @@ namespace Discord


return $">>> {text}"; return $">>> {text}";
} }

/// <summary>
/// Remove discord supported markdown from text.
/// </summary>
/// <param name="text">The to remove markdown from.</param>
/// <returns>Gets the unformatted text.</returns>
public static string StripMarkDown(string text)
{
//Remove discord supported markdown
var newText = Regex.Replace(text, @"(\*|_|`|~|>|\\)", "");
return newText;
}
} }
} }

+ 3
- 0
src/Discord.Net.WebSocket/Discord.Net.WebSocket.xml View File

@@ -4531,6 +4531,9 @@
<member name="P:Discord.WebSocket.SocketMessage.Content"> <member name="P:Discord.WebSocket.SocketMessage.Content">
<inheritdoc /> <inheritdoc />
</member> </member>
<member name="P:Discord.WebSocket.SocketMessage.CleanContent">
<inheritdoc />
</member>
<member name="P:Discord.WebSocket.SocketMessage.CreatedAt"> <member name="P:Discord.WebSocket.SocketMessage.CreatedAt">
<inheritdoc /> <inheritdoc />
</member> </member>


+ 14
- 0
src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs View File

@@ -38,6 +38,9 @@ namespace Discord.WebSocket
/// <inheritdoc /> /// <inheritdoc />
public string Content { get; private set; } public string Content { get; private set; }


/// <inheritdoc />
public string CleanContent => SanitizeMessage();

/// <inheritdoc /> /// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id); public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
/// <inheritdoc /> /// <inheritdoc />
@@ -139,7 +142,11 @@ namespace Discord.WebSocket
_timestampTicks = model.Timestamp.Value.UtcTicks; _timestampTicks = model.Timestamp.Value.UtcTicks;


if (model.Content.IsSpecified) if (model.Content.IsSpecified)
{
Content = model.Content.Value; Content = model.Content.Value;
//Update CleanContent Property
SanitizeMessage();
}


if (model.Application.IsSpecified) if (model.Application.IsSpecified)
{ {
@@ -265,6 +272,13 @@ namespace Discord.WebSocket
/// <inheritdoc /> /// <inheritdoc />
IReadOnlyCollection<IStickerItem> IMessage.Stickers => Stickers; IReadOnlyCollection<IStickerItem> IMessage.Stickers => Stickers;


internal string SanitizeMessage()
{
var newContent = MentionUtils.Resolve(this, 0, TagHandling.Sanitize, TagHandling.Sanitize, TagHandling.Sanitize, TagHandling.Sanitize, TagHandling.Sanitize);
newContent = Format.StripMarkDown(newContent);
return newContent;
}

internal void AddReaction(SocketReaction reaction) internal void AddReaction(SocketReaction reaction)
{ {
_reactions.Add(reaction); _reactions.Add(reaction);


+ 15
- 0
test/Discord.Net.Tests.Unit/FormatTests.cs View File

@@ -59,5 +59,20 @@ namespace Discord
{ {
Assert.Equal(expected, Format.BlockQuote(input)); Assert.Equal(expected, Format.BlockQuote(input));
} }

[Theory]
[InlineData("", "")]
[InlineData("\n", "\n")]
[InlineData("**hi**", "hi")]
[InlineData("__uwu__", "uwu")]
[InlineData(">>__uwu__", "uwu")]
[InlineData("```uwu```", "uwu")]
[InlineData("~uwu~", "uwu")]
[InlineData("berries __and__ *Cream**, i'm a little lad who loves berries and cream", "berries and Cream, i'm a little lad who loves berries and cream")]
public void StripMarkdown(string input, string expected)
{
var test = Format.StripMarkDown(input);
Assert.Equal(expected, test);
}
} }
} }

Loading…
Cancel
Save