/// <summary> /// Main entry point for the class. /// Takes a message revived from chat, determines what action should be taken, then performs that action. /// </summary> /// <param name="incommingChatMessage">The chat message that was said.</param> /// <param name="chatRoom">The room the chat message was said in.</param> public void ProcessChatMessage(Message incommingChatMessage, Room chatRoom) { // Do this first so I only have to find the result once per chat message. bool isReplyToChatbot = MessageIsReplyToChatbot(incommingChatMessage, chatRoom); // Determine the list of possible actions that work from the message. var possibleChatbotActionsToRun = ChatbotActionRegister.AllChatActions .Where(x => x.DoesChatMessageActiveAction(incommingChatMessage, isReplyToChatbot)) .ToList(); if (possibleChatbotActionsToRun.Count > 1) throw new Exception("More than one possible chat bot action to run for the input '{0}'" .FormatSafely(incommingChatMessage.Content)); if (!possibleChatbotActionsToRun.Any()) { // Didn't find an action to run, what to do next depends of if the message was // a reply to the chatbot or not. if (isReplyToChatbot) { // User was trying to make a command. SendUnrecognizedCommandToDatabase(incommingChatMessage.GetContentsWithStrippedMentions()); chatRoom.PostReplyOrThrow(incommingChatMessage, "Sorry, I don't understand that. Use `{0}` for a list of commands." .FormatInline(ChatbotActionRegister.GetChatBotActionUsage<Commands>())); } // Else it's a trigger, do nothing. return; } // You have a single item to run. var chatbotActionToRun = possibleChatbotActionsToRun.Single(); // Now, do you have permission to run it? If you are a mod the answer is yes, else you need to check. if (incommingChatMessage.Author.IsMod || DoesUserHavePermissionToRunAction(chatbotActionToRun, incommingChatMessage.Author.ID)) { // Have permission, run it. RunChatbotAction(chatbotActionToRun, incommingChatMessage, chatRoom); } else { // Don't have permission, tell the user only if it's a command. if (isReplyToChatbot) { chatRoom.PostReplyOrThrow(incommingChatMessage, "Sorry, you need more permissions to run that command."); } // Don't do anything for triggers. } }
/// <summary> /// Takes the contents from the message, strips out any "mentions", and trims the sides of the string. /// </summary> /// <param name="incommingMessage"></param> /// <returns></returns> protected override sealed string GetMessageContentsReadyForRegexParsing(Message incommingMessage) { return incommingMessage .GetContentsWithStrippedMentions() .Trim(); }