File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11using System . Data ;
2+ using System . Text ;
23using System . Text . RegularExpressions ;
34using Elementalist . Models ;
5+ using Elementalist . Shared ;
46using ElementalistBot . Infrastructure . DataAccess . Rules ;
7+ using NetCord ;
58using NetCord . Rest ;
69using NetCord . Services . ApplicationCommands ;
710
@@ -66,7 +69,16 @@ private static EmbedProperties CreateCodexRuleEmbed(CodexEntry rule)
6669
6770 foreach ( var subCodex in rule . Subcodexes )
6871 {
69- codexEmbed . AddFields ( new EmbedFieldProperties ( ) . WithName ( subCodex . Title ) . WithValue ( subCodex . Content ) ) ;
72+ var continued = string . Empty ;
73+ foreach ( var fieldContentChunk in subCodex . Content . ChunkStringOnWords ( 1024 ) ) //discord's max field length
74+ {
75+ codexEmbed . AddFields ( new EmbedFieldProperties ( )
76+ . WithName ( subCodex . Title + continued )
77+ . WithValue ( new string ( fieldContentChunk ) )
78+ ) ;
79+
80+ continued = " - continued" ;
81+ }
7082 }
7183
7284 return codexEmbed ;
Original file line number Diff line number Diff line change 1+ using System . Text ;
2+
3+ namespace Elementalist . Shared ;
4+
5+ public static class StringChunker
6+ {
7+ public static IEnumerable < string > ChunkStringOnWords ( this string value , int maxLength )
8+ {
9+ var words = value . Split ( [ ' ' ] ) ;
10+ var sb = new StringBuilder ( ) ;
11+ int currentLength = 0 ;
12+
13+ foreach ( var word in words )
14+ {
15+ if ( currentLength + word . Length < maxLength )
16+ {
17+ sb . Append ( word ) . Append ( ' ' ) ;
18+ currentLength += word . Length + 1 ;
19+ continue ;
20+ }
21+
22+ var wordChunk = sb . ToString ( ) . Trim ( ) ;
23+ sb = new StringBuilder ( ) ;
24+ sb . Append ( word ) . Append ( ' ' ) ;
25+ currentLength = word . Length + 1 ;
26+
27+ yield return wordChunk ;
28+ }
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ using Elementalist . Shared ;
2+ using Xunit ;
3+
4+ namespace ElementalistTests . Shared ;
5+
6+ public class StringChunkerTests
7+ {
8+ [ Fact ]
9+ public void ChunkStringOnWords ( )
10+ {
11+ var testSentence = "The quick brown fox jumped over the lazy dog." ;
12+ var chunked = testSentence . ChunkStringOnWords ( 12 ) ;
13+
14+ Assert . True ( chunked . All ( chunk => chunk . Length <= 12 ) ) ;
15+ Assert . Equal ( "brown fox" , chunked . ElementAt ( 1 ) ) ;
16+ }
17+ }
You can’t perform that action at this time.
0 commit comments