public async Task SetWiki(CommandContext ctx, [Description("configuring-help-wiki-value"), RemainingText] string value) { string lang = Config.GetLang(ctx.Guild.Id.ToString()); // Ensure that we are in private channel if (ctx.Channel.Name != "moderators") { await ctx.RespondAsync(Locale.GetMessage("denied", lang)); return; } ; await ctx.TriggerTypingAsync(); // Check for return to default if (value == "-") { value = Config.GetWiki(); } // Check for required parameters if (value.ToString() == "") { await ctx.RespondAsync(Locale.GetMessage("configuring-required-value", lang, "help guildWiki", Config.GetValue("prefix"))); return; } if (!value.Contains("/wiki/$1")) { await ctx.RespondAsync(Locale.GetMessage("configuring-badvalue-wiki", lang)); } // Provide some changes value = value.Replace("<", String.Empty).Replace(">", String.Empty); // Do action and respond int succeeds = Config.SetOverride(ctx.Guild.Id.ToString(), "wiki", value); if (succeeds == Config.RESULT_CHANGE) { Linking.Init(ctx.Guild.Id.ToString()); await ctx.RespondAsync(Locale.GetMessage("configuring-changed-wiki", lang, value)); } if (succeeds == Config.RESULT_RESET) { Linking.Remove(ctx.Guild.Id.ToString()); } await RespondOnErrors(succeeds, ctx, lang); }
public async Task SetChannelWiki(CommandContext ctx, [Description("configuring-help-wiki-value"), RemainingText] string value) { string lang = Config.GetLang(ctx.Guild.Id.ToString()); await ctx.TriggerTypingAsync(); // Check for required parameters if (value.ToString() == "") { await ctx.RespondAsync(Locale.GetMessage("configuring-required-value", lang, ctx.Command.Name, Config.GetValue("prefix"))); return; } if (value != "-" && !value.Contains("/wiki/$1")) { await ctx.RespondAsync(Locale.GetMessage("configuring-badvalue-wiki", lang, "/wiki/$1")); return; } // Provide some changes value = value.Replace("<", String.Empty).Replace(">", String.Empty); // Reset to default server value if necessary if (value == Config.GetWiki(ctx.Guild.Id.ToString())) { value = "-"; } // Do action and respond int succeeds = Config.SetOverride($"#{ctx.Channel.Id.ToString()}", "wiki", value); if (succeeds == Config.RESULT_CHANGE) { Linking.Init($"#{ctx.Channel.Id.ToString()}"); await ctx.RespondAsync(Locale.GetMessage("configuring-changed-wiki-channel", lang, value)); } if (succeeds == Config.RESULT_RESET) { Linking.Remove($"#{ctx.Channel.Id.ToString()}"); } await RespondOnErrors(succeeds, ctx, lang); }
/// <summary> /// Initialise the common functions for every server. /// </summary> /// <param name="e">Discord event information.</param> private Task Client_GuildAvailable(DiscordClient sender, GuildCreateEventArgs e) { // Log the name of the guild that just became available LogMessage($"Server is loaded: {e.Guild.Name}"); // Load custom values if needed string guild = e.Guild.Id.ToString(); Task.Run(async() => { Linking.Init(guild); Locale.Init(Config.GetLang(guild)); if (Config.GetTWChannel(guild) != null && Config.GetTWLang(guild) != null) { TranslateWiki.Init(Config.GetTWChannel(guild), Config.GetTWLang(guild)); } }); return(Task.CompletedTask); }
private Task Client_GuildAvailable(GuildCreateEventArgs e) { // Log the name of the guild that just became available e.Client.DebugLogger.LogMessage(LogLevel.Info, "DiscordWikiBot", $"Guild available: {e.Guild.Name}", DateTime.Now); // Load custom values if needed string guild = e.Guild.Id.ToString(); Linking.Init(guild); Locale.LoadCustomLocale(Config.GetLang(guild)); if (Config.GetTWChannel() != null && Config.GetTWLang() != null) { TranslateWiki.Init(Config.GetTWChannel(guild), Config.GetTWLang(guild)); } if (Config.GetDomain() != "") { EventStreams.Subscribe(Config.GetDomain(guild)); } return(Task.FromResult(0)); }
/// <summary> /// Initialise the bot and keep it running /// </summary> public async Task Run() { // Set proper TLS settings ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; // Check for a token string tokenPath = @"token.txt"; if (!File.Exists(tokenPath)) { Console.WriteLine("Please create a file called \"token.txt\" before running the bot!"); Console.WriteLine("[Press any key to exit...]"); Console.ReadKey(); Environment.Exit(0); } Token = File.ReadAllText(tokenPath, Encoding.Default); // Get JSON config file Config.Init(); // Initialise Discord client Client = new DiscordClient(new DiscordConfiguration() { AutoReconnect = true, LargeThreshold = 250, MinimumLogLevel = LogLevel.Information, Token = Token, TokenType = TokenType.Bot, }); // Initialise events LogMessage($"DiscordWikiBot, version {Version}"); // Get default locale Locale.Init(); // Get site information and start linking bot LogMessage("Getting wiki site information"); Linking.Init(); // Methods for linking bot Client.MessageCreated += (s, e) => { Task.Run(async() => { await Linking.Answer(s, e); }); return(Task.CompletedTask); }; Client.MessageUpdated += (s, e) => { Task.Run(async() => { await Linking.Edit(s, e); }); return(Task.CompletedTask); }; Client.MessageDeleted += Linking.Delete; Client.MessagesBulkDeleted += Linking.BulkDelete; // Start EventStreams if (Config.GetDomain() != null) { EventStreams.Init(); } // Start Translatewiki fetches if (Config.GetTWChannel() != null && Config.GetTWLang() != null) { TranslateWiki.Init(); } // Set some events for logging the information Client.Ready += Client_Ready; Client.GuildAvailable += Client_GuildAvailable; Client.GuildCreated += Client_GuildCreated; Client.GuildDeleted += Client_GuildDeleted; Client.ClientErrored += Client_ClientErrored; // Initialise commands LogMessage("Setting up commands"); Commands = Client.UseCommandsNext(new CommandsNextConfiguration { StringPrefixes = new[] { Config.GetValue("prefix") }, EnableDms = false, EnableMentionPrefix = true, }); Commands.RegisterCommands <Pinging>(); Commands.RegisterCommands <Configuring>(); if (EventStreams.Enabled) { Commands.RegisterCommands <Streaming>(); } // Set up custom formatter Commands.SetHelpFormatter <LocalisedHelpFormatter>(); // Connect and start LogMessage("Connecting..."); await Client.ConnectAsync(); // Make sure not to close down automatically await CtrlC(); }
public async Task Run() { // Set proper TLS settings ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; // Check for a token string tokenPath = @"token.txt"; if (!File.Exists(tokenPath)) { Console.WriteLine("Please create a file called \"token.txt\" before running the bot!"); Console.WriteLine("[Press any key to exit...]"); Console.ReadKey(); Environment.Exit(0); } Token = File.ReadAllText(tokenPath); // Get JSON config file Config.Init(); // Initialise Discord client Client = new DiscordClient(new DiscordConfiguration() { AutoReconnect = true, LargeThreshold = 250, LogLevel = LogLevel.Info, Token = Token, TokenType = TokenType.Bot, UseInternalLogHandler = true, }); // Initialise events Client.DebugLogger.LogMessage(LogLevel.Info, "DiscordWikiBot", "Initialising events", DateTime.Now); // Get locale Client.DebugLogger.LogMessage(LogLevel.Info, "DiscordWikiBot", string.Format("Loading {0} locale", Config.GetLang().ToUpper()), DateTime.Now); Locale.Init(); // Get site information and start linking bot Client.DebugLogger.LogMessage(LogLevel.Info, "DiscordWikiBot", "Getting wiki site information", DateTime.Now); Linking.Init(); // Methods for linking bot Client.MessageCreated += Linking.Answer; Client.MessageUpdated += Linking.Edit; Client.MessageDeleted += Linking.Delete; // Start EventStreams if (Config.GetDomain() != "") { EventStreams.Init(); } // Start Translatewiki fetches if (Config.GetTWChannel() != null && Config.GetTWLang() != null) { Client.DebugLogger.LogMessage(LogLevel.Info, "DiscordWikiBot", $"Turning on Translatewiki ({Config.GetTWLang()})", DateTime.Now); TranslateWiki.Init(); } // Set some events for logging the information Client.Ready += Client_Ready; Client.GuildAvailable += Client_GuildAvailable; Client.ClientErrored += Client_ClientErrored; // Initialise commands Client.DebugLogger.LogMessage(LogLevel.Info, "DiscordWikiBot", "Setting up commands", DateTime.Now); Commands = Client.UseCommandsNext(new CommandsNextConfiguration { StringPrefix = Config.GetValue("prefix"), EnableDms = false, EnableMentionPrefix = true, }); Commands.RegisterCommands <Configuring>(); Commands.RegisterCommands <Streaming>(); // Set up custom formatter Commands.SetHelpFormatter <LocalisedHelpFormatter>(); // Connect and start Client.DebugLogger.LogMessage(LogLevel.Info, "DiscordWikiBot", "Connecting...", DateTime.Now); await Client.ConnectAsync(); // Make sure not to close down automatically await CtrlC(); }