using System.Collections.Generic; using System.IO; using Oxide.Core; using Oxide.Core.Libraries.Covalence; using Oxide.Core.Plugins; using System.Collections; using Newtonsoft.Json; using System; using UnityEngine.Networking; namespace Oxide.Plugins { [Info("Feedback", "Ridamees/Razor/HA1FAL1VE", "1.0.1")] [Description("Allows players to send feedback with /feedback, which is saved to a log file.")] class Feedback : CovalencePlugin { private const string PERMISSION_USE_FEEDBACK = "feedback.use"; private PluginConfig config; class PluginConfig { public string LogFileName { get; set; } public string WebHookUrl { get; set; } public string AvatarURL { get; set; } } protected override void LoadDefaultConfig() { Config.WriteObject(new PluginConfig { LogFileName = "feedback", WebHookUrl = "", AvatarURL = "http://images.myvector.xyz/discordimage.png" }, true); } private void Init() { permission.RegisterPermission(PERMISSION_USE_FEEDBACK, this); config = Config.ReadObject(); } [Command("feedback")] private void FeedbackCommand(IPlayer player, string command, string[] args) { if (!permission.UserHasPermission(player.Id, PERMISSION_USE_FEEDBACK)) { player.Reply("You do not have permission to use this command."); return; } if (args.Length < 1) { player.Reply("You must include a message in your feedback."); return; } var message = string.Join(" ", args); var logMessage = $"[{player.Id}] [{player.Name}] {message}"; LogToFile(config.LogFileName, logMessage, this); player.Reply("Thank you for your feedback!"); ServerMgr.Instance.StartCoroutine(SendToDiscord(ConVar.Server.hostname, player.Name, message)); } #region Discord Class public class Message { public string username { get; set; } public string avatar_url { get; set; } public List embeds { get; set; } public class Fields { public string name { get; set; } public string value { get; set; } public bool inline { get; set; } public Fields(string name, string value, bool inline) { this.name = name; this.value = value; this.inline = inline; } } public class Footer { public string text { get; set; } public Footer(string text) { this.text = text; } } public class Embeds { public string title { get; set; } public string description { get; set; } public List fields { get; set; } public Footer footer { get; set; } public Embeds(string title, string description, List fields, Footer footer) { this.title = title; this.description = description; this.fields = fields; this.footer = footer; } } public Message(string username, string avatar_url, List embeds) { this.username = username; this.avatar_url = avatar_url; this.embeds = embeds; } } private Message DiscordMessage(string servername, string displayName, string theMessage) { if (config.AvatarURL == null) config.AvatarURL = ""; var fields = new List() { new Message.Fields("Player", displayName, false), new Message.Fields("Message ", theMessage, false), }; var footer = new Message.Footer($"Logged @{DateTime.UtcNow:dd/MM/yy HH:mm:ss}"); var embeds = new List() { new Message.Embeds(servername, "New Feedback", fields, footer) }; Message msg = new Message("Feedback", config.AvatarURL, embeds); return msg; } private IEnumerator SendToDiscord(string servername, string displayName, string theMessage) { if (!string.IsNullOrEmpty(config.WebHookUrl)) { if (displayName == null) displayName = "UnKnown player"; var msg = DiscordMessage(ConVar.Server.hostname, displayName, theMessage); string jsonmsg = JsonConvert.SerializeObject(msg); UnityWebRequest wwwpost = new UnityWebRequest(config.WebHookUrl, "POST"); byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(jsonmsg.ToString()); wwwpost.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend); wwwpost.SetRequestHeader("Content-Type", "application/json"); yield return wwwpost.SendWebRequest(); if (wwwpost.isNetworkError || wwwpost.isHttpError) { PrintError(wwwpost.error); } wwwpost.Dispose(); } } #endregion } }