using Newtonsoft.Json; using Oxide.Core.Libraries; using Oxide.Plugins; using System; using System.Net; using System.Collections.Generic; using WebSocketSharp; using static FreeImage; namespace Oxide.Plugins { [Info("BanNotice", "Youbin Choi & YaMang -w-", "1.0.0")] internal class BanNotice : RustPlugin { #region Hook void OnRconCommand(IPAddress ip, string command, string[] args) { if (command == "ban" && args != null) { if (args.Length > 2) { var userid = args[0]; var username = args[1]; var reason = args[2]; sendWebhook(userid, username, reason); } } else if (command == "banid" && args != null) { if (args.Length > 2) { var userid = args[0]; var username = args[1]; var reason = args[2]; string length = ""; if (args.Length == 4) // time { length = args[3]; } sendWebhook(userid, username, reason, length); } } } #endregion #region Funtion private long GetCurrentTimeStamp() { DateTime dt = DateTime.Now; return ((DateTimeOffset)dt).ToUnixTimeSeconds(); } private long GetTimeStamp(DateTime date) { return ((DateTimeOffset)date).ToUnixTimeSeconds(); } private long GetBanExpires(string length) { if (length.Contains("s")) return GetTimeStamp(DateTime.Now.AddSeconds(Convert.ToDouble(length.Replace("s", "")))); else if (length.Contains("m")) return GetTimeStamp(DateTime.Now.AddMinutes(Convert.ToDouble(length.Replace("m", "")))); else if (length.Contains("h")) return GetTimeStamp(DateTime.Now.AddHours(Convert.ToDouble(length.Replace("h", "")))); else if (length.Contains("d")) return GetTimeStamp(DateTime.Now.AddDays(Convert.ToDouble(length.Replace("d", "")))); else if (length.Contains("w")) return GetTimeStamp(DateTime.Now.AddDays(Convert.ToDouble(length.Replace("w", "")) * 7)); else if (length.Contains("M")) return GetTimeStamp(DateTime.Now.AddMonths(Convert.ToInt32(length.Replace("M", "")))); else if (length.Contains("Y")) return GetTimeStamp(DateTime.Now.AddYears(Convert.ToInt32(length.Replace("Y", "")))); else return GetTimeStamp(DateTime.Now); } private void sendWebhook(string userid, string username, string reason, string length = null) { string result = string.IsNullOrEmpty(length) ? _config.LangSettings.perms : length; string getExpires = $""; string returnExpires = ""; if (result == _config.LangSettings.perms) returnExpires = result; else returnExpires = getExpires; var message = new { username = "Server Ban Notification", avatar_url = $"{_config.GeneralSettings.Avatar_url}", embeds = new[] { new { author = new { name = $"{ConVar.Server.hostname}", url = $"{ConVar.Server.url}", icon_url = $"{_config.GeneralSettings.Server_icon_url}" }, fields = new[] { new { name = _config.LangSettings.player, value = $"[{username}](https://steamcommunity.com/profiles/${userid})", inline = false }, new { name = _config.LangSettings.reason, value = $"**{reason}**", inline = false }, new { name = _config.LangSettings.Banned, value = $"", inline = true }, new { name = _config.LangSettings.Expires, value = $"{returnExpires}", inline = true } }, color = 14177041, footer = new { text = $"{_config.GeneralSettings.Footer}" } } } }; string json = JsonConvert.SerializeObject(message); webrequest.Enqueue(_config.GeneralSettings.Webhook, json, (code, response) => { if (code != 200) PrintError($"Error sending webhook: {response}"); else Puts("Webhook sent successfully!"); }, this, RequestMethod.POST, new Dictionary { { "Content-Type", "application/json" } }); } #endregion #region Config private ConfigData _config; private class ConfigData { [JsonProperty(PropertyName = "General Settings")] public GeneralSettings GeneralSettings { get; set; } [JsonProperty(PropertyName = "Lang Settings")] public LangSettings LangSettings { get; set; } public Oxide.Core.VersionNumber Version { get; set; } } public class GeneralSettings { public string Webhook { get; set; } public string Avatar_url { get; set; } public string Server_icon_url { get; set; } public string Footer { get; set; } } public class LangSettings { public string perms { get; set; } public string player { get; set; } public string reason { get; set; } public string Banned { get; set; } public string Expires { get; set; } } protected override void LoadConfig() { base.LoadConfig(); _config = Config.ReadObject(); if (_config.Version < Version) UpdateConfigValues(); SaveConfig(); } protected override void LoadDefaultConfig() => _config = GetBaseConfig(); protected override void SaveConfig() => Config.WriteObject(_config, true); private ConfigData GetBaseConfig() { return new ConfigData { GeneralSettings = new GeneralSettings { Webhook = "Please provide the discord webhook URL!", Avatar_url = "Please provide the avatar URL!", Server_icon_url = "Please provide the server icon URL!", Footer = "Please provide the footer text!" }, LangSettings = new LangSettings() { perms = "perms", Banned = "Banned", Expires = "Expires", player = "Player" }, Version = Version }; } private void UpdateConfigValues() { PrintWarning("Config update detected! Updating config values..."); _config.Version = Version; PrintWarning("Config update completed!"); } #endregion } }