From 18b3436d66aff23814b16f39c3efa39ef42140d3 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 28 Jul 2018 16:31:18 +0200 Subject: [PATCH] add more awaits and update !roll --- Geekbot.net.sln.DotSettings | 11 ++++ Geekbot.net/Commands/Audio/Voice.cs | 2 +- Geekbot.net/Commands/Games/Roll.cs | 17 ++++--- Geekbot.net/Commands/Integrations/Mal.cs | 2 +- Geekbot.net/Commands/Randomness/Slap.cs | 2 +- Geekbot.net/Commands/User/Karma.cs | 4 +- Geekbot.net/Commands/User/Stats.cs | 2 +- Geekbot.net/Geekbot.net.csproj | 2 +- Geekbot.net/Handlers.cs | 51 +++++++++---------- Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs | 8 +-- Geekbot.net/Program.cs | 4 +- Tests/Tests.csproj | 2 +- 12 files changed, 60 insertions(+), 47 deletions(-) create mode 100644 Geekbot.net.sln.DotSettings diff --git a/Geekbot.net.sln.DotSettings b/Geekbot.net.sln.DotSettings new file mode 100644 index 0000000..7fa04b3 --- /dev/null +++ b/Geekbot.net.sln.DotSettings @@ -0,0 +1,11 @@ + + NEVER + 200 + True + True + True + True + True + True + True + True \ No newline at end of file diff --git a/Geekbot.net/Commands/Audio/Voice.cs b/Geekbot.net/Commands/Audio/Voice.cs index aa406cc..b12d6e2 100644 --- a/Geekbot.net/Commands/Audio/Voice.cs +++ b/Geekbot.net/Commands/Audio/Voice.cs @@ -37,7 +37,7 @@ namespace Geekbot.net.Commands.Audio } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/Games/Roll.cs b/Geekbot.net/Commands/Games/Roll.cs index e8d2d56..c629ed8 100644 --- a/Geekbot.net/Commands/Games/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll.cs @@ -33,15 +33,18 @@ namespace Geekbot.net.Commands.Games var transDict = await _translation.GetDict(Context); if (guess <= 100 && guess > 0) { - var prevRoll = _redis.Db.HashGet($"{Context.Guild.Id}:RollsPrevious", Context.Message.Author.Id); - if (!prevRoll.IsNullOrEmpty && prevRoll.ToString() == guess.ToString()) + var prevRoll = _redis.Db.HashGet($"{Context.Guild.Id}:RollsPrevious2", Context.Message.Author.Id).ToString()?.Split('|'); + if (prevRoll?.Length == 2) { - await ReplyAsync(string.Format(transDict["NoPrevGuess"], Context.Message.Author.Mention)); - return; + if (prevRoll[0] == guess.ToString() && DateTime.Parse(prevRoll[1]) > DateTime.Now.AddDays(-1)) + { + await ReplyAsync(string.Format(transDict["NoPrevGuess"], Context.Message.Author.Mention)); + return; + } } - _redis.Db.HashSet($"{Context.Guild.Id}:RollsPrevious", - new[] {new HashEntry(Context.Message.Author.Id, guess)}); + _redis.Db.HashSet($"{Context.Guild.Id}:RollsPrevious2", new[] {new HashEntry(Context.Message.Author.Id, $"{guess}|{DateTime.Now}")}); + await ReplyAsync(string.Format(transDict["Rolled"], Context.Message.Author.Mention, number, guess)); if (guess == number) { @@ -56,7 +59,7 @@ namespace Geekbot.net.Commands.Games } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Integrations/Mal.cs b/Geekbot.net/Commands/Integrations/Mal.cs index 7c79861..88f500e 100644 --- a/Geekbot.net/Commands/Integrations/Mal.cs +++ b/Geekbot.net/Commands/Integrations/Mal.cs @@ -112,7 +112,7 @@ namespace Geekbot.net.Commands.Integrations } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Randomness/Slap.cs b/Geekbot.net/Commands/Randomness/Slap.cs index 412b20c..13cd22a 100644 --- a/Geekbot.net/Commands/Randomness/Slap.cs +++ b/Geekbot.net/Commands/Randomness/Slap.cs @@ -87,7 +87,7 @@ namespace Geekbot.net.Commands.Randomness } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/User/Karma.cs b/Geekbot.net/Commands/User/Karma.cs index beb1334..39b05be 100644 --- a/Geekbot.net/Commands/User/Karma.cs +++ b/Geekbot.net/Commands/User/Karma.cs @@ -67,7 +67,7 @@ namespace Geekbot.net.Commands.User } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -114,7 +114,7 @@ namespace Geekbot.net.Commands.User } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index 990b229..4d4d800 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -80,7 +80,7 @@ namespace Geekbot.net.Commands.User } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 6e857bf..68a1870 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -38,7 +38,7 @@ - + diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 0331ca9..9811432 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -25,8 +25,8 @@ namespace Geekbot.net private readonly CommandService _commands; private readonly IUserRepository _userRepository; private readonly IReactionListener _reactionListener; - - public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, + + public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener) { @@ -39,11 +39,11 @@ namespace Geekbot.net _userRepository = userRepository; _reactionListener = reactionListener; } - + // // Incoming Messages // - + public Task RunCommand(SocketMessage messageParam) { try @@ -51,33 +51,31 @@ namespace Geekbot.net if (!(messageParam is SocketUserMessage message)) return Task.CompletedTask; if (message.Author.IsBot) return Task.CompletedTask; var argPos = 0; - + // ToDo: remove // if (!message.Author.Id.Equals(93061333972455424)) return Task.CompletedTask; - + var lowCaseMsg = message.ToString().ToLower(); if (lowCaseMsg.StartsWith("hui")) { - var hasPing = _database.GuildSettings.FirstOrDefault(guild => - guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong())) - ?.Hui ?? false; + var hasPing = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong()))?.Hui ?? false; if (hasPing) { message.Channel.SendMessageAsync("hui!!!"); return Task.CompletedTask; } } + if (lowCaseMsg.StartsWith("ping ") || lowCaseMsg.Equals("ping")) { - var hasPing = _database.GuildSettings.FirstOrDefault(guild => - guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong())) - ?.Ping ?? false; + var hasPing = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong()))?.Ping ?? false; if (hasPing) { message.Channel.SendMessageAsync("pong"); - return Task.CompletedTask; + return Task.CompletedTask; } } + if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return Task.CompletedTask; var context = new CommandContext(_client, message); @@ -104,8 +102,9 @@ namespace Geekbot.net _logger.Information(LogSource.Message, $"[DM-Channel] {message.Content}", SimpleConextConverter.ConvertSocketMessage(message)); return; } + var channel = (SocketGuildChannel) message.Channel; - + // await _database.Database.ExecuteSqlCommandAsync("UPDATE \"Messages\" " + // $"SET \"MessageCount\" = \"MessageCount\" + {1} " + // $"WHERE \"GuildId\" = '{channel.Guild.Id.AsLong()}' " + @@ -122,26 +121,25 @@ namespace Geekbot.net _logger.Error(LogSource.Message, "Could not process message stats", e); } } - + // // User Stuff // - + public async Task UserJoined(SocketGuildUser user) { try { if (!user.IsBot) { - var message = _database.GuildSettings.FirstOrDefault(guild => - guild.GuildId.Equals(user.Guild.Id.AsLong())) - ?.WelcomeMessage; + var message = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(user.Guild.Id.AsLong()))?.WelcomeMessage; if (!string.IsNullOrEmpty(message)) { message = message.Replace("$user", user.Mention); await user.Guild.DefaultChannel.SendMessageAsync(message); } } + await _userRepository.Update(user); _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); } @@ -172,13 +170,14 @@ namespace Geekbot.net { _logger.Error(LogSource.Geekbot, "Failed to send leave message", e); } + _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); } - + // // Message Stuff // - + public async Task MessageDeleted(Cacheable message, ISocketMessageChannel channel) { try @@ -192,14 +191,14 @@ namespace Geekbot.net var sb = new StringBuilder(); if (message.Value != null) { - sb.AppendLine( - $"The following message from {message.Value.Author.Username}#{message.Value.Author.Discriminator} was deleted in <#{channel.Id}>"); + sb.AppendLine($"The following message from {message.Value.Author.Username}#{message.Value.Author.Discriminator} was deleted in <#{channel.Id}>"); sb.AppendLine(message.Value.Content); } else { sb.AppendLine("Someone deleted a message, the message was not cached..."); } + await modChannelSocket.SendMessageAsync(sb.ToString()); } } @@ -208,11 +207,11 @@ namespace Geekbot.net _logger.Error(LogSource.Geekbot, "Failed to send delete message...", e); } } - + // // Reactions // - + public Task ReactionAdded(Cacheable cacheable, ISocketMessageChannel socketMessageChannel, SocketReaction reaction) { if (reaction.User.Value.IsBot) return Task.CompletedTask; @@ -229,4 +228,4 @@ namespace Geekbot.net return Task.CompletedTask; } } -} +} \ No newline at end of file diff --git a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs index 9556b84..e9ff92c 100644 --- a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs @@ -52,16 +52,16 @@ namespace Geekbot.net.Lib.ErrorHandling if (!string.IsNullOrEmpty(resStackTrace)) { var maxLen = Math.Min(resStackTrace.Length, 1850); - context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace.Substring(0, maxLen)}\r\n```"); + await context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace.Substring(0, maxLen)}\r\n```"); } else { - context.Channel.SendMessageAsync(e.Message); + await context.Channel.SendMessageAsync(e.Message); } } else { - context.Channel.SendMessageAsync(errorString); + await context.Channel.SendMessageAsync(errorString); } } @@ -82,7 +82,7 @@ namespace Geekbot.net.Lib.ErrorHandling } catch (Exception ex) { - context.Channel.SendMessageAsync("Something went really really wrong here"); + await context.Channel.SendMessageAsync("Something went really really wrong here"); _logger.Error(LogSource.Geekbot, "Errorception", ex); } } diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 86a3507..3be93c5 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -101,8 +101,8 @@ namespace Geekbot.net { Console.Write("Your bot Token: "); var newToken = Console.ReadLine(); - _globalSettings.SetKey("DiscordToken", newToken); - _globalSettings.SetKey("Game", "Ping Pong"); + await _globalSettings.SetKey("DiscordToken", newToken); + await _globalSettings.SetKey("Game", "Ping Pong"); _token = newToken; } diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 96c170b..58fbfc6 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -7,7 +7,7 @@ - +