Decompiled source of VampireWebhook v0.1.0

VampireWebhook.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using VampireCommandFramework;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("VampireWebhook")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Simple webhook integration")]
[assembly: AssemblyFileVersion("0.1.10.0")]
[assembly: AssemblyInformationalVersion("0.1.10+2e25bb12ca1fff1adf2e640fc93c1d773b40813f")]
[assembly: AssemblyProduct("VampireWebhook")]
[assembly: AssemblyTitle("VampireWebhook")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.10.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace VampireWebhook
{
	public class Commands
	{
		[Command("reloadHook_", "rlh_", null, "Attempts to reload the Discord webhook.", null, true)]
		public async void ReloadDiscordHook(ChatCommandContext ctx)
		{
			ctx.Reply((await DiscordWebhook.LoadHook()) ? "Discord Webhook loaded correctly!" : "Something went wrong loading the Discord Webhook. Check the logs or reset the file.");
		}

		[Command("testhook_", "th_", null, "Sends a test message to the Discord webhook.", null, true)]
		public void TestDiscordHook(ChatCommandContext ctx)
		{
			DiscordWebhook.SendDiscordMessageAsync("The bats whisper in the night, and the wolves howl at the moon. The shadows dance, and the darkness calls. The night is alive with secrets, and the stars are our only witnesses. We are the children of the night, and we embrace the darkness.");
			ctx.Reply("Test message sent to Discord webhook. Check the channel for a message about bat whispering in the night to confirm setup.");
		}
	}
	public static class DiscordWebhook
	{
		private const string DISCORD_HOOK_FILE_PATH = "BepInEx/config/hook.txt";

		private static string _webhookUrl;

		private static int msgCount = 0;

		private static readonly HttpClient _http = new HttpClient();

		internal static async Task<bool> LoadHook()
		{
			bool flag = default(bool);
			if (!File.Exists("BepInEx/config/hook.txt"))
			{
				ManualLogSource logger = Plugin.Logger;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(65, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Discord webhook file not found at '");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("BepInEx/config/hook.txt");
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("'. Webhook could not be setup.");
				}
				logger.LogWarning(val);
				return false;
			}
			string url = File.ReadAllText("BepInEx/config/hook.txt").Trim();
			if (string.IsNullOrWhiteSpace(url))
			{
				ManualLogSource logger2 = Plugin.Logger;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(69, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Discord webhook URL in '");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("BepInEx/config/hook.txt");
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' is empty. Webhook could not be fully setup.");
				}
				logger2.LogWarning(val);
				return false;
			}
			bool ok;
			try
			{
				ok = await PingAsync(url);
			}
			catch (Exception ex)
			{
				ManualLogSource logger3 = Plugin.Logger;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(60, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Exception while pinging Discord webhook: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(". Webhook disabled.");
				}
				logger3.LogWarning(val);
				_webhookUrl = null;
				return false;
			}
			if (!ok)
			{
				ManualLogSource logger4 = Plugin.Logger;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(55, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Discord webhook URL in '");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("BepInEx/config/hook.txt");
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' is invalid. Webhook disabled.");
				}
				logger4.LogWarning(val);
				_webhookUrl = null;
				return false;
			}
			_webhookUrl = url;
			return true;
		}

		public static bool HookEnabled()
		{
			return Settings.UseDiscordWebhook && !string.IsNullOrWhiteSpace(_webhookUrl);
		}

		public static async Task<bool> SendDiscordMessageAsync(string msg, string hookName = "Vardoran whispers", bool forceFreshMessage = true)
		{
			if (!HookEnabled())
			{
				return false;
			}
			if (forceFreshMessage)
			{
				hookName += ((msgCount % 2 == 0) ? "\ud83e\udddb" : "\ud83e\udd87");
			}
			string payload = JsonSerializer.Serialize(new
			{
				username = hookName,
				content = msg
			});
			using StringContent content = new StringContent(payload, Encoding.UTF8, "application/json");
			bool flag = default(bool);
			try
			{
				HttpResponseMessage response = await _http.PostAsync(_webhookUrl, content);
				if (!response.IsSuccessStatusCode)
				{
					ManualLogSource logger = Plugin.Logger;
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(39, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Discord returned ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<HttpStatusCode>(response.StatusCode);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" when sending message.");
					}
					logger.LogWarning(val);
				}
				else
				{
					msgCount++;
				}
			}
			catch (Exception ex2)
			{
				Exception ex = ex2;
				ManualLogSource logger2 = Plugin.Logger;
				BepInExErrorLogInterpolatedStringHandler val2 = new BepInExErrorLogInterpolatedStringHandler(32, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Failed to send Discord message: ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.Message);
				}
				logger2.LogError(val2);
			}
			return false;
		}

		private static async Task<bool> PingAsync(string test_url)
		{
			if (string.IsNullOrWhiteSpace(test_url))
			{
				Plugin.Logger.LogWarning((object)"Webhook URL not loaded. Call Load() first.");
				return false;
			}
			bool flag = default(bool);
			try
			{
				using HttpResponseMessage resp = await _http.GetAsync(test_url);
				if (resp.IsSuccessStatusCode)
				{
					return true;
				}
				ManualLogSource logger = Plugin.Logger;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(31, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Ping failed: Discord returned ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<HttpStatusCode>(resp.StatusCode);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(".");
				}
				logger.LogWarning(val);
				return false;
			}
			catch (Exception ex)
			{
				ManualLogSource logger2 = Plugin.Logger;
				BepInExErrorLogInterpolatedStringHandler val2 = new BepInExErrorLogInterpolatedStringHandler(35, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[Error] Exception pinging webhook: ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.Message);
				}
				logger2.LogError(val2);
				return false;
			}
		}
	}
	[BepInPlugin("phlebotomist.morphine.VampireWebhook", "VampireWebhook", "0.1.10")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BasePlugin
	{
		private Harmony _harmony;

		public static ManualLogSource Logger;

		public override void Load()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Expected O, but got Unknown
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Expected O, but got Unknown
			ManualLogSource log = ((BasePlugin)this).Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(27, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Plugin ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("phlebotomist.morphine.VampireWebhook");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" version ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("0.1.10");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" is loaded!");
			}
			log.LogInfo(val);
			Logger = ((BasePlugin)this).Log;
			_harmony = new Harmony("phlebotomist.morphine.VampireWebhook");
			_harmony.PatchAll(Assembly.GetExecutingAssembly());
			CommandRegistry.RegisterAll();
			Settings.Initialize(((BasePlugin)this).Config);
			DiscordWebhook.LoadHook();
		}

		public override bool Unload()
		{
			CommandRegistry.UnregisterAssembly();
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			return true;
		}
	}
	internal class Settings
	{
		internal static bool UseDiscordWebhook { get; private set; } = true;


		internal static void Initialize(ConfigFile config)
		{
			UseDiscordWebhook = config.Bind<bool>("General", "UseDiscordWebhook", true, "If the webhook should send messages").Value;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "phlebotomist.morphine.VampireWebhook";

		public const string PLUGIN_NAME = "VampireWebhook";

		public const string PLUGIN_VERSION = "0.1.10";
	}
}