@@ -22,6 +22,11 @@ private static string GetName(IUser user)
2222 return user . Username + "#" + user . Discriminator ;
2323 }
2424
25+ private static string ReplaceMention ( string message , MatchEvaluator replace )
26+ {
27+ return Regex . Replace ( message , "<@!{0,1}([0-9]+)>" , replace ) ;
28+ }
29+
2530 public DiscordRelaySource ( DiscordSocketClient client , ulong serverID , SaySource source )
2631 {
2732 discord = client ;
@@ -50,13 +55,20 @@ public void SetTopic(string channel, string topic)
5055 }
5156 }
5257
53-
5458 public void SendMessage ( ChatRelayMessage m )
5559 {
5660 try
5761 {
5862 if ( m . Source != source )
5963 {
64+ //Translate mentions of nicknames to discord mentions
65+ var userIdsByNickname = discord . GetGuild ( serverID ) . Users . ToDictionary ( x => x . Nickname , x => x . Id ) ;
66+ userIdsByNickname . ForEach ( ( pair ) => m . Message = m . Message . Replace ( pair . Key , string . Format ( "<@{0}>" , pair . Value ) ) ) ;
67+
68+ //Block any mentions of an entire role
69+ var roleIds = discord . GetGuild ( serverID ) . Roles . Select ( x => x . Id . ToString ( ) ) . ToList ( ) ;
70+ m . Message = ReplaceMention ( m . Message , match => roleIds . Contains ( match . Groups [ 1 ] . Value ) ? "" : match . Groups [ 1 ] . Value ) ;
71+
6072 if ( m . User != GlobalConst . NightwatchName ) GetChannel ( m . Channel ) ? . SendMessageAsync ( $ "<{ m . User } > { m . Message } ") ;
6173 // don't relay extra "nightwatch" if it is self relay
6274 else GetChannel ( m . Channel ) ? . SendMessageAsync ( m . Message ) ;
@@ -81,18 +93,18 @@ public void SendPm(string user, string message)
8193 }
8294
8395
84- private static string TranslateMentions ( SocketMessage msg )
96+ private string TranslateMentions ( SocketMessage msg )
8597 {
8698 var text = msg . Content ;
8799 if ( string . IsNullOrEmpty ( text ) ) return string . Empty ;
88100
89- return Regex . Replace ( text , "<@([0-9]+)>" ,
101+ return ReplaceMention ( text ,
90102 m =>
91103 {
92104 var mentionedId = m . Groups [ 1 ] . Value ;
93105
94106 var user = msg . MentionedUsers . FirstOrDefault ( x => x . Id . ToString ( ) == mentionedId ) ;
95- if ( user != null ) return user . Username ;
107+ if ( user != null ) return discord . GetGuild ( serverID ) . Users . FirstOrDefault ( x => x . Id == user . Id ) ? . Nickname ?? user . Username ;
96108
97109 var channel = msg . MentionedChannels . FirstOrDefault ( x => x . Id . ToString ( ) == mentionedId ) ;
98110 if ( channel != null ) return channel . Name ;
0 commit comments