Advertisement
OtKashix

Untitled

Jan 22nd, 2021
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Net;
  6. using System.Globalization;
  7. using Newtonsoft.Json;
  8. using Oxide.Core;
  9. using Oxide.Core.Plugins;
  10. using UnityEngine;
  11. using Oxide.Ext.Discord;
  12. using Oxide.Ext.Discord.Attributes;
  13. using Oxide.Ext.Discord.DiscordObjects;
  14. using Oxide.Core.Libraries.Covalence;
  15.  
  16. namespace Oxide.Plugins
  17. {
  18.     [Info("Fantasmeo Bot", "Kashix.", "0.1.0")]
  19.     [Description("Discord Bot for Fantasmeo Island server.")]
  20.  
  21.     class Fantasmeo : RustPlugin{
  22.    
  23.         [PluginReference] Plugin Discord;
  24.         [DiscordClient] private DiscordClient _client;
  25.  
  26.         private Settings _settings;
  27.         private Timer StatusTimer = null;
  28.  
  29.         private Presence DiscordPresence = new Presence()
  30.         {
  31.             Game = new Ext.Discord.DiscordObjects.Game()
  32.             {
  33.                 Type = ActivityType.Game,
  34.                 Name = "Fanstameo cargando..."
  35.             }
  36.         };
  37.  
  38.         // Configuration
  39.         private class Settings
  40.         {
  41.             public string Apikey { get; set; }
  42.             public bool AutoReloadPlugin { get; set; }
  43.             public int AutoReloadTime { get; set; }
  44.             public bool EnableBotStatus { get; set; }
  45.         }
  46.  
  47.         // Channel configuration
  48.         public class Channel
  49.         {
  50.             public string Channelid { get; set; }
  51.             public List<string> perms { get; set; }
  52.         }
  53.  
  54.         private Settings GetDefaultSettings()
  55.         {
  56.             return new Settings
  57.             {
  58.                 ApiKey = "Nzc0MjQ0NzYxMDkxNDQwNjUw.X6U9nw.pgBxizR282mw19Wp6p2ytKVW04s",
  59.                 AutoReloadPlugin = true,
  60.                 AutoReloadTime = 900
  61.             };
  62.         }
  63.  
  64.         protected override void LoadDefaultConfig()
  65.         {
  66.             PrintWarning("Intentando crear un nuevo archivo de configuración...");
  67.             Config.Clear();
  68.             Config.WriteObject(GetDefaultSettings(), true);
  69.             Config.Save();
  70.         }
  71.  
  72.         private void OnServerInitialized()
  73.         {
  74.             var reloadtime = _settings.AutoReloadTime;
  75.  
  76.             if (_settings.AutoReloadPlugin && _settings.AutoReloadTime > 59)
  77.             {
  78.                 timer.Every(reloadtime, () => Reload());
  79.             }
  80.         }
  81.  
  82.         private void Loaded()
  83.         {
  84.             _settings = Config.ReadObject<Settings>();
  85.  
  86.             // Asegurar que la configuración está guardada
  87.             if (_settings.Channels == null)
  88.                 _settings.Channels = new List<Settings.Channel>();
  89.                
  90.             Config.WriteObject(_settings, true);
  91.  
  92.             if (string.IsNullOrEmpty(_settings.Apikey) || _settings.Apikey == null || _settings.Apikey == "BotToken")
  93.             {
  94.                 PrintError("La clave API no es correcta o no está configurada.");
  95.                 return;
  96.             }
  97.             bool flag = true;
  98.             try
  99.             {
  100.                 Oxide.Ext.Discord.Discord.CreateClient(this, _settings.Apikey);
  101.             }
  102.             catch (Exception e)
  103.             {
  104.                 flag = false;
  105.                 PrintError($"No se ha podido crear un cliente de Discord, error: {e.Message}");
  106.             }
  107.         }
  108.  
  109.         private void Reload()
  110.         {
  111.             rust.RunServerCommand("oxide.reload FanstasmeoBot");
  112.         }
  113.  
  114.         private void Unload()
  115.         {
  116.             if (StatusTimer != null && !StatusTimer.Destroyed)
  117.             {
  118.                 StatusTimer.Destroy();
  119.             }
  120.             if (_settings.EnableCustomLogging)
  121.                 UnityEngine.Application.logMessageReceived -= ConsoleLog;
  122.             Discord.CloseClient(_client);
  123.         }
  124.  
  125.         void Discord_Ready(Oxide.Ext.Discord.DiscordEvents.Ready rdy)
  126.         {
  127.             //_settings.Botid(rdy.User.id);
  128.             _channelCount = _settings?.Channels.Count;
  129.             if (_settings.EnableBotStatus)
  130.             {
  131.                 NextFrame(() =>
  132.                 {
  133.                     if (StatusTimer != null && !StatusTimer.Destroyed)
  134.                     {
  135.                         StatusTimer.Destroy();
  136.                     }
  137.                     StatusTimer = timer.Every(10f, () =>
  138.                     {
  139.                         var text = new Dictionary<string, string>
  140.                         {
  141.                             ["players"] = Convert.ToString(BasePlayer.activePlayerList.Count),
  142.                             ["maxplayers"] = Convert.ToString(ConVar.Server.maxplayers),
  143.                             ["sleepers"] = Convert.ToString(BasePlayer.sleepingPlayerList.Count)
  144.                         };
  145.                         var msg = Translate("Discord_Status", text);
  146.                         DiscordPresence.Game.Name = string.IsNullOrEmpty(msg) ? "Iniciando bot..." : msg;
  147.                         _client.UpdateStatus(DiscordPresence);
  148.                     });
  149.                 });
  150.             }
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement