Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #104 from SO-Close-Vote-Reviewers/C#6
Browse files Browse the repository at this point in the history
C# 6 features/clean up
  • Loading branch information
ArcticEcho committed Nov 24, 2015
2 parents 61fc824 + 48fc6ea commit f312be9
Show file tree
Hide file tree
Showing 44 changed files with 469 additions and 936 deletions.
11 changes: 4 additions & 7 deletions CVChatbot/CVChatbot.Bot/ChatMessageProcessor.cs
Expand Up @@ -90,7 +90,7 @@ public void ProcessChatMessage(Message incommingChatMessage, Room chatRoom)
/// <returns></returns>
private bool DoesUserHavePermissionToRunAction(ChatbotAction actionToRun, int chatUserId)
{
var neededPermissionLevel = actionToRun.GetPermissionLevel();
var neededPermissionLevel = actionToRun.PermissionLevel;

// If the permission level of the action is "everyone" then just return true.
// Don't need to do anything else, like searching though the database.
Expand Down Expand Up @@ -146,7 +146,7 @@ private void RunChatbotAction(ChatbotAction action, Message incommingChatMessage
{
// Record as started.
var id = RunningChatbotActionsManager.MarkChatbotActionAsStarted(
action.GetActionName(),
action.ActionName,
incommingChatMessage.Author.Name,
incommingChatMessage.Author.ID);

Expand Down Expand Up @@ -178,10 +178,8 @@ private void RunChatbotAction(ChatbotAction action, Message incommingChatMessage
/// a table of commands that the chatbot did not recognize.
/// </summary>
/// <param name="command"></param>
private void SendUnrecognizedCommandToDatabase(string command)
{
private void SendUnrecognizedCommandToDatabase(string command) =>
da.InsertUnrecognizedCommand(command);
}

/// <summary>
/// Call this method if you get an error while running a ChatbotAction.
Expand All @@ -192,8 +190,7 @@ private void SendUnrecognizedCommandToDatabase(string command)
/// <param name="actionToRun"></param>
private void TellChatAboutErrorWhileRunningAction(Exception ex, Room chatRoom, ChatbotAction actionToRun)
{
var headerLine = "I hit an error while trying to run '{0}':"
.FormatSafely(actionToRun.GetActionName());
var headerLine = $"I hit an error while trying to run '{actionToRun.ActionName}':";

var errorMessage = " " + ex.FullErrorMessage(Environment.NewLine + " ");

Expand Down
39 changes: 11 additions & 28 deletions CVChatbot/CVChatbot.Bot/ChatbotActions/ChatbotAction.cs
@@ -1,10 +1,5 @@
using ChatExchangeDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CVChatbot.Bot.ChatbotActions
{
Expand All @@ -13,6 +8,8 @@ namespace CVChatbot.Bot.ChatbotActions
/// </summary>
public abstract class ChatbotAction
{
public static RegexOptions RegexObjOptions => RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase;

/// <summary>
/// Determines if the incoming chat message activates this action.
/// </summary>
Expand All @@ -22,16 +19,13 @@ public abstract class ChatbotAction
public bool DoesChatMessageActiveAction(Message incomingMessage, bool isMessageAReplyToChatbot)
{
// First, check if the message is a reply or not and if the Action accepts that.
var requiredIsReplyValue = GetMessageIsReplyToChatbotRequiredValue();

if (isMessageAReplyToChatbot != requiredIsReplyValue)
if (isMessageAReplyToChatbot != ReplyMessagesOnly)
return false;

// Now regex test it.
var formattedContents = GetMessageContentsReadyForRegexParsing(incomingMessage);
var regex = GetRegexMatchingObject();

return regex.IsMatch(formattedContents);
return RegexMatchingObject.IsMatch(formattedContents);
}

/// <summary>
Expand All @@ -40,25 +34,14 @@ public bool DoesChatMessageActiveAction(Message incomingMessage, bool isMessageA
/// action to be activated. For example, User Commands MUST be a reply to the chatbot.
/// </summary>
/// <returns></returns>
protected abstract bool GetMessageIsReplyToChatbotRequiredValue();
protected abstract bool ReplyMessagesOnly { get; }

/// <summary>
/// Returns the regex matching pattern for this action. This is used to determine if the
/// Returns the regex matching object for this action. This is used to determine if the
/// chat message is appropriate for this action.
/// </summary>
/// <returns></returns>
protected abstract string GetRegexMatchingPattern();

/// <summary>
/// This is already populated with the necessary matching pattern text.
/// You may call this method from the RunAction() method if you need arguments within chat message.
/// </summary>
/// <returns>The regex object needed for pattern matching with the incoming chat message.</returns>
protected Regex GetRegexMatchingObject()
{
var pattern = GetRegexMatchingPattern();
return new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
protected abstract Regex RegexMatchingObject { get; }

/// <summary>
/// Formats the incoming message's contents so that it can be regex matched more reliably.
Expand All @@ -78,25 +61,25 @@ protected Regex GetRegexMatchingObject()
/// Returns the human-friendly name of the chatbot action.
/// </summary>
/// <returns></returns>
public abstract string GetActionName();
public abstract string ActionName { get; }

/// <summary>
/// Returns a short description of what the action does. Triggers return null.
/// </summary>
/// <returns></returns>
public abstract string GetActionDescription();
public abstract string ActionDescription { get; }

/// <summary>
/// Returns the usage of the action, including optional arguments. Triggers return null.
/// </summary>
/// <returns></returns>
public abstract string GetActionUsage();
public abstract string ActionUsage { get; }

/// <summary>
/// Returns the minimum permission level needed by a user to run this action.
/// </summary>
/// <returns></returns>
public abstract ActionPermissionLevel GetPermissionLevel();
public abstract ActionPermissionLevel PermissionLevel { get; }
}

/// <summary>
Expand Down
Expand Up @@ -2,8 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CVChatbot.Bot.ChatbotActions
{
Expand Down Expand Up @@ -37,7 +35,7 @@ public static string GetChatBotActionUsage<TAction>()
{
return AllChatActions
.Single(x => x is TAction)
.GetActionUsage();
.ActionUsage;
}

private static class ReflectiveEnumerator
Expand Down
46 changes: 16 additions & 30 deletions CVChatbot/CVChatbot.Bot/ChatbotActions/Commands/Alive.cs
@@ -1,14 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using TCL.Extensions;

namespace CVChatbot.Bot.ChatbotActions.Commands
{
public class Alive : UserCommand
{
private Regex ptn = new Regex(@"^(?:(?:are )?you )?(alive|still there|(still )?with us)\??$", RegexObjOptions);

public override string ActionDescription => "A simple ping command to test if the bot is running.";

public override string ActionName => "Alive";

public override string ActionUsage => "alive";

public override ActionPermissionLevel PermissionLevel => ActionPermissionLevel.Everyone;

protected override Regex RegexMatchingObject => ptn;



public override void RunAction(ChatExchangeDotNet.Message incommingChatMessage, ChatExchangeDotNet.Room chatRoom, InstallationSettings roomSettings)
{
var responsePhrases = new List<string>()
Expand All @@ -26,30 +37,5 @@ public override void RunAction(ChatExchangeDotNet.Message incommingChatMessage,

chatRoom.PostReplyOrThrow(incommingChatMessage, phrase);
}

public override ActionPermissionLevel GetPermissionLevel()
{
return ActionPermissionLevel.Everyone;
}

protected override string GetRegexMatchingPattern()
{
return @"^(?:(?:are )?you )?(alive|still there|(still )?with us)\??$";
}

public override string GetActionName()
{
return "Alive";
}

public override string GetActionDescription()
{
return "A simple ping command to test if the bot is running.";
}

public override string GetActionUsage()
{
return "alive";
}
}
}
50 changes: 17 additions & 33 deletions CVChatbot/CVChatbot.Bot/ChatbotActions/Commands/AuditStats.cs
@@ -1,17 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TCL.Extensions;
using System.Text.RegularExpressions;
using System.Threading;
using CVChatbot.Bot.Database;

namespace CVChatbot.Bot.ChatbotActions.Commands
{
public class AuditStats : UserCommand
{
private readonly Regex ptn = new Regex("^(show (me )?|display )?(my )?(audit stats|stats (of|about) my audits)( pl(ease|[sz]))?$", RegexObjOptions);

public override string ActionDescription => "Shows stats about your recorded audits.";

public override string ActionName => "Audit Stats";

public override string ActionUsage => "audit stats";

public override ActionPermissionLevel PermissionLevel => ActionPermissionLevel.Registered;

protected override Regex RegexMatchingObject => ptn;



public override void RunAction(ChatExchangeDotNet.Message incommingChatMessage, ChatExchangeDotNet.Room chatRoom, InstallationSettings roomSettings)
{
var da = new DatabaseAccessor(roomSettings.DatabaseConnectionString);
Expand All @@ -26,37 +35,12 @@ public override void RunAction(ChatExchangeDotNet.Message incommingChatMessage,

var message = auditEntries
.ToStringTable(new[] { "Tag Name", "%", "Count" },
(x) => x.TagName,
(x) => Math.Round(x.Percent, 2),
(x) => x.Count);
x => x.TagName,
x => Math.Round(x.Percent, 2),
x => x.Count);

chatRoom.PostReplyOrThrow(incommingChatMessage, "Stats of all tracked audits by tag:");
chatRoom.PostMessageOrThrow(message);
}

public override ActionPermissionLevel GetPermissionLevel()
{
return ActionPermissionLevel.Registered;
}

protected override string GetRegexMatchingPattern()
{
return @"^(show (me )?|display )?(my )?(audit stats|stats (of|about) my audits)( pl(ease|[sz]))?$";
}

public override string GetActionName()
{
return "Audit Stats";
}

public override string GetActionDescription()
{
return "Shows stats about your recorded audits.";
}

public override string GetActionUsage()
{
return "audit stats";
}
}
}
55 changes: 20 additions & 35 deletions CVChatbot/CVChatbot.Bot/ChatbotActions/Commands/Commands.cs
@@ -1,10 +1,8 @@
using ChatExchangeDotNet;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using TCL.Extensions;

namespace CVChatbot.Bot.ChatbotActions.Commands
Expand All @@ -14,6 +12,20 @@ namespace CVChatbot.Bot.ChatbotActions.Commands
/// </summary>
public class Commands : UserCommand
{
private Regex ptn = new Regex("^(show the )?(list of )?(user )?command(s| list)( pl(ease|[sz]))?$", RegexObjOptions);

public override string ActionDescription => "Shows this list.";

public override string ActionName => "Commands";

public override string ActionUsage => "commands";

public override ActionPermissionLevel PermissionLevel => ActionPermissionLevel.Everyone;

protected override Regex RegexMatchingObject => ptn;



private static class ReflectiveEnumerator
{
public static IEnumerable<T> GetEnumerableOfType<T>(params object[] constructorArgs) where T : class
Expand All @@ -29,25 +41,23 @@ private static class ReflectiveEnumerator
}
}



public override void RunAction(ChatExchangeDotNet.Message incommingChatMessage, ChatExchangeDotNet.Room chatRoom, InstallationSettings roomSettings)
{
var groupedCommands = ChatbotActionRegister.AllChatActions
.Where(x => x is UserCommand)
.GroupBy(x => x.GetPermissionLevel());
.GroupBy(x => x.PermissionLevel);

var finalMessageLines = new List<string>();
finalMessageLines.Add("Below is a list of commands for the Close Vote Chat Bot");
finalMessageLines.Add("");

foreach (var group in groupedCommands)
{
finalMessageLines.Add("{0}".FormatInline(group.Key.ToString()));
finalMessageLines.Add(group.Key.ToString());

var groupCommandLines = group
.OrderBy(x => x.GetActionName())
.Select(x => " {0} - {1}".FormatInline(x.GetActionUsage(), x.GetActionDescription()));
.OrderBy(x => x.ActionName)
.Select(x => $" {x.ActionUsage} - {x.ActionDescription}");

finalMessageLines.AddRange(groupCommandLines);
finalMessageLines.Add("");
Expand All @@ -59,30 +69,5 @@ public override void RunAction(ChatExchangeDotNet.Message incommingChatMessage,

chatRoom.PostMessageOrThrow(finalMessage);
}

public override ActionPermissionLevel GetPermissionLevel()
{
return ActionPermissionLevel.Everyone;
}

protected override string GetRegexMatchingPattern()
{
return "^(show the )?(list of )?(user )?command(s| list)( pl(ease|[sz]))?$";
}

public override string GetActionName()
{
return "Commands";
}

public override string GetActionDescription()
{
return "Shows this list.";
}

public override string GetActionUsage()
{
return "commands";
}
}
}

0 comments on commit f312be9

Please sign in to comment.