From 1a5edf0ccd204feaeecba9338f539ce401510418 Mon Sep 17 00:00:00 2001 From: DeinFreund Date: Wed, 17 Oct 2018 15:48:56 +0200 Subject: [PATCH] discord relay uses nicknames over usernames, allow zkls users to mention discord users by nick, disallow mentioning roles like @everyone --- ZkLobbyServer/DiscordRelaySource.cs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/ZkLobbyServer/DiscordRelaySource.cs b/ZkLobbyServer/DiscordRelaySource.cs index 05f7ad5d47..efd13da11b 100644 --- a/ZkLobbyServer/DiscordRelaySource.cs +++ b/ZkLobbyServer/DiscordRelaySource.cs @@ -22,6 +22,11 @@ private static string GetName(IUser user) return user.Username + "#" + user.Discriminator; } + private static string ReplaceMention(string message, MatchEvaluator replace) + { + return Regex.Replace(message, "<@!{0,1}([0-9]+)>", replace); + } + public DiscordRelaySource(DiscordSocketClient client, ulong serverID, SaySource source) { discord = client; @@ -50,13 +55,20 @@ public void SetTopic(string channel, string topic) } } - public void SendMessage(ChatRelayMessage m) { try { if (m.Source != source) { + //Translate mentions of nicknames to discord mentions + var userIdsByNickname = discord.GetGuild(serverID).Users.ToDictionary(x => x.Nickname, x => x.Id); + userIdsByNickname.ForEach((pair) => m.Message = m.Message.Replace(pair.Key, string.Format("<@{0}>", pair.Value))); + + //Block any mentions of an entire role + var roleIds = discord.GetGuild(serverID).Roles.Select(x => x.Id.ToString()).ToList(); + m.Message = ReplaceMention(m.Message, match => roleIds.Contains(match.Groups[1].Value) ? "" : match.Groups[1].Value); + if (m.User != GlobalConst.NightwatchName) GetChannel(m.Channel)?.SendMessageAsync($"<{m.User}> {m.Message}"); // don't relay extra "nightwatch" if it is self relay else GetChannel(m.Channel)?.SendMessageAsync(m.Message); @@ -81,18 +93,18 @@ public void SendPm(string user, string message) } - private static string TranslateMentions(SocketMessage msg) + private string TranslateMentions(SocketMessage msg) { var text = msg.Content; if (string.IsNullOrEmpty(text)) return string.Empty; - return Regex.Replace(text, "<@([0-9]+)>", + return ReplaceMention(text, m => { var mentionedId = m.Groups[1].Value; var user = msg.MentionedUsers.FirstOrDefault(x => x.Id.ToString() == mentionedId); - if (user != null) return user.Username; + if (user != null) return discord.GetGuild(serverID).Users.FirstOrDefault(x => x.Id == user.Id)?.Nickname ?? user.Username; var channel = msg.MentionedChannels.FirstOrDefault(x => x.Id.ToString() == mentionedId); if (channel != null) return channel.Name;