diff --git a/NadekoBot/Classes/NadekoStats.cs b/NadekoBot/Classes/NadekoStats.cs index 15953d61..57d730d4 100644 --- a/NadekoBot/Classes/NadekoStats.cs +++ b/NadekoBot/Classes/NadekoStats.cs @@ -7,7 +7,9 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; +#if NADEKO_RELEASE using System.IO; +#endif using System.Linq; using System.Net.Http; using System.Reflection; @@ -227,6 +229,7 @@ namespace NadekoBot DateTime dt; if (!commandTracker.TryGetValue(e.Message.Id, out dt)) return; +#if NADEKO_RELEASE try { if (e is CommandErrorEventArgs) @@ -248,6 +251,7 @@ namespace NadekoBot } } catch { } +#endif } private async void StatsCollector_RanCommand(object sender, CommandEventArgs e) diff --git a/NadekoBot/Classes/SearchHelper.cs b/NadekoBot/Classes/SearchHelper.cs index a0a59e9b..e5bbcc2e 100644 --- a/NadekoBot/Classes/SearchHelper.cs +++ b/NadekoBot/Classes/SearchHelper.cs @@ -61,10 +61,31 @@ namespace NadekoBot.Classes IEnumerable> headers = null, RequestHttpMethod method = RequestHttpMethod.Get) { - - using (var streamReader = new StreamReader(await GetResponseStreamAsync(url, headers, method).ConfigureAwait(false))) + if (string.IsNullOrWhiteSpace(url)) + throw new ArgumentNullException(nameof(url)); + var cl = new HttpClient(); + cl.DefaultRequestHeaders.Clear(); + switch (method) { - return await streamReader.ReadToEndAsync().ConfigureAwait(false); + case RequestHttpMethod.Get: + if (headers != null) + { + foreach (var header in headers) + { + cl.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value); + } + } + return await cl.GetStringAsync(url).ConfigureAwait(false); + case RequestHttpMethod.Post: + FormUrlEncodedContent formContent = null; + if (headers != null) + { + formContent = new FormUrlEncodedContent(headers); + } + var message = await cl.PostAsync(url, formContent).ConfigureAwait(false); + return await message.Content.ReadAsStringAsync().ConfigureAwait(false); + default: + throw new NotImplementedException("That type of request is unsupported."); } } diff --git a/NadekoBot/Classes/lib/sqlite3.dll b/NadekoBot/Classes/lib/sqlite3.dll new file mode 100644 index 00000000..c68c1f14 Binary files /dev/null and b/NadekoBot/Classes/lib/sqlite3.dll differ diff --git a/NadekoBot/Modules/Searches/Commands/StreamNotifications.cs b/NadekoBot/Modules/Searches/Commands/StreamNotifications.cs index 11609db2..153c48db 100644 --- a/NadekoBot/Modules/Searches/Commands/StreamNotifications.cs +++ b/NadekoBot/Modules/Searches/Commands/StreamNotifications.cs @@ -7,71 +7,83 @@ using System; using System.Collections.Concurrent; using System.Linq; using System.Threading.Tasks; -using System.Timers; namespace NadekoBot.Modules.Searches.Commands { internal class StreamNotifications : DiscordCommand { - - private readonly Timer checkTimer = new Timer - { - Interval = new TimeSpan(0, 0, 15).TotalMilliseconds, - }; - private ConcurrentDictionary> cachedStatuses = new ConcurrentDictionary>(); + private bool FirstPass { get; set; } = true; public StreamNotifications(DiscordModule module) : base(module) { - - checkTimer.Elapsed += async (s, e) => + //start checking only after ready, because we need all servers to be initialized + NadekoBot.OnReady += () => Task.Run(async () => { - cachedStatuses.Clear(); - try + while (true) { - var streams = SpecificConfigurations.Default.AllConfigs.SelectMany(c => c.ObservingStreams); - if (!streams.Any()) return; - - foreach (var stream in streams) + cachedStatuses.Clear(); + try { - Tuple data; - try + var streams = SpecificConfigurations.Default.AllConfigs.SelectMany(c => c.ObservingStreams); + if (!streams.Any()) return; +#if NADEKO_RELEASE + var clr = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Blue; + Console.WriteLine($"Getting {streams.Count()} streams."); + Console.ForegroundColor = clr; +#endif + foreach (var stream in streams) { - data = await GetStreamStatus(stream).ConfigureAwait(false); - } - catch - { - continue; - } - - if (data.Item1 != stream.LastStatus) - { - stream.LastStatus = data.Item1; - var server = NadekoBot.Client.GetServer(stream.ServerId); - var channel = server?.GetChannel(stream.ChannelId); - if (channel == null) + Tuple data; + try + { + data = await GetStreamStatus(stream).ConfigureAwait(false); + } + catch + { continue; - var msg = $"`{stream.Username}`'s stream is now " + - $"**{(data.Item1 ? "ONLINE" : "OFFLINE")}** with " + - $"**{data.Item2}** viewers."; - if (stream.LastStatus) - if (stream.Type == StreamNotificationConfig.StreamType.Hitbox) - msg += $"\n`Here is the Link:`【 http://www.hitbox.tv/{stream.Username}/ 】"; - else if (stream.Type == StreamNotificationConfig.StreamType.Twitch) - msg += $"\n`Here is the Link:`【 http://www.twitch.tv/{stream.Username}/ 】"; - else if (stream.Type == StreamNotificationConfig.StreamType.Beam) - msg += $"\n`Here is the Link:`【 http://www.beam.pro/{stream.Username}/ 】"; - else if (stream.Type == StreamNotificationConfig.StreamType.YoutubeGaming) - msg += $"\n`Here is the Link:`【 not implemented yet - {stream.Username} 】"; - await channel.SendMessage(msg).ConfigureAwait(false); + } + + if (data.Item1 != stream.LastStatus) + { + stream.LastStatus = data.Item1; + if (FirstPass) + continue; + var server = NadekoBot.Client.GetServer(stream.ServerId); + var channel = server?.GetChannel(stream.ChannelId); + if (channel == null) + continue; + var msg = $"`{stream.Username}`'s stream is now " + + $"**{(data.Item1 ? "ONLINE" : "OFFLINE")}** with " + + $"**{data.Item2}** viewers."; + if (stream.LastStatus) + if (stream.Type == StreamNotificationConfig.StreamType.Hitbox) + msg += $"\n`Here is the Link:`【 http://www.hitbox.tv/{stream.Username}/ 】"; + else if (stream.Type == StreamNotificationConfig.StreamType.Twitch) + msg += $"\n`Here is the Link:`【 http://www.twitch.tv/{stream.Username}/ 】"; + else if (stream.Type == StreamNotificationConfig.StreamType.Beam) + msg += $"\n`Here is the Link:`【 http://www.beam.pro/{stream.Username}/ 】"; + else if (stream.Type == StreamNotificationConfig.StreamType.YoutubeGaming) + msg += $"\n`Here is the Link:`【 not implemented yet - {stream.Username} 】"; + await channel.SendMessage(msg).ConfigureAwait(false); + } } + FirstPass = false; +#if NADEKO_RELEASE + clr = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Blue; + Console.WriteLine($"Done getting streams."); + Console.ForegroundColor = clr; +#endif + } + catch { } + finally + { + await Task.Delay(TimeSpan.FromSeconds(15)); } } - catch { } - await ConfigHandler.SaveConfig().ConfigureAwait(false); - }; - //start checking only after ready, because we need all servers to be initialized - NadekoBot.OnReady += checkTimer.Start; + }); } private async Task> GetStreamStatus(StreamNotificationConfig stream, bool checkCache = true) diff --git a/NadekoBot/Modules/Searches/SearchesModule.cs b/NadekoBot/Modules/Searches/SearchesModule.cs index 2662107a..4caa708b 100644 --- a/NadekoBot/Modules/Searches/SearchesModule.cs +++ b/NadekoBot/Modules/Searches/SearchesModule.cs @@ -300,10 +300,11 @@ $@"🌍 **Weather for** 【{obj["target"]}】 { var items = JObject.Parse(res); var sb = new System.Text.StringBuilder(); - sb.AppendLine($"`Term:` {items["list"][0]["word"].ToString()}"); - sb.AppendLine($"`Definition:` {items["list"][0]["definition"].ToString()}"); - sb.Append($"`Link:` <{await items["list"][0]["permalink"].ToString().ShortenUrl().ConfigureAwait(false)}>"); - await e.Channel.SendMessage(sb.ToString()); + var item = items["list"][0]; + sb.AppendLine($"`Term:` {item["word"].ToString()}"); + sb.AppendLine($"`Definition:` {item["definition"].ToString()}"); + sb.Append($"`Link:` <{item["permalink"].ToString()}>"); + await e.Channel.SendMessage(sb.ToString()).ConfigureAwait(false); } catch { diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index b706b95d..829fc263 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -117,7 +117,7 @@ namespace NadekoBot Client = new DiscordClient(new DiscordConfigBuilder() { MessageCacheSize = 10, - ConnectionTimeout = 180000, + ConnectionTimeout = 200000, LogLevel = LogSeverity.Warning, LogHandler = (s, e) => Console.WriteLine($"Severity: {e.Severity}" + @@ -197,7 +197,7 @@ namespace NadekoBot return; } #if NADEKO_RELEASE - await Task.Delay(150000).ConfigureAwait(false); + await Task.Delay(180000).ConfigureAwait(false); #else await Task.Delay(1000).ConfigureAwait(false); #endif @@ -228,6 +228,12 @@ namespace NadekoBot if (string.IsNullOrWhiteSpace(request.Content)) e.Cancel = true; }; +#if NADEKO_RELEASE + Client.ClientAPI.SentRequest += (s, e) => + { + Console.WriteLine($"[Request of type {e.Request.GetType()} sent in {e.Milliseconds}]"); + }; +#endif PermissionsHandler.Initialize(); NadekoBot.Ready = true; NadekoBot.OnReady(); diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 341f1850..223f778f 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -13,6 +13,9 @@ 512 true false + + + C:\Users\Master\Desktop\NadekoBot\ true Disk @@ -30,9 +33,6 @@ false true true - - - AnyCPU @@ -161,6 +161,9 @@ False lib\ScaredFingers.UnitsConversion.dll + + Classes\lib\sqlite3.dll + @@ -299,8 +302,12 @@ - - + + Designer + + + Designer + @@ -553,9 +560,7 @@ This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - -