protected virtual void HandleThankCommand(CommandMatch cmd, IChannelMessageEventArgs msg) { string thankerNick = msg.SenderNickname; bool forceThanks = cmd.Options.Any(f => f.Key == "--force"); string thankeeNick = ((string)cmd.Arguments[0]).TrimEnd(':', ',', ';'); string reason = ((string)cmd.Arguments[1]).Trim(); if (reason.Length == 0) // trimmed! { reason = null; } string thanker; string thankee; if (forceThanks) { thanker = thankerNick; thankee = thankeeNick; } else { thanker = ConnectionManager.RegisteredNameForNick(thankerNick); if (thanker == null) { ConnectionManager.SendChannelMessageFormat( msg.Channel, "{0}: You can't use this unless you're logged in with NickServ.", thankerNick ); return; } thankee = ConnectionManager.RegisteredNameForNick(thankeeNick); if (thankee == null) { ConnectionManager.SendChannelMessageFormat( msg.Channel, "{0}: Unfortunately, {1} doesn't seem to be logged in with NickServ.", thankerNick, thankeeNick ); return; } } var thankerLower = thanker.ToLowerInvariant(); var thankeeLower = thankee.ToLowerInvariant(); if (thankeeLower == thankerLower) { ConnectionManager.SendChannelMessageFormat( msg.Channel, "You are so full of yourself, {0}.", thankerNick ); return; } Logger.LogDebug("{Thanker} thanks {Thankee}", thanker, thankee); long thankedCount; using (var ctx = GetNewContext()) { var entry = new ThanksEntry { Channel = msg.Channel, ThankerLowercase = thankerLower, ThankeeLowercase = thankeeLower, Timestamp = DateTime.Now.ToUniversalTimeForDatabase(), Deleted = false, Reason = reason }; ctx.ThanksEntries.Add(entry); ctx.SaveChanges(); thankedCount = ctx.ThanksEntries.Count(te => te.ThankeeLowercase == thankeeLower && !te.Deleted); } ConnectionManager.SendChannelMessageFormat( msg.Channel, "{0}: Alright! By the way, {1} has been thanked {2} until now.", msg.SenderNickname, thankee, (thankedCount == 1) ? "once" : (thankedCount + " times") ); }
protected override void ProcessUpdatedMessage(ChatboxMessage message, bool isPartOfInitialSalvo = false, bool isEdited = false, bool isBanned = false) { if (isBanned || isEdited || isPartOfInitialSalvo) { return; } if (message.UserName == Connector.ForumConfig.Username) { return; } var body = message.BodyBBCode; var thanksMatch = ThankRegex.Match(body); if (thanksMatch.Success) { var nickname = thanksMatch.Groups[1].Value.Trim(); var lowerNickname = nickname.ToLowerInvariant(); if (lowerNickname == message.UserName.ToLowerInvariant()) { Connector.SendMessage(string.Format( "You are so full of yourself, [noparse]{0}[/noparse].", message.UserName )); return; } UserIDAndNickname?userInfo = null; try { userInfo = Connector.UserIDAndNicknameForUncasedName(nickname); } catch (TransferException) { // never mind } if (!userInfo.HasValue) { Connector.SendMessage(string.Format( "I don't know '[noparse]{0}[/noparse]'!", nickname )); return; } Logger.DebugFormat("{0} thanks {1}", message.UserName, nickname); long thankedCount = -1; using (var ctx = GetNewContext()) { var entry = ctx.ThanksEntries.Where(te => te.Thanker == message.UserName && te.ThankeeFolded == lowerNickname).FirstOrDefault(); if (entry == null) { entry = new ThanksEntry { Thanker = message.UserName, ThankeeFolded = lowerNickname, ThankCount = 1 }; ctx.ThanksEntries.Add(entry); } else { ++entry.ThankCount; } ctx.SaveChanges(); thankedCount = ctx.ThanksEntries.Where(te => te.ThankeeFolded == lowerNickname).Sum(te => te.ThankCount); } Connector.SendMessage(string.Format( "[noparse]{0}[/noparse]: Alright! By the way, [noparse]{1}[/noparse] has been thanked {2} until now.", message.UserName, userInfo.Value.Nickname, (thankedCount == 1) ? "once" : (thankedCount + " times") )); } else if (body.StartsWith("!thanked ")) { var nickname = body.Substring(("!thanked ").Length).Trim(); var lowerNickname = nickname.ToLowerInvariant(); UserIDAndNickname?userInfo = null; try { userInfo = Connector.UserIDAndNicknameForUncasedName(nickname); } catch (TransferException) { // never mind } if (!userInfo.HasValue) { Connector.SendMessage(string.Format( "I don't know '[noparse]{0}[/noparse]'!", nickname )); return; } long thankedCount = -1; using (var ctx = GetNewContext()) { thankedCount = ctx.ThanksEntries.Where(te => te.ThankeeFolded == lowerNickname).Sum(te => te.ThankCount); } string countPhrase = null; bool showStats = (thankedCount != 0); if (thankedCount == 0) { countPhrase = "not been thanked"; } else if (thankedCount == 1) { countPhrase = "been thanked once"; } else { countPhrase = string.Format("been thanked {0} times", thankedCount); } var statsString = ""; if (showStats) { List <string> mostGratefulStrings; using (var ctx = GetNewContext()) { mostGratefulStrings = ctx.ThanksEntries .Where(te => te.ThankeeFolded == lowerNickname) .OrderByDescending(te => te.ThankCount) .Take(_config.MostGratefulCount + 1) .ToList() .Select(te => string.Format("[noparse]{0}[/noparse]: {1}\u00D7", te.Thanker, te.ThankCount)) .ToList(); } // mention that the list is truncated if there are more than MostGratefulCount entries var countString = (mostGratefulStrings.Count <= _config.MostGratefulCount) ? "" : (" " + _config.MostGratefulCountText); statsString = string.Format( " (Most grateful{0}: {1})", countString, string.Join(", ", mostGratefulStrings.Take(_config.MostGratefulCount)) ); } Connector.SendMessage(string.Format( "[noparse]{0}: {1}[/noparse] has {2} until now.{3}", message.UserName, userInfo.Value.Nickname, countPhrase, statsString )); } else if (body == "!topthanked") { List <ThankeeAndCount> top; using (var ctx = GetNewContext()) { top = ctx.ThanksEntries .GroupBy(te => te.ThankeeFolded) .Select(teg => new ThankeeAndCount { ThankeeFolded = teg.FirstOrDefault().ThankeeFolded, ThankCount = teg.Sum(te => te.ThankCount) }) .OrderByDescending(teg => teg.ThankCount) .Take(_config.MostThankedCount) .ToList() ; } var topStrings = new List <string>(); foreach (var thankeeAndCount in top) { var actualUsername = thankeeAndCount.ThankeeFolded; try { var info = Connector.UserIDAndNicknameForUncasedName(thankeeAndCount.ThankeeFolded); if (info.HasValue) { actualUsername = info.Value.Nickname; } } catch (TransferException) { } topStrings.Add(string.Format("{0}: {1}", actualUsername, thankeeAndCount.ThankCount)); } Connector.SendMessage(string.Format( "[noparse]{0}[/noparse]: {1}", message.UserName, string.Join(", ", topStrings) )); } }