Guest User

Discord chat modified

a guest
Jul 10th, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Newtonsoft.Json;
  4. using Oxide.Core;
  5. using Oxide.Core.Libraries.Covalence;
  6.  
  7. namespace Oxide.Plugins
  8. {
  9. [Info("Admin Chat", "LaserHydra", "2.0.4")]
  10. [Description("Allows admins to write in an admin-only chatroom")]
  11. internal class AdminChat : CovalencePlugin
  12. {
  13. private const string Permission = "adminchat.use";
  14.  
  15. private Configuration _config;
  16. private List<string> _enabledUserIds = new List<string>();
  17. #region Hooks
  18.  
  19. private object OnUserChat(IPlayer player, string message)
  20. {
  21. if (!player.HasPermission(Permission))
  22. return null;
  23.  
  24. if (message.StartsWith(_config.Prefix))
  25. {
  26. SendAdminMessage(player, message.Substring(_config.Prefix.Length), false);
  27. return true;
  28. }
  29.  
  30.  
  31. if (HasAdminChatEnabled(player))
  32. {
  33. SendAdminMessage(player, message, false);
  34. return true;
  35. }
  36.  
  37.  
  38. return null;
  39. }
  40.  
  41. private void OnBetterChat(Dictionary<string, object> data)
  42. {
  43. var player = data["Player"] as IPlayer;
  44. var message = data["Message"] as string;
  45.  
  46. if (!player.HasPermission(Permission))
  47. return;
  48.  
  49. if (message.StartsWith(_config.Prefix) || HasAdminChatEnabled(player))
  50. {
  51. data["CancelOption"] = 1;
  52. }
  53. }
  54.  
  55. #endregion
  56.  
  57. #region Command
  58.  
  59. [Command("adminchat"), Permission(Permission)]
  60. private void AdminChatTogggleCommand(IPlayer player, string command, string[] args)
  61. {
  62. if (_enabledUserIds.Contains(player.Id))
  63. {
  64. _enabledUserIds.Remove(player.Id);
  65. player.Reply(lang.GetMessage("Disabled Admin Chat", this, player.Id));
  66. }
  67. else
  68. {
  69. _enabledUserIds.Add(player.Id);
  70. player.Reply(lang.GetMessage("Enabled Admin Chat", this, player.Id));
  71. }
  72. }
  73.  
  74. #endregion
  75.  
  76. #region Helper
  77.  
  78. private void SendAdminMessage(IPlayer sender, string message, bool fromDiscord)
  79. {
  80. Puts($"{sender.Name}: {message}");
  81. string formattedMessage = _config.Format
  82. .Replace("{name}", sender.Name)
  83. .Replace("{message}", message.Trim());
  84.  
  85. foreach (var player in players.Connected.Where(p => p.HasPermission(Permission)))
  86. {
  87. #if RUST
  88. (player.Object as BasePlayer).SendConsoleCommand("chat.add", new object[] { (int) ConVar.Chat.ChatChannel.Global, sender.Id, covalence.FormatText(formattedMessage) });
  89. #else
  90. player.Message(formattedMessage);
  91. #endif
  92. }
  93.  
  94. Interface.Call("OnAdminChat", sender, message, fromDiscord, formattedMessage);
  95. fromDiscord = false;
  96. }
  97.  
  98. private bool HasAdminChatEnabled(IPlayer player) => _enabledUserIds.Contains(player.Id);
  99.  
  100. #endregion
  101.  
  102. #region Localization
  103.  
  104. protected override void LoadDefaultMessages()
  105. {
  106. lang.RegisterMessages(new Dictionary<string, string>
  107. {
  108. ["Enabled Admin Chat"] = "You are now talking in admin chat.",
  109. ["Disabled Admin Chat"] = "You are no longer talking in admin chat."
  110. }, this);
  111. }
  112.  
  113. #endregion
  114.  
  115. #region Configuration
  116.  
  117. protected override void LoadConfig()
  118. {
  119. base.LoadConfig();
  120. _config = Config.ReadObject<Configuration>();
  121. SaveConfig();
  122. }
  123.  
  124. protected override void LoadDefaultConfig() => _config = new Configuration();
  125.  
  126. protected override void SaveConfig() => Config.WriteObject(_config);
  127.  
  128. private class Configuration
  129. {
  130. [JsonProperty("Prefix (admin chat is used when message starts with this)")]
  131. public string Prefix { get; private set; } = "@";
  132.  
  133. [JsonProperty("Format (how the message is formatted in chat)")]
  134. public string Format { get; private set; } = "[#red]Admin Chat[/#] [#grey]{name}[/#]: {message}";
  135. }
  136.  
  137. #endregion
  138. }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment