Skip to content

Commit 5ab43b4

Browse files
committed
discord relay uses nicknames over usernames, allow zkls users to mention discord users by nick, disallow mentioning roles like [at]everyone
1 parent b50fb0e commit 5ab43b4

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

ZkLobbyServer/DiscordRelaySource.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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,24 @@ 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 via ID
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+
72+
//Block any mentions of an entire role via Name
73+
var roleNames = discord.GetGuild(serverID).Roles.Select(x => x.Name).ToList();
74+
roleNames.ForEach(role => m.Message = m.Message.Replace(string.Format("@{0}", role), ""));
75+
6076
if (m.User != GlobalConst.NightwatchName) GetChannel(m.Channel)?.SendMessageAsync($"<{m.User}> {m.Message}");
6177
// don't relay extra "nightwatch" if it is self relay
6278
else GetChannel(m.Channel)?.SendMessageAsync(m.Message);
@@ -81,18 +97,18 @@ public void SendPm(string user, string message)
8197
}
8298

8399

84-
private static string TranslateMentions(SocketMessage msg)
100+
private string TranslateMentions(SocketMessage msg)
85101
{
86102
var text = msg.Content;
87103
if (string.IsNullOrEmpty(text)) return string.Empty;
88104

89-
return Regex.Replace(text, "<@([0-9]+)>",
105+
return ReplaceMention(text,
90106
m =>
91107
{
92108
var mentionedId = m.Groups[1].Value;
93109

94110
var user = msg.MentionedUsers.FirstOrDefault(x => x.Id.ToString() == mentionedId);
95-
if (user != null) return user.Username;
111+
if (user != null) return discord.GetGuild(serverID).Users.FirstOrDefault(x => x.Id == user.Id)?.Nickname ?? user.Username;
96112

97113
var channel = msg.MentionedChannels.FirstOrDefault(x => x.Id.ToString() == mentionedId);
98114
if (channel != null) return channel.Name;

0 commit comments

Comments
 (0)