@@ -34,45 +34,77 @@ public static string UploadMarkDown(string input)
34
34
35
35
public static string MentionToUsername ( string input , SocketUserMessage message )
36
36
{
37
- string returnString = message . Content ;
37
+ Regex regex = new Regex ( "<@!?([0-9]+)>" ) ; // create patern
38
38
39
- Regex regex = new Regex ( "<@!?[0-9]+>" ) ;
40
- Match match = regex . Match ( input ) ;
41
- if ( match . Success ) // contains a mention
39
+ var m = regex . Matches ( input ) ; // find all matches
40
+ var itRegex = m . GetEnumerator ( ) ; // lets iterate matches
41
+ var itUsers = message . MentionedUsers . GetEnumerator ( ) ; // iterate mentions, too
42
+ int difference = 0 ; // will explain later
43
+ while ( itUsers . MoveNext ( ) && itRegex . MoveNext ( ) ) // we'll loop iterators together
42
44
{
43
- string substring = input . Substring ( match . Index , match . Length ) ;
44
-
45
- SocketUser user = message . MentionedUsers . First ( ) ;
46
-
47
-
48
- returnString = input . Replace ( substring , user . Username ) ;
45
+ var match = ( Match ) itRegex . Current ; // C# makes us cast here.. gross
46
+ var user = itUsers . Current ;
47
+ int len = match . Length ;
48
+ int start = match . Index ;
49
+ string removal = input . Substring ( start - difference , len ) ; // seperate what we're trying to replace
50
+
51
+ /**
52
+ * Since we're replacing `input` after every iteration, we have to
53
+ * store the difference in length after our edits. This is because that
54
+ * the Match object is going to use lengths from before the replacments
55
+ * occured. Thus, we add the length and then subtract after the replace
56
+ */
57
+ difference += input . Length ;
58
+ input = ReplaceFirst ( input , removal , user . Username ) ;
59
+ difference -= input . Length ;
49
60
}
50
61
51
- return returnString ;
62
+ return input ;
52
63
}
53
64
54
- public static string Unescape ( string input )
55
- {
56
- return Regex . Replace ( input , @"\\([^A-Za-z0-9])" , "$1" ) ;
57
- }
65
+ public static string Unescape ( string input )
66
+ {
67
+ return Regex . Replace ( input , @"\\([^A-Za-z0-9])" , "$1" ) ;
68
+ }
58
69
59
70
public static string ChannelMentionToName ( string input , SocketUserMessage message )
60
71
{
61
- string returnString = input ;
72
+ Regex regex = new Regex ( "<#([0-9]+)>" ) ; // create patern
62
73
63
- Regex regex = new Regex ( "<#[0-9]+>" ) ;
64
- Match match = regex . Match ( input ) ;
65
- if ( match . Success ) // contains a mention
74
+ var m = regex . Matches ( input ) ; // find all matches
75
+ var itRegex = m . GetEnumerator ( ) ; // lets iterate matches
76
+ var itChan = message . MentionedChannels . GetEnumerator ( ) ; // iterate mentions, too
77
+ int difference = 0 ; // will explain later
78
+ while ( itChan . MoveNext ( ) && itRegex . MoveNext ( ) ) // we'll loop iterators together
66
79
{
67
- string substring = input . Substring ( match . Index , match . Length ) ;
68
-
69
- var chan = message . MentionedChannels . First ( ) ;
80
+ var match = ( Match ) itRegex . Current ; // C# makes us cast here.. gross
81
+ var channel = itChan . Current ;
82
+ int len = match . Length ;
83
+ int start = match . Index ;
84
+ string removal = input . Substring ( start - difference , len ) ; // seperate what we're trying to replace
85
+
86
+ /**
87
+ * Since we're replacing `input` after every iteration, we have to
88
+ * store the difference in length after our edits. This is because that
89
+ * the Match object is going to use lengths from before the replacments
90
+ * occured. Thus, we add the length and then subtract after the replace
91
+ */
92
+ difference += input . Length ;
93
+ input = ReplaceFirst ( input , removal , channel . Name ) ;
94
+ difference -= input . Length ;
95
+ }
70
96
97
+ return input ;
98
+ }
71
99
72
- returnString = input . Replace ( substring , "#" + chan . Name ) ;
100
+ public static string ReplaceFirst ( string text , string search , string replace )
101
+ {
102
+ int pos = text . IndexOf ( search ) ;
103
+ if ( pos < 0 )
104
+ {
105
+ return text ;
73
106
}
74
-
75
- return returnString ;
107
+ return text . Substring ( 0 , pos ) + replace + text . Substring ( pos + search . Length ) ;
76
108
}
77
109
78
110
// Converts <:emoji:23598052306> to :emoji:
0 commit comments