Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve discord relay mentions #2424

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions ZkLobbyServer/DiscordRelaySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,13 +55,25 @@ 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.ToString(), StringComparer.OrdinalIgnoreCase);
m.Message = Regex.Replace(m.Message, "(\\w+)", match => userIdsByNickname.ContainsKey(match.Groups[1].Value) ?
string.Format("<@{0}>", userIdsByNickname[match.Groups[1].Value]) : match.Groups[1].Value);

//Block any mentions of an entire role via ID
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);

//Block any mentions of an entire role via Name
var roleNames = discord.GetGuild(serverID).Roles.Select(x => x.Name).ToList();
roleNames.ForEach(role => m.Message = m.Message.Replace(string.Format("@{0}", role), ""));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

Suggested change
roleNames.ForEach(role => m.Message = m.Message.Replace(string.Format("@{0}", role), ""));
roleNames.ForEach(role => m.Message = m.Message.Replace(string.Format("@{0}", role), role));

For stuff like

is any @admin online?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, but what about @@Admin?

Copy link
Member

@sprunk sprunk Oct 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with string.Format(" {0}", role) or string.Format("@{1}{0}", role, <zero width space>) or somesuch then?


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);
Expand All @@ -81,18 +98,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;
Expand Down