/// <summary>Bot meta reload admin command.</summary>
        public void CMD_Reload(CommandData command)
        {
            // NOTE: This implies a one-guild bot. A multi-guild bot probably shouldn't have this "BotCommander" role-based verification.
            // But under current scale, a true-admin confirmation isn't worth the bother.
            if (!DenizenMetaBot.IsBotCommander(command.Message.Author as SocketGuildUser))
            {
                SendErrorMessageReply(command.Message, "Authorization Failure", "Nope! That's not for you!");
                return;
            }
            SendGenericPositiveMessageReply(command.Message, "Reloading", "Yes, boss. Reloading meta documentation now...");
            BuildNumberTracker.UpdateAll();
            MetaDocs docs = MetaDocsLoader.DownloadAll();

            MetaDocs.CurrentMeta = docs;
            EmbedBuilder embed = new EmbedBuilder().WithTitle("Reload Complete").WithDescription("Documentation reloaded successfully.");

            if (docs.LoadErrors.Count > 0)
            {
                List <string> errors = docs.LoadErrors.Count > 5 ? docs.LoadErrors.GetRange(0, 5) : docs.LoadErrors;
                SendErrorMessageReply(command.Message, "Error(s) While Reloading", string.Join("\n", errors));
                embed.AddField("Errors", docs.LoadErrors.Count, true);
            }
            embed.AddField("Commands", docs.Commands.Count, true);
            embed.AddField("Mechanisms", docs.Mechanisms.Count, true);
            embed.AddField("Tags", docs.Tags.Count, true);
            embed.AddField("Object Types", docs.ObjectTypes.Count, true);
            embed.AddField("Events", docs.Events.Count, true);
            embed.AddField("Actions", docs.Actions.Count, true);
            embed.AddField("Languages", docs.Languages.Count, true);
            embed.AddField("Guide Pages", docs.GuidePages.Count, true);
            SendReply(command.Message, embed.Build());
            foreach (string url in DenizenMetaBot.ReloadWebooks)
            {
                try
                {
                    Program.ReusableWebClient.PostAsync(url, new ByteArrayContent(Array.Empty <byte>())).Wait();
                }
                catch (Exception ex)
                {
                    Console.Error.Write($"Failed to ping webhook URL '{url}': {ex}");
                }
            }
        }
Exemple #2
0
        public static void ReloadMeta()
        {
            lock (ReloadTimeLock)
            {
                DateTimeOffset now = DateTimeOffset.UtcNow;
                if (now.Subtract(LastReload).TotalSeconds < 15)
                {
                    Console.WriteLine("Ignoring too-fast reload...");
                    return;
                }
                LastReload = now;
            }
            lock (ReloadLock)
            {
                Console.WriteLine("Reloading meta...");
                MetaDocs docs = MetaDocsLoader.DownloadAll();
                Console.WriteLine("Meta loaded, HTMLizing...");
                List <WebsiteMetaCommand>    _commands    = new List <WebsiteMetaCommand>();
                List <WebsiteMetaTag>        _tags        = new List <WebsiteMetaTag>();
                List <WebsiteMetaObjectType> _objectTypes = new List <WebsiteMetaObjectType>();
                List <WebsiteMetaEvent>      _events      = new List <WebsiteMetaEvent>();
                List <WebsiteMetaAction>     _actions     = new List <WebsiteMetaAction>();
                List <WebsiteMetaLanguage>   _languages   = new List <WebsiteMetaLanguage>();
                List <WebsiteMetaMechanism>  _mechanisms  = new List <WebsiteMetaMechanism>();
                List <WebsiteMetaObject>     _allObjects  = new List <WebsiteMetaObject>();
                void procSet <T, T2>(ref List <T> webObjs, ICollection <T2> origObjs) where T : WebsiteMetaObject <T2>, new() where T2 : MetaObject
                {
                    foreach (T2 obj in origObjs)
                    {
                        T webObj = new T()
                        {
                            Object = obj
                        };
                        webObjs.Add(webObj);
                    }
                    webObjs = webObjs.OrderBy(o => string.IsNullOrWhiteSpace(o.Object.Plugin) ? 0 : 1).ThenBy(o => o.Object.Warnings.Count).ThenBy(o => o.Object.Group).ThenBy(o => o.Object.CleanName).ToList();
                    _allObjects.AddRange(webObjs);
                }

                procSet(ref _commands, docs.Commands.Values);
                procSet(ref _tags, docs.Tags.Values);
                procSet(ref _objectTypes, docs.ObjectTypes.Values);
                procSet(ref _events, docs.Events.Values);
                procSet(ref _actions, docs.Actions.Values);
                procSet(ref _languages, docs.Languages.Values);
                procSet(ref _mechanisms, docs.Mechanisms.Values);
                foreach (WebsiteMetaObject obj in _allObjects)
                {
                    obj.Docs = docs;
                    obj.LoadHTML();
                    obj.AllSearchableText = obj.ObjectGeneric.GetAllSearchableText().ToLowerFast();
                }
                Commands             = _commands;
                Tags                 = _tags;
                ObjectTypes          = _objectTypes;
                Events               = _events;
                Actions              = _actions;
                Languages            = _languages;
                Mechanisms           = _mechanisms;
                AllObjects           = _allObjects;
                MetaDocs.CurrentMeta = docs;
                Console.WriteLine("Meta loaded and ready!");
            }
        }