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 System.Threading.Tasks; | ||||
| using System.Reflection; | ||||
| using System.Timers; | ||||
| using NadekoBot.Modules; | ||||
|  | ||||
| namespace NadekoBot { | ||||
| @@ -22,6 +23,8 @@ namespace NadekoBot { | ||||
|         public int TextChannelsCount { get; private set; } = 0; | ||||
|         public int VoiceChannelsCount { get; private set; } = 0; | ||||
|  | ||||
|         private readonly Timer commandLogTimer = new Timer() {Interval = 10000}; | ||||
|  | ||||
|         static NadekoStats() { } | ||||
|  | ||||
|         private NadekoStats() { | ||||
| @@ -31,7 +34,8 @@ namespace NadekoBot { | ||||
|             commandService.CommandExecuted += StatsCollector_RanCommand; | ||||
|  | ||||
|             Task.Run(StartCollecting); | ||||
|             Console.WriteLine("Logging enabled."); | ||||
|  | ||||
|             commandLogTimer.Start(); | ||||
|  | ||||
|             ServerCount = NadekoBot.Client.Servers.Count(); | ||||
|             var channels = NadekoBot.Client.Servers.SelectMany(s => s.AllChannels); | ||||
| @@ -139,6 +143,7 @@ namespace NadekoBot { | ||||
|         } | ||||
|  | ||||
|         private async void StatsCollector_RanCommand(object sender, CommandEventArgs e) { | ||||
|             Console.WriteLine($">>Command {e.Command.Text}"); | ||||
|             await Task.Run(() => { | ||||
|                 try { | ||||
|                     commandsRan++; | ||||
|   | ||||
| @@ -83,7 +83,7 @@ namespace NadekoBot.Classes.Trivia { | ||||
|             ShouldStopGame = true; | ||||
|             await channel.SendMessage("**Trivia game ended**\n" + GetLeaderboard()); | ||||
|             TriviaGame throwAwayValue; | ||||
|             Commands.Trivia.runningTrivias.TryRemove(server, out throwAwayValue); | ||||
|             Commands.Trivia.RunningTrivias.TryRemove(server.Id, out throwAwayValue); | ||||
|         } | ||||
|  | ||||
|         public async Task StopGame() { | ||||
|   | ||||
| @@ -54,10 +54,10 @@ namespace NadekoBot.Commands { | ||||
|                         } | ||||
|  | ||||
|                         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.SendFile("dices.png", bitmap.ToStream(ImageFormat.Png)); | ||||
|                     } catch  { | ||||
|                         await e.Channel.SendMessage("Please enter a number of dices to roll."); | ||||
|                         await e.Channel.SendMessage(values.Count + " Dice rolled. Total: **" + values.Sum() + "** Average: **" + (values.Sum() / (1.0f * values.Count)).ToString("N2") + "**"); | ||||
|                         await e.Channel.SendFile("dice.png", bitmap.ToStream(ImageFormat.Png)); | ||||
|                     } catch { | ||||
|                         await e.Channel.SendMessage("Please enter a number of dice to roll."); | ||||
|                     } | ||||
|                 } | ||||
|             }; | ||||
| @@ -87,9 +87,9 @@ namespace NadekoBot.Commands { | ||||
|                                                  .ToArray(); | ||||
|                         if (arr[0] > arr[1]) | ||||
|                             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 { | ||||
|                         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}**."); | ||||
|   | ||||
| @@ -30,7 +30,7 @@ namespace NadekoBot.Commands { | ||||
|                     } | ||||
|                 }, | ||||
|                 {"%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(); | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| using System; | ||||
| using System.Collections.Concurrent; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Threading.Tasks; | ||||
| @@ -96,7 +97,6 @@ namespace NadekoBot.Commands { | ||||
|                     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:"); | ||||
|                     } | ||||
|  | ||||
|                 } | ||||
|             } | ||||
|             catch { } | ||||
| @@ -108,32 +108,33 @@ namespace NadekoBot.Commands { | ||||
|  | ||||
|     internal class SpeedTyping : IDiscordCommand { | ||||
|  | ||||
|         private static Dictionary<ulong, TypingGame> runningContests; | ||||
|         public static ConcurrentDictionary<ulong, TypingGame> RunningContests; | ||||
|  | ||||
|         public SpeedTyping()  { | ||||
|             runningContests = new Dictionary<ulong, TypingGame>(); | ||||
|             RunningContests = new ConcurrentDictionary<ulong, TypingGame>(); | ||||
|         } | ||||
|  | ||||
|         public Func<CommandEventArgs, Task> DoFunc() => | ||||
|             async e => { | ||||
|                 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."); | ||||
|                 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."); | ||||
|                     return; | ||||
|                 } | ||||
|                 if (runningContests.ContainsKey(e.User.Server.Id) && !runningContests[e.User.Server.Id].IsActive) { | ||||
|                     await runningContests[e.User.Server.Id].Start(); | ||||
|                 if (RunningContests.ContainsKey(e.User.Server.Id) && !RunningContests[e.User.Server.Id].IsActive) { | ||||
|                     await RunningContests[e.User.Server.Id].Start(); | ||||
|                     return; | ||||
|                 } | ||||
|                 var tg = new TypingGame(e.Channel); | ||||
|                 runningContests.Add(e.Server.Id, tg); | ||||
|                 RunningContests.TryAdd(e.Server.Id, tg); | ||||
|                 await tg.Start(); | ||||
|             }; | ||||
|  | ||||
|         private Func<CommandEventArgs, Task> QuitFunc() => | ||||
|             async e => { | ||||
|                 if (runningContests.ContainsKey(e.User.Server.Id) && | ||||
|                     await runningContests[e.User.Server.Id].Stop()) { | ||||
|                     runningContests.Remove(e.User.Server.Id); | ||||
|                 if (RunningContests.ContainsKey(e.User.Server.Id) && | ||||
|                     await RunningContests[e.User.Server.Id].Stop()) { | ||||
|                     TypingGame throwaway; | ||||
|                     RunningContests.TryRemove(e.User.Server.Id, out throwaway); | ||||
|                     return; | ||||
|                 } | ||||
|                 await e.Channel.SendMessage("No contest to stop on this channel."); | ||||
|   | ||||
| @@ -7,13 +7,13 @@ using TriviaGame = NadekoBot.Classes.Trivia.TriviaGame; | ||||
|  | ||||
| namespace NadekoBot.Commands { | ||||
|     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 => { | ||||
|             TriviaGame trivia; | ||||
|             if (!runningTrivias.TryGetValue(e.Server, out trivia)) { | ||||
|             if (!RunningTrivias.TryGetValue(e.Server.Id, out trivia)) { | ||||
|                 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.**"); | ||||
|                 else | ||||
|                     await triviaGame.StopGame(); | ||||
| @@ -34,7 +34,7 @@ namespace NadekoBot.Commands { | ||||
|                 .Alias("-tlb") | ||||
|                 .Do(async e=> { | ||||
|                     TriviaGame trivia; | ||||
|                     if (runningTrivias.TryGetValue(e.Server, out trivia)) | ||||
|                     if (RunningTrivias.TryGetValue(e.Server.Id, out trivia)) | ||||
|                         await e.Channel.SendMessage(trivia.GetLeaderboard()); | ||||
|                     else | ||||
|                         await e.Channel.SendMessage("No trivia is running on this server."); | ||||
| @@ -45,7 +45,7 @@ namespace NadekoBot.Commands { | ||||
|                 .Alias("-tq") | ||||
|                 .Do(async e=> { | ||||
|                     TriviaGame trivia; | ||||
|                     if (runningTrivias.TryGetValue(e.Server, out trivia)) { | ||||
|                     if (RunningTrivias.TryGetValue(e.Server.Id, out trivia)) { | ||||
|                         await trivia.StopGame(); | ||||
|                     } else | ||||
|                         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.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}`"); | ||||
|                         }); | ||||
|                     }); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user