コード例 #1
0
        public async Task <CommandHandlerResult> ProcessCommand(BotCommand command, TwitchChannel channel)
        {
            Quote result;

            string[] args;
            switch (command.Command.ToLower())
            {
            case "!quote":
                args = command.SplitArgumentsOnSpaces(2);

                //check to see if the command was passed arguments.
                if (args.Length > 0)
                {
                    bool hasQuoteNumber;
                    int  quoteNumber;

                    // attempt to turn the first argument recieved into an int index to get a specific quote.
                    hasQuoteNumber = Int32.TryParse(args[0], out quoteNumber);
                    if (hasQuoteNumber)
                    {
                        //attempt to get a specificquote; otherwise return a message indicating failure.
                        result = await _apiClient.Quotes.GetSpecificQuoteForPersonalityAsync(channel.Personality, quoteNumber);

                        string responseMessage;
                        if (result == null)
                        {
                            responseMessage = $"Quote number {quoteNumber} doesn't exist. Beg the bot admins to add more quotes.";
                        }
                        else
                        {
                            responseMessage = result.Content;
                        }
                        return(new CommandHandlerResult(ResultType.HandledWithMessage, responseMessage, $"#{channel.Name}"));
                    }
                }
                //no args or bad args passed; return a random quote for the personality
                result = await _apiClient.Quotes.GetRandomQuoteForPersonalityAsync(channel.Personality);

                return(new CommandHandlerResult(ResultType.HandledWithMessage, result.Content, $"#{channel.Name}"));

            case "!addquote":
                //only channel admins can run this command
                if (channel.Users.Find(u => u.TwitchUsername.ToLower() == command.Sender) == null)
                {
                    return(new CommandHandlerResult(ResultType.Handled));
                }
                else
                {
                    result = await _apiClient.Quotes.AddQuoteForPersonalityAsync(channel.Personality, command.Arguments);

                    return(new CommandHandlerResult(ResultType.HandledWithMessage, result.Content, $"#{channel.Name}"));
                }

            default:
                return(new CommandHandlerResult(ResultType.NotHandled));
            }
        }
コード例 #2
0
        public async Task <CommandHandlerResult> ProcessCommand(BotCommand command, TwitchChannel channel)
        {
            PersonalityCommand personalityCommand;

            string[] args;
            switch (command.Command.ToLower())
            {
            case "!addcommand":
                //RC1 has some limitations around error handling that make this command able to crash the bot. Since a lot of this is going to get
                //reworked (maybe) just restrict this command to me for now
                //only channel admins can run this command.
                //if(channel.Users.Find( u => u.TwitchUsername == command.sender) == null)
                if (!(command.Sender == "feelthechi"))
                {
                    return(new CommandHandlerResult(ResultType.Handled));
                }
                //split the command arguments, and only use the first two; throw out the rest.
                args = command.SplitArgumentsOnSpaces(3);
                personalityCommand = await _apiClient.PersonalityCommands.AddCommandForPersonalityAsync(channel.Personality, new PersonalityCommand(args[0], args[1]));

                return(new CommandHandlerResult(ResultType.HandledWithMessage, $"{personalityCommand.Trigger} was created!", $"#{channel.Name}"));

            case "!deletecommand":
                //only channel admins can run this command.
                if (channel.Users.Find(u => u.TwitchUsername.ToLower() == command.Sender) == null)
                {
                    return(new CommandHandlerResult(ResultType.Handled));
                }
                //split the command arguments, and only use the first one; throw out the rest.
                args = command.SplitArgumentsOnSpaces(2);
                await _apiClient.PersonalityCommands.DeleteCommandForPersonalityAsync(channel.Personality, args[0]);

                return(new CommandHandlerResult(ResultType.HandledWithMessage, $"{args[0]} was deleted.", $"#{channel.Name}"));;

            default:
                personalityCommand = await _apiClient.PersonalityCommands.GetCommandForPersonalityAsync(channel.Personality, command.Command);

                if (personalityCommand == null)
                {
                    return(new CommandHandlerResult(ResultType.NotHandled));
                }
                else
                {
                    return(new CommandHandlerResult(ResultType.HandledWithMessage, personalityCommand.Response, $"#{channel.Name}"));;
                }
            }
        }