public static void Init() { Config = FDSUtility.ReadFile("config/config.fds"); ReloadWebhookToken = Config.GetString("reload-webhook-token"); if (Config.HasKey("alt-sources")) { MetaDocsLoader.SourcesToUse = Config.GetStringList("alt-sources").ToArray(); } ReloadMeta(); }
/// <summary>Loads the paste config.</summary> public static void LoadConfig() { FDSSection Config = FDSUtility.ReadFile("config/config.fds"); URL_BASE = Config.GetString("url-base"); MaxPasteRawLength = Config.GetInt("max-paste-size").Value; TrustXForwardedFor = Config.GetBool("trust-x-forwarded-for").Value; MaxPastesPerMinute = Config.GetInt("max-pastes-per-minute").Value; NewPasteWebhooks = (Config.GetStringList("webhooks.new-paste") ?? new List <string>()).ToArray(); Console.WriteLine($"Loaded at URL-base {URL_BASE} with max length {MaxPasteRawLength} with ratelimit {MaxPastesPerMinute} and x-forwarded-for set {TrustXForwardedFor}"); }
/// <summary> /// Initializes the bot object, connects, and runs the active loop. /// </summary> public void InitAndRun(string[] args) { Console.WriteLine("Preparing..."); BotMonitor = new ConnectionMonitor(this); if (File.Exists(CONFIG_FILE)) { lock (ConfigSaveLock) { ConfigFile = FDSUtility.ReadFile(CONFIG_FILE); } } PopulateFromConfig(); DefaultCommands(); Console.WriteLine("Loading Discord..."); DiscordSocketConfig config = new DiscordSocketConfig { MessageCacheSize = 256 }; //config.LogLevel = LogSeverity.Debug; Client = new DiscordSocketClient(config); /*Client.Log += (m) => * { * Console.WriteLine(m.Severity + ": " + m.Source + ": " + m.Exception + ": " + m.Message); * return Task.CompletedTask; * };*/ Client.Ready += () => { if (BotMonitor.ShouldStopAllLogic()) { return(Task.CompletedTask); } BotMonitor.ConnectedCurrently = true; Client.SetGameAsync("Type !help").Wait(); if (BotMonitor.ConnectedOnce) { return(Task.CompletedTask); } Console.WriteLine($"Args: {args.Length}"); if (args.Length > 0 && ulong.TryParse(args[0], out ulong argument1)) { ISocketMessageChannel channelToNotify = Client.GetChannel(argument1) as ISocketMessageChannel; Console.WriteLine($"Restarted as per request in channel: {channelToNotify.Name}"); channelToNotify.SendMessageAsync(embed: UserCommands.GetGenericPositiveMessageEmbed("Restarted", "Connected and ready!")).Wait(); } BotMonitor.ConnectedOnce = true; return(Task.CompletedTask); }; Client.MessageReceived += (message) => { if (BotMonitor.ShouldStopAllLogic()) { return(Task.CompletedTask); } if (message.Author.Id == Client.CurrentUser.Id) { return(Task.CompletedTask); } BotMonitor.LoopsSilent = 0; if (message.Author.IsBot || message.Author.IsWebhook) { return(Task.CompletedTask); } if (message.Channel.Name.StartsWith("@") || !(message.Channel is SocketGuildChannel sgc)) { Console.WriteLine($"Refused message from ({message.Author.Username}): (Invalid Channel: {message.Channel.Name}): {message.Content}"); return(Task.CompletedTask); } if (ValidChannels.Count != 0 && !ValidChannels.Contains(message.Channel.Id)) { Console.WriteLine($"Refused message from ({message.Author.Username}): (Non-whitelisted Channel: {message.Channel.Name}): {message.Content}"); return(Task.CompletedTask); } bool mentionedMe = message.MentionedUsers.Any((su) => su.Id == Client.CurrentUser.Id); Console.WriteLine($"Parsing message from ({message.Author.Username}), in channel: {message.Channel.Name}: {message.Content}"); if (mentionedMe || message.Content.StartsWith(Constants.COMMAND_PREFIX)) { try { Respond(message, mentionedMe); } catch (Exception ex) { if (ex is ThreadAbortException) { throw; } Console.WriteLine($"Error handling command: {ex.ToString()}"); } } return(Task.CompletedTask); }; Console.WriteLine("Logging in to Discord..."); Client.LoginAsync(TokenType.Bot, TOKEN).Wait(); Console.WriteLine("Connecting to Discord..."); Client.StartAsync().Wait(); Console.WriteLine("Running Discord!"); Console.WriteLine("Starting monitor..."); BotMonitor.StartMonitorLoop(); StoppedEvent.WaitOne(); }