예제 #1
0
파일: Help.cs 프로젝트: hxhxd/SOCVR-Chatbot
        public override void RunAction(Message incomingChatMessage, Room chatRoom)
        {
            var message = "This is a chat bot for the SO Close Vote Reviewers chat room, developed by the [SOCVR developers](https://github.com/SO-Close-Vote-Reviewers). " +
                          "For more information see the [github page](https://github.com/SO-Close-Vote-Reviewers/SOCVR-Chatbot). " +
                          "Reply with `{0}` to see a list of commands."
                          .FormatInline(ChatbotActionRegister.GetChatBotActionUsage <Commands>());

            chatRoom.PostMessageOrThrow(message);
        }
예제 #2
0
        /// <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="incomingChatMessage">The chat message that was said.</param>
        /// <param name="chatRoom">The room the chat message was said in.</param>
        public void ProcessPing(Message incomingChatMessage, Room chatRoom)
        {
            var           isReplyToChatbot   = false;
            ChatbotAction chatbotActionToRun = null;

            EnsureAuthorInDatabase(incomingChatMessage);

            // Is the message a confirmation to a command suggestion?
            if (yesReply.IsMatch(incomingChatMessage.Content))
            {
                var cmd = unrecdCmds.FirstOrDefault(kv => kv.Value.Key.ID == incomingChatMessage.ParentID &&
                                                    kv.Key.Author.ID == incomingChatMessage.Author.ID);

                if (cmd.Key != null)
                {
                    // What's a good sign of laziness? ..... Using reflection.
                    typeof(Message)
                    .GetProperty("Content")
                    .SetValue(incomingChatMessage, cmd.Value.Value);

                    KeyValuePair <Message, string> temp;
                    unrecdCmds.TryRemove(cmd.Key, out temp);
                }
            }

            if (chatbotActionToRun == null)
            {
                // Determine the list of possible actions that work from the message.
                var possibleChatbotActionsToRun = ChatbotActionRegister.AllChatActions
                                                  .Where(x => x.DoesChatMessageActiveAction(incomingChatMessage, true))
                                                  .ToList();

                if (possibleChatbotActionsToRun.Count > 1)
                {
                    throw new Exception("More than one possible chat bot action to run for the input '{0}'"
                                        .FormatSafely(incomingChatMessage.Content));
                }

                if (!possibleChatbotActionsToRun.Any())
                {
                    var results = simCmd.FindCommand(incomingChatMessage.Content);
                    var msg     = "Sorry, I don't understand that. ";

                    if (results != null)
                    {
                        if (!results.OptionsSubstituted)
                        {
                            msg += $"Maybe you meant `{results.SuggestedCmdText}`.";
                        }
                        else
                        {
                            msg += $"Did you mean `{results.SuggestedCmdText}`?";
                        }

                        Message reply;
                        try
                        {
                            reply = chatRoom.PostReply(incomingChatMessage, msg);
                            if (reply == null)
                            {
                                throw new InvalidOperationException("Unable to post message");
                            }

                            if (results.OptionsSubstituted)
                            {
                                unrecdCmds[incomingChatMessage] = new KeyValuePair <Message, string>(reply, results.SuggestedCmdText);
                            }
                        }
                        catch (DuplicateMessageException)
                        {
                            // Not sure what to do here atm.
                        }
                    }

                    return;
                }

                // You have a single item to run.
                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 (incomingChatMessage.Author.IsMod || DoesUserHavePermissionToRunAction(chatbotActionToRun, incomingChatMessage.Author.ID))
            {
                // Have permission, run it.
                RunChatbotAction(chatbotActionToRun, incomingChatMessage, chatRoom);
            }
            else
            {
                // Don't have permission, tell the user only if it's a command.
                if (isReplyToChatbot)
                {
                    //a person can be denied a command for one of two reasons:
                    // 1) they are not in the required permission group
                    // 2) they are not in ANY permission groups
                    // this is dependent on the command they try to run

                    if (chatbotActionToRun.RequiredPermissionGroup != null)
                    {
                        //the command required a specific group which the user was not a part of
                        chatRoom.PostReplyOrThrow(incomingChatMessage,
                                                  $"Sorry, you are not in the {chatbotActionToRun.RequiredPermissionGroup} permission group. You can request access by running `request permission to {chatbotActionToRun.RequiredPermissionGroup}`.");
                    }
                    else
                    {
                        //the command can be ran by anyone who is in at least one permission group,
                        //but this user is not in any
                        chatRoom.PostReplyOrThrow(incomingChatMessage,
                                                  $"Sorry, you need to be in at least one permission group to run this command. Run `{ChatbotActionRegister.GetChatBotActionUsage<Membership>()}` to see the list of groups.");
                    }
                }
                // Don't do anything for triggers.
            }
        }