Иконка ресурса

FREE AskPlugin : 0.0.1 0.0.1

Нет прав для скачивания
C#:
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Core.Libraries.Covalence;
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using Oxide.Core.Libraries;

namespace Oxide.Plugins
{
    [Info("Ask Plugin", "AKUSIK", "0.0.1")]
    [Description("Позволяет игрокам отправлять просьбы администраторам через команду /ask")]
    public class AskPlugin : RustPlugin
    {
        private ConfigData config;
        
        private Dictionary<ulong, DateTime> lastAskTime = new Dictionary<ulong, DateTime>();

        void Init()
        {
            permission.RegisterPermission("askplugin.use", this);
            cmd.AddChatCommand("ask", this, "CmdAsk");
            
            LoadConfig();
            LoadData();
            
            Puts($"[{config.PluginPrefix}] Плагин загружен успешно!");
        }

        void OnServerInitialized()
        {
            Puts($"[{config.PluginPrefix}] Сервер инициализирован!");
        }

        void Unload()
        {
            Puts($"[{config.PluginPrefix}] Плагин выгружен!");
        }

        [ChatCommand("ask")]
        void CmdAsk(BasePlayer player, string command, string[] args)
        {
            if (args.Length == 0)
            {
                player.ChatMessage($"[{config.PluginPrefix}] Использование: /ask <ваша просьба>");
                return;
            }

            if (!permission.UserHasPermission(player.UserIDString, config.PermissionNode))
            {
                player.ChatMessage($"[{config.PluginPrefix}] У вас нет прав на использование этой команды!");
                return;
            }

            if (IsOnCooldown(player.userID))
            {
                var remainingTime = GetRemainingCooldown(player.userID);
                player.ChatMessage($"[{config.PluginPrefix}] Подождите {remainingTime} секунд перед следующей просьбой.");
                return;
            }

            string request = string.Join(" ", args);
            SendToDiscord(player, request);
            
            SetCooldown(player.userID);
            
            player.ChatMessage($"[{config.PluginPrefix}] Ваша просьба отправлена администраторам!");
        }

        private bool IsOnCooldown(ulong playerID)
        {
            if (!lastAskTime.ContainsKey(playerID))
                return false;

            var timeSinceLastAsk = DateTime.Now - lastAskTime[playerID];
            return timeSinceLastAsk.TotalMinutes < config.AskCooldownMinutes;
        }

        private int GetRemainingCooldown(ulong playerID)
        {
            if (!lastAskTime.ContainsKey(playerID))
                return 0;

            var timeSinceLastAsk = DateTime.Now - lastAskTime[playerID];
            var remainingSeconds = (config.AskCooldownMinutes * 60) - (int)timeSinceLastAsk.TotalSeconds;
            return Math.Max(0, remainingSeconds);
        }

        private void SetCooldown(ulong playerID)
        {
            lastAskTime[playerID] = DateTime.Now;
        }

        private void SendToDiscord(BasePlayer player, string request)
        {
            try
            {
                var embed = new DiscordEmbed
                {
                    title = config.DiscordEmbedTitle,
                    color = config.DiscordEmbedColor,
                    timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"),
                    fields = new List<DiscordField>
                    {
                        new DiscordField
                        {
                            name = "👤 Игрок",
                            value = player.displayName,
                            inline = true
                        },
                        new DiscordField
                        {
                            name = "🆔 Steam ID",
                            value = player.UserIDString,
                            inline = true
                        },
                        new DiscordField
                        {
                            name = "🌍 Сервер",
                            value = ConVar.Server.hostname,
                            inline = true
                        },
                        new DiscordField
                        {
                            name = "📝 Просьба",
                            value = request,
                            inline = false
                        },
                        new DiscordField
                        {
                            name = "⏰ Время",
                            value = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"),
                            inline = true
                        }
                    },
                    footer = new DiscordFooter
                    {
                        text = config.DiscordFooterText
                    }
                };

                var payload = new DiscordPayload
                {
                    embeds = new List<DiscordEmbed> { embed }
                };

                webrequest.Enqueue(config.DiscordWebhookUrl, JsonConvert.SerializeObject(payload), 
                    (code, response) => 
                    {
                        if (code == 204)
                        {
                            if (config.LogToConsole)
                                Puts($"[{config.PluginPrefix}] Просьба успешно отправлена в Discord от игрока {player.displayName}");
                        }
                        else
                        {
                            if (config.LogToConsole)
                                Puts($"[{config.PluginPrefix}] Ошибка отправки в Discord: {code} - {response}");
                        }
                    }, 
                    this, RequestMethod.POST, 
                    new Dictionary<string, string> { { "Content-Type", "application/json" } });
            }
            catch (Exception ex)
            {
                if (config.LogToConsole)
                    Puts($"[{config.PluginPrefix}] Ошибка при отправке в Discord: {ex.Message}");
            }
        }

        void LoadData()
        {
            lastAskTime = new Dictionary<ulong, DateTime>();
        }

        protected override void LoadDefaultConfig()
        {
            Config.WriteObject(GetDefaultConfig(), true);
        }

        private ConfigData GetDefaultConfig()
        {
            return new ConfigData
            {
                DiscordWebhookUrl = "https://discord.com/api/webhooks/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN",
                AskCooldownMinutes = 5,
                PluginPrefix = "AskPlugin",
                DiscordEmbedColor = 0x00ff00,
                DiscordEmbedTitle = "🎯 Новая просьба от игрока",
                DiscordFooterText = "AskPlugin - RUST Server",
                PermissionNode = "askplugin.use",
                LogToConsole = true
            };
        }

        void LoadConfig()
        {
            config = Config.ReadObject<ConfigData>();
            if (config == null)
            {
                LoadDefaultConfig();
                config = Config.ReadObject<ConfigData>();
            }
        }
    }

    public class ConfigData
    {
        public string DiscordWebhookUrl { get; set; }
        public int AskCooldownMinutes { get; set; }
        public string PluginPrefix { get; set; }
        public int DiscordEmbedColor { get; set; }
        public string DiscordEmbedTitle { get; set; }
        public string DiscordFooterText { get; set; }
        public string PermissionNode { get; set; }
        public bool LogToConsole { get; set; }
    }

    public class DiscordPayload
    {
        public List<DiscordEmbed> embeds { get; set; }
    }

    public class DiscordEmbed
    {
        public string title { get; set; }
        public string description { get; set; }
        public int color { get; set; }
        public string timestamp { get; set; }
        public List<DiscordField> fields { get; set; }
        public DiscordFooter footer { get; set; }
    }

    public class DiscordField
    {
        public string name { get; set; }
        public string value { get; set; }
        public bool inline { get; set; }
    }

    public class DiscordFooter
    {
        public string text { get; set; }
    }
}

Автор
15FPS
Скачиваний
0
Просмотры
2
Первый выпуск
Обновление
Оценка
0.00 звёзд 0 оценок
Сверху