Skip to content

Commit

Permalink
Add regex to replace initial message content sent to Discord.
Browse files Browse the repository at this point in the history
  • Loading branch information
PotatoCider committed Dec 18, 2021
1 parent 0d9edae commit 9b538d3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
31 changes: 30 additions & 1 deletion TCR-Discord/ChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,20 @@ public override void GameMessageReceivedHandler(object sender, TerrariaChatEvent
}

outMsg = outMsg.Replace("%worldname%", TerrariaChatRelay.Game.World.GetName());
outMsg = outMsg.Replace("%message%", msg.Message);

if (!Configuration.RegexMessageFormat.IsNullOrEmpty() && IsValidRegex(Configuration.RegexMessageFormat))
{
string newMsg = Regex.Replace(msg.Message, Configuration.RegexMessageFormat, Configuration.RegexMessageReplace);
// Suppress empty %message% being sent if RegexMessageEmptySend is false
if (!Configuration.RegexMessageEmptySend && newMsg.IsNullOrEmpty())
return;
outMsg = outMsg.Replace("%message%", newMsg);
}
else
outMsg = outMsg.Replace("%message%", msg.Message);




if (outMsg == "" || outMsg == null)
return;
Expand Down Expand Up @@ -413,5 +426,21 @@ public override void GameMessageSentHandler(object sender, TerrariaChatEventArgs
{
return LastSequenceNumber;
}

private static bool IsValidRegex(string pattern)
{
if (string.IsNullOrWhiteSpace(pattern)) return false;

try
{
Regex.Match("", pattern);
}
catch (ArgumentException)
{
return false;
}

return true;
}
}
}
13 changes: 13 additions & 0 deletions TCR-Discord/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public class Configuration : SimpleConfig<Configuration>
public string FormatHelp6 { get; set; } = "%groupprefix% = Group prefix";
[JsonProperty(Order = 65)]
public string FormatHelp7 { get; set; } = "%groupsuffix% = Group suffix";
[JsonProperty(Order = 66)]
public string RegexHelp1 { get; set; } = "For more advanced users, you can use RegexMessageFormat & RegexMessageReplace to modify %message%";
[JsonProperty(Order = 67)]
public string RegexHelp2 { get; set; } = "If RegexMessageEmptySend is true, then the discord message is sent even if %message% is empty. This is useful when you want to filter messages send to Discord.";


[JsonProperty(Order = 70)]
public static string PlayerChatFormat = ":speech_left: **%playername%:** %message%";
Expand All @@ -62,6 +67,14 @@ public class Configuration : SimpleConfig<Configuration>
[JsonProperty(Order = 115)]
public static string VanillaBossSpawned = ":anger: **%bossname% has awoken!**";

[JsonProperty(Order = 125)]
public static string RegexMessageFormat = "";
[JsonProperty(Order = 130)]
public static string RegexMessageReplace = "";
[JsonProperty(Order = 135)]
public static bool RegexMessageEmptySend = false;


public Configuration()
{
if (!File.Exists(FileName))
Expand Down

0 comments on commit 9b538d3

Please sign in to comment.