Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #125 from ldilley/master
Browse files Browse the repository at this point in the history
Regex fix, NuGet stuff, and item ID conversion
  • Loading branch information
ldilley authored Feb 9, 2021
2 parents 55f7816 + 99df63f commit e8d06bf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Terracord/Terracord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private void OnBroadcast(ServerBroadcastEventArgs args)
if(Util.FilterBroadcast($"{args.Message}"))
return;

Util.Log($"Server broadcast: {args.Message}", Util.Severity.Info);
Util.Log($"Server broadcast: {Util.ConvertItems(args.Message.ToString())}", Util.Severity.Info);
discord.Send(Config.BroadcastText.Replace("%m%", args.Message.ToString()));
}

Expand Down Expand Up @@ -186,6 +186,10 @@ private void OnChat(PlayerChatEventArgs args)
if(Regex.IsMatch(modifiedMessage, @"@.+"))
modifiedMessage = Util.ConvertRoleUserMentions(modifiedMessage, discord.Client);

// Check for game items and convert them to friendly names if found
if(Regex.IsMatch(modifiedMessage, @"\[i(/[p|s][0-9]+)?:([0-9]+)\]"))
modifiedMessage = Util.ConvertItems(modifiedMessage);

if(Config.LogChat)
{
//Util.Log($"{TShock.Players[args.Who].Name} said: {modifiedMessage}", Util.Severity.Info);
Expand Down
1 change: 1 addition & 0 deletions Terracord/Terracord.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<NeutralLanguage>en-US</NeutralLanguage>
<ApplicationIcon />
<Win32Resource />
<!--<NuspecFile>Terracord.nuspec</NuspecFile>-->
<CodeAnalysisRuleSet>Terracord.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

Expand Down
14 changes: 14 additions & 0 deletions Terracord/Terracord.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>Terracord</id>
<version>1.2.2</version>
<authors>Lloyd Dilley</authors>
<license type="expression">GPL-3.0-or-later</license>
<projectUrl>https://github.com/FragLand/terracord</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A Discord &lt;-&gt; Terraria bridge plugin for TShock</description>
<copyright>Copyright 2019-2021</copyright>
<tags>bot bridge chat Discord plugin relay Terraria TShock</tags>
</metadata>
</package>
22 changes: 20 additions & 2 deletions Terracord/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using TShockAPI;

namespace FragLand.TerracordPlugin
{
Expand Down Expand Up @@ -237,6 +238,22 @@ public static string ConvertRoleUserMentions(string message, DiscordSocketClient
return modifiedMessage;
}

/// <summary>
/// Attempts to convert Terraria item IDs to friendly names
/// </summary>
/// <param name="message">message to modify</param>
/// <returns>modified message</returns>
public static string ConvertItems(string message)
{
string modifiedMessage = message;
string itemPattern = @"\[i(/[p|s][0-9]+)?:([0-9]+)\]"; // matches [i:219] or [i/s4:75] (where "/s4" is an optional amount of item)

if(Regex.IsMatch(modifiedMessage, itemPattern))
modifiedMessage = Regex.Replace(modifiedMessage, itemPattern, m => $"**[{TShock.Utils.GetItemById(int.Parse(m.Groups[2].Value)).Name}]**", RegexOptions.IgnoreCase);

return modifiedMessage;
}

/// <summary>
/// Filter broadcast message based on contents (TShock 4.4 broadcast fix)
/// </summary>
Expand All @@ -245,9 +262,10 @@ public static string ConvertRoleUserMentions(string message, DiscordSocketClient
public static bool FilterBroadcast(string message)
{
string discordMessage = Config.ChatText.Replace("%u%", ".+");
if(Regex.IsMatch(message, $"^{discordMessage} .*$")) // Discord message
discordMessage = discordMessage.Replace("%m%", ".*");
if(Regex.IsMatch(message, $"^{discordMessage}$")) // Discord message
return true;
if(Regex.IsMatch(message, "^.+: .*$")) // Terraria chat message
if(Regex.IsMatch(message, "^.+: .*$")) // Terraria chat message
return true;
if(Config.SilenceSaves)
{
Expand Down

0 comments on commit e8d06bf

Please sign in to comment.