From 314f3adf4485ae1cde93687ecf6f71c5692531d6 Mon Sep 17 00:00:00 2001 From: Aaron Scherer Date: Mon, 1 Mar 2021 18:56:28 -0600 Subject: [PATCH] Adding filters to chat events, custom messages for join/leave/death, hopefully fixing leave/join bug --- .github/workflows/compile.yml | 13 ---- Configuration.cs | 57 +++++++++++++++++ DiscordNotifier.cs | 83 +++++-------------------- DiscordNotifier.csproj | 30 +++++++-- Patches/ChatPatch.cs | 43 +++++++++---- Patches/PlayerPatch.cs | 39 ++++++++++++ Patches/ZNetPatch.cs | 113 +++++++++++++++++++--------------- README.md | 13 ++++ Utils.cs | 16 ++--- ValheimEventHandler.cs | 39 ++++++------ manifest.json | 2 +- packages.config | 5 ++ 12 files changed, 274 insertions(+), 179 deletions(-) delete mode 100644 .github/workflows/compile.yml create mode 100644 Configuration.cs create mode 100644 Patches/PlayerPatch.cs create mode 100644 packages.config diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml deleted file mode 100644 index d49b22f..0000000 --- a/.github/workflows/compile.yml +++ /dev/null @@ -1,13 +0,0 @@ -on: - - push - -name: compile - -jobs: - build: - name: Build project - runs-on: windows-latest - steps: - - uses: actions/checkout@main - - uses: actions/setup-dotnet@v1 - - run: dotnet build DiscordNotifier.csproj \ No newline at end of file diff --git a/Configuration.cs b/Configuration.cs new file mode 100644 index 0000000..bdd5737 --- /dev/null +++ b/Configuration.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Runtime.Remoting.Messaging; +using BepInEx; +using BepInEx.Configuration; +using BepInEx.Logging; +using DiscordNotifier.Patches; +using HarmonyLib; +using LitJson; +using UnityEngine; + +namespace DiscordNotifier +{ + public class Configuration + { + public readonly ConfigEntry WebhookUrl; + public readonly ConfigEntry Enabled; + public readonly ConfigEntry FetchAndShowIp; + public readonly ConfigEntry RawIgnoredUsernames; + public readonly ConfigEntry IgnoredChatMessageRegex; + + public readonly Dictionary> Events = new Dictionary>(); + public readonly Dictionary> EventMessages = new Dictionary>(); + private string[] _ignoredUsernames; + + public string[] IgnoredUsernames => _ignoredUsernames ??= JsonMapper.ToObject(RawIgnoredUsernames.Value); + + internal Configuration(ConfigFile config) + { + Enabled = config.Bind("General", "Enabled", true, "Is the plugin enabled?"); + FetchAndShowIp = config.Bind("General", "FetchAndShowIp", false, "Should the plugin attempt to get the server IP and post to the webhook"); + WebhookUrl = config.Bind("General", "WebhookUrl", "", "Enter the Webhook URL from discord here."); + RawIgnoredUsernames = config.Bind("Chat", "IgnoredUsernames", "[]", "Array of ignored usernames. Use the format: [\"Coralle\", \"Steve\"]"); + IgnoredChatMessageRegex = config.Bind("Chat", "IgnoredChatMessageRegex", "", "Specify a regex used to ignore chat messages.\nSyntax: /(^START)|(#END$)/\nThis would ignore text from the MapSync mod"); + + foreach (var eventName in Enum.GetNames(typeof(ValheimEvent))) + { + if (!Enum.TryParse(eventName, false, out ValheimEvent evt)) + { + Main.StaticLogger.LogError("Bad event name, somehow: " + eventName); + continue; + } + + Events[evt] = config.Bind("Events", eventName, true); + } + + EventMessages[ValheimEvent.OnPlayerDeath] = config.Bind("Events", "OnPlayerDeathMessage", "{{username}} has died!", + "Message to send when a player dies.\nAvailable variables: {{username}}, {{userId}}"); + EventMessages[ValheimEvent.OnPlayerDisconnected] = config.Bind("Events", "OnPlayerDisconnectedMessage", "{{username}} has disconnected!", + "Message to send when a player disconnects from the server.\nAvailable variables: {{username}}, {{userId}}"); + EventMessages[ValheimEvent.OnPlayerJoined] = config.Bind("Events", "OnPlayerJoinedMessage", "{{username}} has joined!", + "Message to send when a player joins the server.\nAvailable variables: {{username}}, {{userId}}"); + } + } +} diff --git a/DiscordNotifier.cs b/DiscordNotifier.cs index 56ef06e..908c3c6 100644 --- a/DiscordNotifier.cs +++ b/DiscordNotifier.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Reflection; +using System.Reflection; using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; @@ -9,89 +7,36 @@ namespace DiscordNotifier { [BepInPlugin(GUID, MODNAME, VERSION)] - [HarmonyPatch] public class Main : BaseUnityPlugin { public const string MODNAME = "Discord Notifier"; public const string AUTHOR = "CryptikLemur"; public const string GUID = "CryptikLemur_DiscordNotifier"; - public const string VERSION = "0.0.1.0"; + public const string VERSION = "0.0.3.5"; - internal readonly Harmony harmony; - internal readonly Assembly assembly; - - public static ConfigEntry configWebhookUrl; - public static ConfigEntry configEnabled; - public static ConfigEntry configFetchAndShowIp; - public static ConfigEntry configTrackAllUsers; - public static Dictionary> configEvents = new Dictionary>(); + internal Harmony harmony; + internal Assembly assembly; internal static ManualLogSource StaticLogger; - internal static ZNet zNet; - internal static ZDOMan zdoMan; - internal static Game game; - public Main() + public static Configuration Configuration; + + private void Awake() { - harmony = new Harmony(GUID); - assembly = Assembly.GetExecutingAssembly(); StaticLogger = Logger; + Configuration = new Configuration(Config); - configEnabled = Config.Bind("General", "Enabled", true, "Is the plugin enabled?"); - configFetchAndShowIp = Config.Bind("General", "FetchAndShowIp", false, "Should the plugin attempt to get the server IP and post to the webhook"); - configWebhookUrl = Config.Bind("General", "WebhookUrl", "", "Enter the Webhook URL from discord here."); - configTrackAllUsers = Config.Bind("General", "TrackAllUsers", false, "Should the plugin track all the users on the server, or just you. If running as the server, this is ignored."); + if (!Configuration.Enabled.Value || Configuration.WebhookUrl.Value.Length <= 0) return; - foreach (var eventName in Enum.GetNames(typeof(ValheimEvent))) - { - if (!Enum.TryParse(eventName, false, out ValheimEvent evt)) - { - Logger.LogError("Bad event name, somehow: " + eventName); - continue; - } - - configEvents[evt] = Config.Bind("Events", eventName, true); - } - } + harmony = new Harmony(GUID); + assembly = Assembly.GetExecutingAssembly(); - private void Awake() - { - if (configEnabled.Value && configWebhookUrl.Value.Length > 0) - { - harmony.PatchAll(assembly); - Logger.LogMessage($"{AUTHOR}'s {MODNAME} (v{VERSION}) has started"); - } + harmony.PatchAll(assembly); + Logger.LogMessage($"{AUTHOR}'s {MODNAME} (v{VERSION}) has started"); } private void OnDestroy() { - if (configEnabled.Value) harmony.UnpatchSelf(); - } - - [HarmonyPatch(typeof(ZNet), "Awake")] - private class ZNetPatch - { - static void Prefix(ref ZNet __instance) - { - zNet = __instance; - } - } - - [HarmonyPatch(typeof(ZDOMan), "ResetSectorArray")] - private class ZDOManPatch - { - static void Postfix(ref ZDOMan __instance) - { - zdoMan = __instance; - } - } - - [HarmonyPatch(typeof(Game), "Awake")] - private class GamePatch - { - static void Prefix(ref Game __instance) - { - game = __instance; - } + if (Configuration.Enabled.Value) harmony.UnpatchSelf(); } } } diff --git a/DiscordNotifier.csproj b/DiscordNotifier.csproj index 696215f..cfd5fe2 100644 --- a/DiscordNotifier.csproj +++ b/DiscordNotifier.csproj @@ -33,6 +33,9 @@ 8.0 true + + Always + Libs\0Harmony.dll @@ -49,6 +52,9 @@ ..\..\..\AppData\Roaming\r2modmanPlus-local\Valheim\profiles\Modded2\BepInEx\core\BepInEx.Harmony.dll + + packages\LitJson.0.17.0\lib\net45\LitJSON.dll + @@ -74,9 +80,10 @@ + - + @@ -86,13 +93,28 @@ Always + + + + + + - - + rmdir /s /q "$(TargetDir)\DiscordNotifier" - copy /Y "$(TargetDir)$(ProjectName).dll" "C:\Users\aequa\AppData\Roaming\r2modmanPlus-local\Valheim\profiles\Modded3\BepInEx\plugins\$(ProjectName).dll" + copy /Y "$(TargetDir)$(ProjectName).dll" "C:\Users\aequa\AppData\Roaming\r2modmanPlus-local\Valheim\profiles\Modded3\BepInEx\plugins\" +copy /Y "$(TargetDir)LitJSON.dll" "C:\Users\aequa\AppData\Roaming\r2modmanPlus-local\Valheim\profiles\Modded3\BepInEx\plugins\" + +mkdir "$(TargetDir)\DiscordNotifier" +mkdir "$(TargetDir)\DiscordNotifier\DiscordNotifier" +copy /Y "$(ProjectDir)\README.md" "$(TargetDir)\DiscordNotifier\" +copy /Y "$(ProjectDir)\icon.png" "$(TargetDir)\DiscordNotifier\" +copy /Y "$(ProjectDir)\manifest.json" "$(TargetDir)\DiscordNotifier\" +copy /Y "$(TargetDir)$(ProjectName).dll" "$(TargetDir)\DiscordNotifier\DiscordNotifier\" +copy /Y "$(TargetDir)LitJSON.dll" "$(TargetDir)\DiscordNotifier\DiscordNotifier\" + \ No newline at end of file diff --git a/Patches/ChatPatch.cs b/Patches/ChatPatch.cs index f26fbbd..fb9c5c4 100644 --- a/Patches/ChatPatch.cs +++ b/Patches/ChatPatch.cs @@ -1,5 +1,6 @@ -using System.Collections.Generic; +using System; using System.Linq; +using System.Text.RegularExpressions; using HarmonyLib; using UnityEngine; @@ -7,26 +8,42 @@ namespace DiscordNotifier.Patches { internal class ChatPatch { + public static bool IsChatMessageIgnored(string user, string text) + { + // Ignoring text from MapSyncMod + if (text.StartsWith("START_") && text.EndsWith("#END")) return true; + + // Ignoring "I HAVE ARRIVED!" yell, if we are already alerting when someone joins + if (Main.Configuration.Events[ValheimEvent.OnPlayerJoined].Value && text.ToLower() == "i have arrived!") return true; + + // Ignoring messages from Configuration.ignoredUsernames + if (Main.Configuration.IgnoredUsernames.ToList().Contains(user)) return true; + + var regexPattern = Main.Configuration.IgnoredChatMessageRegex.Value; + if (regexPattern.Length == 0 || !regexPattern.StartsWith("/")) return false; + var pattern = regexPattern.Substring(1, regexPattern.LastIndexOf("/", StringComparison.Ordinal) - 1); + var flagChars = regexPattern.Substring(regexPattern.LastIndexOf("/", StringComparison.Ordinal) + 1).ToLower().ToCharArray().ToList(); + + RegexOptions opts = RegexOptions.None; + if (flagChars.Contains('i')) opts |= RegexOptions.IgnoreCase; + if (flagChars.Contains('m')) opts |= RegexOptions.Multiline; + if (!flagChars.Contains('g')) opts |= RegexOptions.Singleline; + + return Regex.IsMatch(text, pattern, opts); + } + [HarmonyPatch(typeof(Chat), "OnNewChatMessage")] internal class OnNewChatMessage { - private static bool Prefix(ref long senderID, ref string user, ref string text, ref Talker.Type type, ref Vector3 pos) + private static bool Prefix(ref string user, ref string text, ref Talker.Type type, ref Vector3 pos) { - Main.StaticLogger.LogInfo($"Message from {user} ({senderID}): {text}"); - - // Ignoring text from MapSyncMod - if (text.StartsWith("START_") && text.EndsWith("#END")) return true; + if (!ZNet.instance.IsServer()|| IsChatMessageIgnored(user, text)) return true; - // Ignoring "I HAVE ARRIVED!" yell, if we are already alerting when someone joins - if (Main.configEvents[ValheimEvent.OnPlayerJoined].Value && text.ToLower() == "i have arrived!") return true; - - if (Main.zNet.IsServer() || Main.configTrackAllUsers.Value) - { - ValheimEventHandler.OnPlayerMessage(type, user, text, pos); - } + ValheimEventHandler.OnPlayerMessage(type, user, text, pos); return true; } + } } } \ No newline at end of file diff --git a/Patches/PlayerPatch.cs b/Patches/PlayerPatch.cs new file mode 100644 index 0000000..8875ab9 --- /dev/null +++ b/Patches/PlayerPatch.cs @@ -0,0 +1,39 @@ +using HarmonyLib; + +namespace DiscordNotifier.Patches +{ + internal class PlayerPatch + { + private static bool hasSpawned = false; + + private static ZNet.PlayerInfo getBasicPlayerInfoFromPlayer(Player player) => new ZNet.PlayerInfo + { + m_characterID = player.GetZDOID(), + m_name = player.GetPlayerName(), + m_position = player.transform.position + }; + + [HarmonyPatch(typeof(Player), "OnSpawned")] + internal class OnSpawned + { + private static void Prefix(ref Player __instance) + { + if (hasSpawned || !ZNet.instance.IsServer() || ZNet.instance.IsDedicated()) return; + hasSpawned = true; + + ValheimEventHandler.OnPlayerJoined(getBasicPlayerInfoFromPlayer(__instance)); + } + } + + [HarmonyPatch(typeof(Player), "OnDeath")] + internal class OnDeath + { + private static void Postfix(ref Player __instance) + { + if (!ZNet.instance.IsServer() || ZNet.instance.IsDedicated()) return; + + ValheimEventHandler.OnPlayerDeath(getBasicPlayerInfoFromPlayer(__instance)); + } + } + } +} \ No newline at end of file diff --git a/Patches/ZNetPatch.cs b/Patches/ZNetPatch.cs index 7f13ce9..907caa2 100644 --- a/Patches/ZNetPatch.cs +++ b/Patches/ZNetPatch.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using HarmonyLib; @@ -6,37 +7,55 @@ namespace DiscordNotifier.Patches { internal class ZNetPatch { - internal static Dictionary players = new Dictionary(); + private static readonly List ConnectedPlayers = new List(); - [HarmonyPatch(typeof(ZNet), "SendPlayerList")] - internal class SendPlayerList + public static ZNet.PlayerInfo GetPlayerInfoFromPeer(ZNetPeer peer) + { + ZNet.PlayerInfo player = new ZNet.PlayerInfo(); + player.m_characterID = peer.m_characterID; + player.m_name = peer.m_playerName; + player.m_host = peer.m_socket.GetHostName(); + player.m_publicPosition = peer.m_publicRefPos; + if (player.m_publicPosition) + player.m_position = peer.m_refPos; + + return player; + } + + [HarmonyPatch(typeof(ZNet), "Awake")] + internal class Awake { private static void Postfix(ref ZNet __instance) { - var onServer = new List(); - foreach (var peer in __instance.m_peers) - { - ZNet.PlayerInfo player = new ZNet.PlayerInfo(); - player.m_characterID = peer.m_characterID; - player.m_name = peer.m_playerName; - player.m_host = peer.m_socket.GetHostName(); - player.m_publicPosition = peer.m_publicRefPos; - if (player.m_publicPosition) - player.m_position = peer.m_refPos; - - // If the player is new, trigger an OnPlayerJoined event - if (!players.ContainsKey(player.m_characterID.userID)) ValheimEventHandler.OnPlayerJoined(player); - onServer.Add(player.m_characterID.userID); - players[player.m_characterID.userID] = player; - } - // If the player is no longer on the server, remove them from the list, and trigger an OnPlayerDisconnected event - var toRemove = players.Values.Where(player => !onServer.Contains(player.m_characterID.userID)).ToList(); - toRemove.ForEach(player => - { - ValheimEventHandler.OnPlayerDisconnected(player); - players.Remove(player.m_characterID.userID); - }); + ChatPatch.IsChatMessageIgnored("", "Testing message"); + if (!__instance.IsServer()) Main.StaticLogger.LogError("This mod does not work on the client!"); + } + } + + [HarmonyPatch(typeof(ZNet), "RPC_CharacterID")] + internal class RPC_CharacterID + { + private static void Postfix(ref ZNet __instance, ref ZRpc rpc) + { + ZNetPeer peer = __instance.GetPeer(rpc); + if (peer.m_characterID.IsNone() || !ZNet.instance.IsServer() || !__instance.IsConnected(peer.m_uid) || ConnectedPlayers.Contains(peer.m_characterID.userID)) return; + ConnectedPlayers.Add(peer.m_characterID.userID); + + ValheimEventHandler.OnPlayerJoined(GetPlayerInfoFromPeer(peer)); + } + } + + [HarmonyPatch(typeof(ZNet), "RPC_Disconnect")] + internal class RPC_Disconnect + { + private static void Prefix(ref ZNet __instance, ref ZRpc rpc) + { + ZNetPeer peer = __instance.GetPeer(rpc); + if (peer.m_characterID.IsNone() || !ZNet.instance.IsServer() || !__instance.IsConnected(peer.m_uid)) return; + ConnectedPlayers.Remove(peer.m_uid); + + ValheimEventHandler.OnPlayerDisconnected(GetPlayerInfoFromPeer(peer)); } } @@ -45,7 +64,9 @@ internal class LoadWorld { private static void Postfix(ref ZNet __instance) { - ValheimEventHandler.OnServerStarted(Main.configFetchAndShowIp.Value ? Utils.FetchIPAddress() : null); + if (!__instance.IsServer()) return; + + ValheimEventHandler.OnServerStarted(Main.Configuration.FetchAndShowIp.Value ? Utils.FetchIPAddress() : null); } } @@ -54,31 +75,30 @@ internal class Shutdown { private static void Prefix(ref ZNet __instance) { - if (__instance.IsServer()) - { - ValheimEventHandler.OnServerStopped(); - } + if (!__instance.IsServer()) return; + + ValheimEventHandler.OnServerStopped(); } } [HarmonyPatch(typeof(ZNet), "SendPeriodicData")] internal class SendPeriodicData { - private static List deadPlayers = new List(); + private static readonly List DeadPlayers = new List(); - private static ZNet.PlayerInfo getPlayerInfo(ZNet __instance, ZDOID zdoId) + private static ZNet.PlayerInfo GetPlayerInfo(ZNet __instance, ZDOID zdoId) { return __instance.GetPlayerList().Find(player => player.m_characterID.userID == zdoId.userID); } - private static void process(ZNet __instance, ZDOID zdoID) + private static void Process(ZNet __instance, ZDOID zdoID) { if (zdoID.IsNone()) return; - ZDO zdo = Main.zdoMan.GetZDO(zdoID); + var zdo = ZDOMan.instance.GetZDO(zdoID); if (zdo == null) return; - bool dead = zdo.GetBool("dead", false); + var dead = zdo.GetBool("dead", false); // If dead, and not in deadPlayers, add to deadPlayers and create event // If dead, and in deadPlayers, do nothing @@ -86,27 +106,18 @@ private static void process(ZNet __instance, ZDOID zdoID) // If not dead, and not in deadPlayers, do nothing if (dead) { - if (deadPlayers.Contains(zdoID.userID)) return; - deadPlayers.Add(zdoID.userID); - ValheimEventHandler.OnPlayerDeath(getPlayerInfo(__instance, zdoID)); + if (DeadPlayers.Contains(zdoID.userID)) return; + DeadPlayers.Add(zdoID.userID); + ValheimEventHandler.OnPlayerDeath(GetPlayerInfo(__instance, zdoID)); } - else if (deadPlayers.Contains(zdoID.userID)) deadPlayers.Remove(zdoID.userID); + else if (DeadPlayers.Contains(zdoID.userID)) DeadPlayers.Remove(zdoID.userID); } private static void Prefix(ref ZNet __instance) { if (!__instance.IsServer()) return; - if (Player.m_localPlayer != null) - { - process(__instance, Player.m_localPlayer.GetZDOID()); - } - - foreach (var cur in __instance.GetPeers()) - { - if (!cur.IsReady()) continue; - process(__instance, cur.m_characterID); - } + foreach (var cur in __instance.m_peers.Where(cur => cur.IsReady())) Process(__instance, cur.m_characterID); } } } diff --git a/README.md b/README.md index 9e4834e..12703c7 100644 --- a/README.md +++ b/README.md @@ -26,5 +26,18 @@ Under `General` are the basic config settings. Make sure you set the "WebhookUrl ## Changelog +#### 0.0.3 + +- Fixing death leave/join bug again, maybe? +- Adding filters to chat events (By Username, by Regex) +- Adding the ability to customize join, leave, and death messages + +#### 0.0.2 + +- Fixing death leave/join bug +- Removing respawn event +- Adding chat event +- Added the ability to show the IP on server startup message + #### 0.0.1 - Initial release, unstable until tested more diff --git a/Utils.cs b/Utils.cs index 1d21acb..43b243c 100644 --- a/Utils.cs +++ b/Utils.cs @@ -1,6 +1,7 @@ -using System.CodeDom; +using System.Collections.Generic; using System.IO; using System.Net; +using LitJson; namespace DiscordNotifier { @@ -9,21 +10,16 @@ public class Utils public static void PostMessage(string message, string username = null) { Main.StaticLogger.LogMessage($"Posting message to webhook: {message}"); - var httpWebRequest = (HttpWebRequest)WebRequest.Create(Main.configWebhookUrl.Value); + var httpWebRequest = (HttpWebRequest) WebRequest.Create(Main.Configuration.WebhookUrl.Value); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { - var json = $"{{\"content\":\"{message}\""; - if (username != null) - { - json += $", \"username\": \"{username}\""; - } + var body = new Dictionary {{"content", message}}; + if (username != null) body.Add("username", username); - json += "}"; - - streamWriter.Write(json); + streamWriter.Write(JsonMapper.ToJson(body)); } httpWebRequest.GetResponseAsync(); diff --git a/ValheimEventHandler.cs b/ValheimEventHandler.cs index 1411a63..1c9e9e8 100644 --- a/ValheimEventHandler.cs +++ b/ValheimEventHandler.cs @@ -16,12 +16,9 @@ public enum ValheimEvent public class ValheimEventHandler { - private static bool IsTrackingAll() => Main.zNet.IsServer() || Main.configTrackAllUsers.Value; - private static bool IsTrackingUserId(ZDOID zdoId) => !Main.zNet.IsServer() && !Main.configTrackAllUsers.Value && Player.m_localPlayer.GetZDOID().userID == zdoId.userID; - public static void OnServerStarted(string ipAddress = null) { - if (!Main.configEvents[ValheimEvent.OnServerStarted].Value) return; + if (!Main.Configuration.Events[ValheimEvent.OnServerStarted].Value) return; if (ipAddress == null) { @@ -36,7 +33,7 @@ public static void OnServerStarted(string ipAddress = null) public static void OnServerStopped() { - if (!Main.configEvents[ValheimEvent.OnServerStopped].Value) return; + if (!Main.Configuration.Events[ValheimEvent.OnServerStopped].Value) return; Utils.PostMessage("Server has stopped"); } @@ -44,33 +41,39 @@ public static void OnServerStopped() public static void OnPlayerJoined(ZNet.PlayerInfo playerInfo) { - if (!Main.configEvents[ValheimEvent.OnPlayerJoined].Value) return; - if (playerInfo.m_characterID.IsNone() || !IsTrackingAll() && !IsTrackingUserId(playerInfo.m_characterID)) return; + if (!Main.Configuration.Events[ValheimEvent.OnPlayerJoined].Value || playerInfo.m_characterID.IsNone()) return; - Main.StaticLogger.LogInfo($"Player joined: {playerInfo.m_name} ({playerInfo.m_characterID})"); - Utils.PostMessage($"{playerInfo.m_name} has joined the server!"); + Utils.PostMessage( + Main.Configuration.EventMessages[ValheimEvent.OnPlayerJoined].Value + .Replace("{{username}}", playerInfo.m_name) + .Replace("{{userId}}", playerInfo.m_characterID.userID.ToString()) + ); } public static void OnPlayerDisconnected(ZNet.PlayerInfo playerInfo) { - if (!Main.configEvents[ValheimEvent.OnPlayerDisconnected].Value) return; - if (playerInfo.m_characterID.IsNone() || !IsTrackingAll() && !IsTrackingUserId(playerInfo.m_characterID)) return; + if (!Main.Configuration.Events[ValheimEvent.OnPlayerDisconnected].Value || playerInfo.m_characterID.IsNone()) return; - Main.StaticLogger.LogInfo($"Player left: {playerInfo.m_name} ({playerInfo.m_characterID})"); - Utils.PostMessage($"{playerInfo.m_name} has left the server!"); + Utils.PostMessage( + Main.Configuration.EventMessages[ValheimEvent.OnPlayerDisconnected].Value + .Replace("{{username}}", playerInfo.m_name) + .Replace("{{userId}}", playerInfo.m_characterID.userID.ToString()) + ); } public static void OnPlayerDeath(ZNet.PlayerInfo playerInfo) { - if (!Main.configEvents[ValheimEvent.OnPlayerDeath].Value) return; - if (playerInfo.m_characterID.IsNone() || !IsTrackingAll() && !IsTrackingUserId(playerInfo.m_characterID)) return; + if (!Main.Configuration.Events[ValheimEvent.OnPlayerDeath].Value || playerInfo.m_characterID.IsNone()) return; - Main.StaticLogger.LogInfo($"Player died: {playerInfo.m_name} ({playerInfo.m_characterID})"); - Utils.PostMessage($"{playerInfo.m_name} has died!"); + Utils.PostMessage( + Main.Configuration.EventMessages[ValheimEvent.OnPlayerDeath].Value + .Replace("{{username}}", playerInfo.m_name) + .Replace("{{userId}}", playerInfo.m_characterID.userID.ToString()) + ); } public static void OnPlayerMessage(Talker.Type type, string user, string message, Vector3 pos) { - if (!Main.configEvents[ValheimEvent.OnPlayerMessage].Value) return; + if (!Main.Configuration.Events[ValheimEvent.OnPlayerMessage].Value) return; switch (type) { diff --git a/manifest.json b/manifest.json index cbe8501..71710e8 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "name": "DiscordNotifier", "description": "Sends messages to Discord using webhooks", - "version_number": "0.0.1", + "version_number": "0.0.3", "website_url": "https://github.com/aequasi/valheim-discord-notifier", "dependencies": [ "denikson-BepInExPack_Valheim-5.4.600" diff --git a/packages.config b/packages.config new file mode 100644 index 0000000..f5381fd --- /dev/null +++ b/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file