some improvements, cleanup trivias and typings when blacklisting a server
This commit is contained in:
		| @@ -6,6 +6,7 @@ using System.Linq; | |||||||
| using NadekoBot.Extensions; | using NadekoBot.Extensions; | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
| using System.Reflection; | using System.Reflection; | ||||||
|  | using System.Timers; | ||||||
| using NadekoBot.Modules; | using NadekoBot.Modules; | ||||||
|  |  | ||||||
| namespace NadekoBot { | namespace NadekoBot { | ||||||
| @@ -22,6 +23,8 @@ namespace NadekoBot { | |||||||
|         public int TextChannelsCount { get; private set; } = 0; |         public int TextChannelsCount { get; private set; } = 0; | ||||||
|         public int VoiceChannelsCount { get; private set; } = 0; |         public int VoiceChannelsCount { get; private set; } = 0; | ||||||
|  |  | ||||||
|  |         private readonly Timer commandLogTimer = new Timer() {Interval = 10000}; | ||||||
|  |  | ||||||
|         static NadekoStats() { } |         static NadekoStats() { } | ||||||
|  |  | ||||||
|         private NadekoStats() { |         private NadekoStats() { | ||||||
| @@ -31,7 +34,8 @@ namespace NadekoBot { | |||||||
|             commandService.CommandExecuted += StatsCollector_RanCommand; |             commandService.CommandExecuted += StatsCollector_RanCommand; | ||||||
|  |  | ||||||
|             Task.Run(StartCollecting); |             Task.Run(StartCollecting); | ||||||
|             Console.WriteLine("Logging enabled."); |  | ||||||
|  |             commandLogTimer.Start(); | ||||||
|  |  | ||||||
|             ServerCount = NadekoBot.Client.Servers.Count(); |             ServerCount = NadekoBot.Client.Servers.Count(); | ||||||
|             var channels = NadekoBot.Client.Servers.SelectMany(s => s.AllChannels); |             var channels = NadekoBot.Client.Servers.SelectMany(s => s.AllChannels); | ||||||
| @@ -139,6 +143,7 @@ namespace NadekoBot { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         private async void StatsCollector_RanCommand(object sender, CommandEventArgs e) { |         private async void StatsCollector_RanCommand(object sender, CommandEventArgs e) { | ||||||
|  |             Console.WriteLine($">>Command {e.Command.Text}"); | ||||||
|             await Task.Run(() => { |             await Task.Run(() => { | ||||||
|                 try { |                 try { | ||||||
|                     commandsRan++; |                     commandsRan++; | ||||||
|   | |||||||
| @@ -83,7 +83,7 @@ namespace NadekoBot.Classes.Trivia { | |||||||
|             ShouldStopGame = true; |             ShouldStopGame = true; | ||||||
|             await channel.SendMessage("**Trivia game ended**\n" + GetLeaderboard()); |             await channel.SendMessage("**Trivia game ended**\n" + GetLeaderboard()); | ||||||
|             TriviaGame throwAwayValue; |             TriviaGame throwAwayValue; | ||||||
|             Commands.Trivia.runningTrivias.TryRemove(server, out throwAwayValue); |             Commands.Trivia.RunningTrivias.TryRemove(server.Id, out throwAwayValue); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         public async Task StopGame() { |         public async Task StopGame() { | ||||||
|   | |||||||
| @@ -54,10 +54,10 @@ namespace NadekoBot.Commands { | |||||||
|                         } |                         } | ||||||
|  |  | ||||||
|                         var bitmap = dices.Merge(); |                         var bitmap = dices.Merge(); | ||||||
|                         await e.Channel.SendMessage(values.Count + " Dies rolled. Total: **" + values.Sum() + "** Average: **" + (values.Sum() / (1.0f * values.Count)).ToString("N2") + "**"); |                         await e.Channel.SendMessage(values.Count + " Dice rolled. Total: **" + values.Sum() + "** Average: **" + (values.Sum() / (1.0f * values.Count)).ToString("N2") + "**"); | ||||||
|                         await e.Channel.SendFile("dices.png", bitmap.ToStream(ImageFormat.Png)); |                         await e.Channel.SendFile("dice.png", bitmap.ToStream(ImageFormat.Png)); | ||||||
|                     } catch  { |                     } catch { | ||||||
|                         await e.Channel.SendMessage("Please enter a number of dices to roll."); |                         await e.Channel.SendMessage("Please enter a number of dice to roll."); | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|             }; |             }; | ||||||
| @@ -87,9 +87,9 @@ namespace NadekoBot.Commands { | |||||||
|                                                  .ToArray(); |                                                  .ToArray(); | ||||||
|                         if (arr[0] > arr[1]) |                         if (arr[0] > arr[1]) | ||||||
|                             throw new ArgumentException("First argument should be bigger than the second one."); |                             throw new ArgumentException("First argument should be bigger than the second one."); | ||||||
|                         rolled = new Random().Next(arr[0],arr[1]+1); |                         rolled = new Random().Next(arr[0], arr[1] + 1); | ||||||
|                     } else { |                     } else { | ||||||
|                         rolled = new Random().Next(0, int.Parse(e.GetArg("range"))+1); |                         rolled = new Random().Next(0, int.Parse(e.GetArg("range")) + 1); | ||||||
|                     } |                     } | ||||||
|  |  | ||||||
|                     await e.Channel.SendMessage($"{e.User.Mention} rolled **{rolled}**."); |                     await e.Channel.SendMessage($"{e.User.Mention} rolled **{rolled}**."); | ||||||
|   | |||||||
| @@ -30,7 +30,7 @@ namespace NadekoBot.Commands { | |||||||
|                     } |                     } | ||||||
|                 }, |                 }, | ||||||
|                 {"%queued%", () => Music.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count).ToString()}, |                 {"%queued%", () => Music.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count).ToString()}, | ||||||
|                 {"%trivia%", () => Trivia.runningTrivias.Count.ToString()} |                 {"%trivia%", () => Trivia.RunningTrivias.Count.ToString()} | ||||||
|             }; |             }; | ||||||
|  |  | ||||||
|         private readonly object playingPlaceholderLock = new object(); |         private readonly object playingPlaceholderLock = new object(); | ||||||
|   | |||||||
| @@ -1,4 +1,5 @@ | |||||||
| using System; | using System; | ||||||
|  | using System.Collections.Concurrent; | ||||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||||
| using System.Linq; | using System.Linq; | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
| @@ -96,7 +97,6 @@ namespace NadekoBot.Commands { | |||||||
|                     if (finishedUserIds.Count % 2 == 0) { |                     if (finishedUserIds.Count % 2 == 0) { | ||||||
|                         await e.Channel.SendMessage($":exclamation: `A lot of people finished, here is the text for those still typing:`\n\n:book:**{CurrentSentence}**:book:"); |                         await e.Channel.SendMessage($":exclamation: `A lot of people finished, here is the text for those still typing:`\n\n:book:**{CurrentSentence}**:book:"); | ||||||
|                     } |                     } | ||||||
|  |  | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             catch { } |             catch { } | ||||||
| @@ -108,32 +108,33 @@ namespace NadekoBot.Commands { | |||||||
|  |  | ||||||
|     internal class SpeedTyping : IDiscordCommand { |     internal class SpeedTyping : IDiscordCommand { | ||||||
|  |  | ||||||
|         private static Dictionary<ulong, TypingGame> runningContests; |         public static ConcurrentDictionary<ulong, TypingGame> RunningContests; | ||||||
|  |  | ||||||
|         public SpeedTyping()  { |         public SpeedTyping()  { | ||||||
|             runningContests = new Dictionary<ulong, TypingGame>(); |             RunningContests = new ConcurrentDictionary<ulong, TypingGame>(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         public Func<CommandEventArgs, Task> DoFunc() => |         public Func<CommandEventArgs, Task> DoFunc() => | ||||||
|             async e => { |             async e => { | ||||||
|                 if (runningContests.ContainsKey(e.User.Server.Id) && runningContests[e.User.Server.Id].IsActive) { |                 if (RunningContests.ContainsKey(e.User.Server.Id) && RunningContests[e.User.Server.Id].IsActive) { | ||||||
|                     await e.Channel.SendMessage($"Contest already running in { runningContests[e.User.Server.Id].Channell.Mention } channel."); |                     await e.Channel.SendMessage($"Contest already running in { RunningContests[e.User.Server.Id].Channell.Mention } channel."); | ||||||
|                     return; |                     return; | ||||||
|                 } |                 } | ||||||
|                 if (runningContests.ContainsKey(e.User.Server.Id) && !runningContests[e.User.Server.Id].IsActive) { |                 if (RunningContests.ContainsKey(e.User.Server.Id) && !RunningContests[e.User.Server.Id].IsActive) { | ||||||
|                     await runningContests[e.User.Server.Id].Start(); |                     await RunningContests[e.User.Server.Id].Start(); | ||||||
|                     return; |                     return; | ||||||
|                 } |                 } | ||||||
|                 var tg = new TypingGame(e.Channel); |                 var tg = new TypingGame(e.Channel); | ||||||
|                 runningContests.Add(e.Server.Id, tg); |                 RunningContests.TryAdd(e.Server.Id, tg); | ||||||
|                 await tg.Start(); |                 await tg.Start(); | ||||||
|             }; |             }; | ||||||
|  |  | ||||||
|         private Func<CommandEventArgs, Task> QuitFunc() => |         private Func<CommandEventArgs, Task> QuitFunc() => | ||||||
|             async e => { |             async e => { | ||||||
|                 if (runningContests.ContainsKey(e.User.Server.Id) && |                 if (RunningContests.ContainsKey(e.User.Server.Id) && | ||||||
|                     await runningContests[e.User.Server.Id].Stop()) { |                     await RunningContests[e.User.Server.Id].Stop()) { | ||||||
|                     runningContests.Remove(e.User.Server.Id); |                     TypingGame throwaway; | ||||||
|  |                     RunningContests.TryRemove(e.User.Server.Id, out throwaway); | ||||||
|                     return; |                     return; | ||||||
|                 } |                 } | ||||||
|                 await e.Channel.SendMessage("No contest to stop on this channel."); |                 await e.Channel.SendMessage("No contest to stop on this channel."); | ||||||
|   | |||||||
| @@ -7,13 +7,13 @@ using TriviaGame = NadekoBot.Classes.Trivia.TriviaGame; | |||||||
|  |  | ||||||
| namespace NadekoBot.Commands { | namespace NadekoBot.Commands { | ||||||
|     internal class Trivia : IDiscordCommand { |     internal class Trivia : IDiscordCommand { | ||||||
|         public static ConcurrentDictionary<Server, TriviaGame> runningTrivias = new ConcurrentDictionary<Server, TriviaGame>(); |         public static ConcurrentDictionary<ulong, TriviaGame> RunningTrivias = new ConcurrentDictionary<ulong, TriviaGame>(); | ||||||
|  |  | ||||||
|         public Func<CommandEventArgs, Task> DoFunc() => async e => { |         public Func<CommandEventArgs, Task> DoFunc() => async e => { | ||||||
|             TriviaGame trivia; |             TriviaGame trivia; | ||||||
|             if (!runningTrivias.TryGetValue(e.Server, out trivia)) { |             if (!RunningTrivias.TryGetValue(e.Server.Id, out trivia)) { | ||||||
|                 var triviaGame = new TriviaGame(e); |                 var triviaGame = new TriviaGame(e); | ||||||
|                 if (runningTrivias.TryAdd(e.Server, triviaGame)) |                 if (RunningTrivias.TryAdd(e.Server.Id, triviaGame)) | ||||||
|                     await e.Channel.SendMessage("**Trivia game started!**\nFirst player to get to 10 points wins! You have 30 seconds per question.\nUse command `tq` if game was started by accident.**"); |                     await e.Channel.SendMessage("**Trivia game started!**\nFirst player to get to 10 points wins! You have 30 seconds per question.\nUse command `tq` if game was started by accident.**"); | ||||||
|                 else |                 else | ||||||
|                     await triviaGame.StopGame(); |                     await triviaGame.StopGame(); | ||||||
| @@ -34,7 +34,7 @@ namespace NadekoBot.Commands { | |||||||
|                 .Alias("-tlb") |                 .Alias("-tlb") | ||||||
|                 .Do(async e=> { |                 .Do(async e=> { | ||||||
|                     TriviaGame trivia; |                     TriviaGame trivia; | ||||||
|                     if (runningTrivias.TryGetValue(e.Server, out trivia)) |                     if (RunningTrivias.TryGetValue(e.Server.Id, out trivia)) | ||||||
|                         await e.Channel.SendMessage(trivia.GetLeaderboard()); |                         await e.Channel.SendMessage(trivia.GetLeaderboard()); | ||||||
|                     else |                     else | ||||||
|                         await e.Channel.SendMessage("No trivia is running on this server."); |                         await e.Channel.SendMessage("No trivia is running on this server."); | ||||||
| @@ -45,7 +45,7 @@ namespace NadekoBot.Commands { | |||||||
|                 .Alias("-tq") |                 .Alias("-tq") | ||||||
|                 .Do(async e=> { |                 .Do(async e=> { | ||||||
|                     TriviaGame trivia; |                     TriviaGame trivia; | ||||||
|                     if (runningTrivias.TryGetValue(e.Server, out trivia)) { |                     if (RunningTrivias.TryGetValue(e.Server.Id, out trivia)) { | ||||||
|                         await trivia.StopGame(); |                         await trivia.StopGame(); | ||||||
|                     } else |                     } else | ||||||
|                         await e.Channel.SendMessage("No trivia is running on this server."); |                         await e.Channel.SendMessage("No trivia is running on this server."); | ||||||
|   | |||||||
| @@ -480,6 +480,11 @@ namespace NadekoBot.Modules { | |||||||
|                             } |                             } | ||||||
|                             NadekoBot.Config.ServerBlacklist.Add(server.Id); |                             NadekoBot.Config.ServerBlacklist.Add(server.Id); | ||||||
|                             NadekoBot.SaveConfig(); |                             NadekoBot.SaveConfig(); | ||||||
|  |                             //cleanup trivias and typeracing | ||||||
|  |                             Classes.Trivia.TriviaGame trivia; | ||||||
|  |                             Commands.Trivia.RunningTrivias.TryRemove(server.Id, out trivia); | ||||||
|  |                             Commands.TypingGame typeracer; | ||||||
|  |                             Commands.SpeedTyping.RunningContests.TryRemove(server.Id, out typeracer); | ||||||
|                             await e.Channel.SendMessage($"`Sucessfully blacklisted server {server.Name}`"); |                             await e.Channel.SendMessage($"`Sucessfully blacklisted server {server.Name}`"); | ||||||
|                         }); |                         }); | ||||||
|                     }); |                     }); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user