/// <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 SetTranslateWiki(CommandContext ctx, [Description("configuring-help-translatewiki-channel")] DiscordChannel channel, [Description("configuring-help-translatewiki-value"), RemainingText] string value) { string chanId = channel.Id.ToString(); string chanPrevId = Config.GetTWChannel(ctx.Guild.Id.ToString()); string chanPrevLang = Config.GetTWLang(ctx.Guild.Id.ToString()); string lang = Config.GetLang(ctx.Guild.Id.ToString()); await ctx.TriggerTypingAsync(); // Check for return to default if (value == "-") { chanId = "-"; value = "-"; } // Check for required parameters if (value.ToString() == "") { await ctx.RespondAsync(Locale.GetMessage("configuring-required-value", lang, ctx.Command.Name, Config.GetValue("prefix"))); return; } // Set language to lowercase value = value.ToLower(); // Check if it is a valid language if (value != "-" && !Locale.IsValidLanguage(value)) { await ctx.RespondAsync(Locale.GetMessage("configuring-badvalue-lang", lang)); return; } // Do action and respond int succeedsChan = Config.SetOverride(ctx.Guild.Id.ToString(), "translatewiki-channel", chanId); int succeedsLang = Config.SetOverride(ctx.Guild.Id.ToString(), "translatewiki-lang", value); if (succeedsChan == Config.RESULT_CHANGE && succeedsLang == Config.RESULT_CHANGE) { // Different channel and language await ctx.RespondAsync(Locale.GetMessage("configuring-changed-translatewiki", lang, channel.Mention, GetLanguageInfo(value))); TranslateWiki.Init(chanId, value); } if (succeedsChan == Config.RESULT_CHANGE && succeedsLang == Config.RESULT_RESET) { // Different channel, default language await ctx.RespondAsync(Locale.GetMessage("configuring-changed-translatewiki-channel", lang, channel.Mention)); TranslateWiki.Remove(chanId, chanPrevLang); TranslateWiki.Init(chanId, value); } if (succeedsChan == Config.RESULT_CHANGE && succeedsLang == Config.RESULT_SAME) { // Different channel, same language await ctx.RespondAsync(Locale.GetMessage("configuring-changed-translatewiki-channel", lang, channel.Mention)); TranslateWiki.Remove(chanPrevId, chanPrevLang); TranslateWiki.Init(chanId, value); } if (succeedsChan == Config.RESULT_RESET && succeedsLang == Config.RESULT_CHANGE || ( succeedsChan == Config.RESULT_SAME && (succeedsLang == Config.RESULT_RESET || succeedsLang == Config.RESULT_CHANGE) ) ) { // Same or default channel, different language await ctx.RespondAsync(Locale.GetMessage("configuring-changed-translatewiki-lang", lang, GetLanguageInfo(value))); TranslateWiki.Remove(chanId, chanPrevLang); TranslateWiki.Init(chanId, value); } if (succeedsChan == Config.RESULT_RESET && succeedsLang == Config.RESULT_RESET) { // Reset both channel and language with - await ctx.RespondAsync(Locale.GetMessage("configuring-changed-reset", lang)); TranslateWiki.Remove(channel.Id.ToString(), chanPrevLang); } if (succeedsChan == Config.RESULT_STRANGE || succeedsLang == Config.RESULT_STRANGE) { // Other strange errors await ctx.RespondAsync(Locale.GetMessage("configuring-error-strange", lang)); } }
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(); }
public async Task SetTranslate(CommandContext ctx, [Description("configuring-help-tw-channel")] DiscordChannel channel, [Description("configuring-help-tw-value"), RemainingText] string value) { string chanId = channel.Id.ToString(); string chanPrevId = Config.GetTWChannel(ctx.Guild.Id.ToString()); string chanPrevLang = Config.GetTWLang(ctx.Guild.Id.ToString()); 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 == "-") { chanId = Config.GetTWChannel(); value = Config.GetTWLang(); } // Check for required parameters if (channel == null) { await ctx.RespondAsync(Locale.GetMessage("configuring-required-channel", lang, "help guildTW", Config.GetValue("prefix"))); return; } if (value.ToString() == "") { await ctx.RespondAsync(Locale.GetMessage("configuring-required-value", lang, "help guildTW", Config.GetValue("prefix"))); return; } // Do action and respond int succeedsChan = Config.SetOverride(ctx.Guild.Id.ToString(), "translatewiki-channel", chanId); int succeedsLang = Config.SetOverride(ctx.Guild.Id.ToString(), "translatewiki-lang", value); if (succeedsChan == Config.RESULT_CHANGE && succeedsLang == Config.RESULT_CHANGE) { // Different channel and language await ctx.RespondAsync(Locale.GetMessage("configuring-changed-translatewiki", lang, channel.Mention, value.ToUpper())); TranslateWiki.Init(chanId, value); } if (succeedsChan == Config.RESULT_CHANGE && succeedsLang == Config.RESULT_RESET) { // Different channel, default language await ctx.RespondAsync(Locale.GetMessage("configuring-changed-translatewiki-channel", lang, channel.Mention)); TranslateWiki.Remove(chanId, chanPrevLang); TranslateWiki.Init(chanId, value); } if (succeedsChan == Config.RESULT_CHANGE && succeedsLang == Config.RESULT_SAME) { // Different channel, same language await ctx.RespondAsync(Locale.GetMessage("configuring-changed-translatewiki-channel", lang, channel.Mention)); TranslateWiki.Remove(chanPrevId, value); TranslateWiki.Init(chanId, value); } if (succeedsChan == Config.RESULT_RESET && succeedsLang == Config.RESULT_CHANGE || ( succeedsChan == Config.RESULT_SAME && (succeedsLang == Config.RESULT_RESET || succeedsLang == Config.RESULT_CHANGE) ) ) { // Same or default channel, different language await ctx.RespondAsync(Locale.GetMessage("configuring-changed-translatewiki-lang", lang, value.ToUpper())); TranslateWiki.Remove(chanId, chanPrevLang); TranslateWiki.Init(chanId, value); } if (succeedsChan == Config.RESULT_RESET && succeedsLang == Config.RESULT_RESET) { // Reset both channel and language with - await ctx.RespondAsync(Locale.GetMessage("configuring-changed-reset", lang)); TranslateWiki.Remove(channel.Id.ToString(), value); } if (succeedsChan == Config.RESULT_STRANGE || succeedsLang == Config.RESULT_STRANGE) { // Other strange errors await ctx.RespondAsync(Locale.GetMessage("configuring-error-strange", lang)); } }