Skip to content
This repository was archived by the owner on Sep 29, 2020. It is now read-only.

Commit e122780

Browse files
committed
Finally replace all user/channel mentions
1 parent b243e31 commit e122780

File tree

1 file changed

+57
-25
lines changed

1 file changed

+57
-25
lines changed

IRC-Relay/Helpers.cs

+57-25
Original file line numberDiff line numberDiff line change
@@ -34,45 +34,77 @@ public static string UploadMarkDown(string input)
3434

3535
public static string MentionToUsername(string input, SocketUserMessage message)
3636
{
37-
string returnString = message.Content;
37+
Regex regex = new Regex("<@!?([0-9]+)>"); // create patern
3838

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
4244
{
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;
4960
}
5061

51-
return returnString;
62+
return input;
5263
}
5364

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+
}
5869

5970
public static string ChannelMentionToName(string input, SocketUserMessage message)
6071
{
61-
string returnString = input;
72+
Regex regex = new Regex("<#([0-9]+)>"); // create patern
6273

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
6679
{
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+
}
7096

97+
return input;
98+
}
7199

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;
73106
}
74-
75-
return returnString;
107+
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
76108
}
77109

78110
// Converts <:emoji:23598052306> to :emoji:

0 commit comments

Comments
 (0)