-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding filters to chat events, custom messages for join/leave/death, …
…hopefully fixing leave/join bug
- Loading branch information
1 parent
40ad730
commit 314f3ad
Showing
12 changed files
with
274 additions
and
179 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string> WebhookUrl; | ||
public readonly ConfigEntry<bool> Enabled; | ||
public readonly ConfigEntry<bool> FetchAndShowIp; | ||
public readonly ConfigEntry<string> RawIgnoredUsernames; | ||
public readonly ConfigEntry<string> IgnoredChatMessageRegex; | ||
|
||
public readonly Dictionary<ValheimEvent, ConfigEntry<bool>> Events = new Dictionary<ValheimEvent, ConfigEntry<bool>>(); | ||
public readonly Dictionary<ValheimEvent, ConfigEntry<string>> EventMessages = new Dictionary<ValheimEvent, ConfigEntry<string>>(); | ||
private string[] _ignoredUsernames; | ||
|
||
public string[] IgnoredUsernames => _ignoredUsernames ??= JsonMapper.ToObject<string[]>(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}}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,49 @@ | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using HarmonyLib; | ||
using UnityEngine; | ||
|
||
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; | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.