public override void ExecuteCommand(EvtChatCommandArgs args)
        {
            List <string> arguments = args.Command.ArgumentsAsList;

            if (arguments.Count == 0)
            {
                QueueMessage(UsageMessage);
                return;
            }

            char[] msgArr = args.Command.ArgumentsAsString.ToCharArray();
            Array.Reverse(msgArr);

            string msg = new string(msgArr);

            ClientServiceTypes clientServiceType = DataHelper.GetClientServiceType();

            //Ignore any output starting with a "/" to avoid exploiting Twitch chat commands
            if (clientServiceType == ClientServiceTypes.Twitch && msg.StartsWith('/') == true)
            {
                QueueMessage("!uoy rof sdnammoc tahc hctiwT oN");
                return;
            }

            QueueMessage(msg);
        }
        public override void ExecuteCommand(EvtChatCommandArgs args)
        {
            ClientServiceTypes clientServiceType = DataHelper.GetClientServiceType();

            string userName = args.Command.ChatMessage.Username;

            User user = DataHelper.GetOrAddUser(userName, out bool added);

            //Check if the user has the ability to calculate
            using (BotDBContext context = DatabaseManager.OpenContext())
            {
                user = DataHelper.GetUserNoOpen(userName, context);

                if (user.HasEnabledAbility(PermissionConstants.CALCULATE_ABILITY) == false)
                {
                    QueueMessage("You do not have the ability to make calculations.");
                    return;
                }
            }

            Expression exp = null;

            try
            {
                string expression = args.Command.ArgumentsAsString;

                exp                    = new Expression(expression);
                exp.Options            = EvaluateOptions.IgnoreCase;
                exp.EvaluateParameter += Exp_EvaluateParameter;
                exp.EvaluateFunction  += Exp_EvaluateFunction;

                string finalExpr = exp.Evaluate().ToString();

                //You can use text in calculate to make the bot do things, such as Twitch chat commands
                //Ignore any output starting with a "/" to avoid exploiting this
                if (clientServiceType == ClientServiceTypes.Twitch && finalExpr.StartsWith('/') == true)
                {
                    QueueMessage("Very clever, but I'm one step ahead of you.");
                    return;
                }

                QueueMessage(finalExpr);
            }
            catch (Exception exc)
            {
                QueueMessage(exc.Message);
            }
            finally
            {
                if (exp != null)
                {
                    exp.EvaluateParameter -= Exp_EvaluateParameter;
                    exp.EvaluateFunction  -= Exp_EvaluateFunction;
                }
            }
        }
Esempio n. 3
0
        private void InitClientService()
        {
            ClientServiceTypes clientServiceType = ClientServiceTypes.Terminal;

            using (BotDBContext dbContext = DatabaseManager.OpenContext())
            {
                Settings setting = dbContext.SettingCollection.FirstOrDefault((set) => set.Key == SettingsConstants.CLIENT_SERVICE_TYPE);

                if (setting != null)
                {
                    clientServiceType = (ClientServiceTypes)setting.ValueInt;
                }
                else
                {
                    TRBotLogger.Logger.Warning($"Database does not contain the {SettingsConstants.CLIENT_SERVICE_TYPE} setting. It will default to {clientServiceType}.");
                }
            }

            TRBotLogger.Logger.Information($"Setting up client service: {clientServiceType}");

            if (clientServiceType == ClientServiceTypes.Terminal)
            {
                ClientService = new TerminalClientService(DataConstants.COMMAND_IDENTIFIER);

                MsgHandler.SetLogToLogger(false);
            }
            else if (clientServiceType == ClientServiceTypes.Twitch)
            {
                TwitchLoginSettings twitchSettings = ConnectionHelper.ValidateTwitchCredentialsPresent(DataConstants.DataFolderPath,
                                                                                                       TwitchConstants.LOGIN_SETTINGS_FILENAME);

                //If either of these fields are empty, the data is invalid
                if (string.IsNullOrEmpty(twitchSettings.ChannelName) || string.IsNullOrEmpty(twitchSettings.Password) ||
                    string.IsNullOrEmpty(twitchSettings.BotName))
                {
                    TRBotLogger.Logger.Error($"Twitch login settings are invalid. Please modify the data in the \"{TwitchConstants.LOGIN_SETTINGS_FILENAME}\" file.");
                    return;
                }

                TwitchClient          client      = new TwitchClient();
                ConnectionCredentials credentials = ConnectionHelper.MakeCredentialsFromTwitchLogin(twitchSettings);
                ClientService = new TwitchClientService(credentials, twitchSettings.ChannelName, DataConstants.COMMAND_IDENTIFIER, DataConstants.COMMAND_IDENTIFIER, true);
            }

            //Initialize service
            ClientService?.Initialize();
        }
Esempio n. 4
0
        public override void ExecuteCommand(EvtChatCommandArgs args)
        {
            string message = args.Command.ArgumentsAsString;

            if (string.IsNullOrEmpty(message) == true)
            {
                QueueMessage(UsageMessage);
                return;
            }

            ClientServiceTypes clientServiceType = DataHelper.GetClientServiceType();

            //Ignore any output starting with a "/" to avoid exploiting Twitch chat commands
            if (clientServiceType == ClientServiceTypes.Twitch && message.StartsWith('/') == true)
            {
                QueueMessage("I can't say any Twitch chat commands for you - no hard feelings!");
                return;
            }

            QueueMessage(message);
        }
        public void Initialize()
        {
            if (Initialized == true)
            {
                return;
            }

            //Kimimaru: Use invariant culture
            Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

            //Load all the necessary data; if something doesn't exist, save out an empty object so it can be filled in manually
            string loginText = Globals.ReadFromTextFileOrCreate(Globals.LoginInfoFilename);

            LoginInformation = JsonConvert.DeserializeObject <LoginInfo>(loginText);

            if (LoginInformation == null)
            {
                Console.WriteLine("No login information found; attempting to create file template. If created, please manually fill out the information.");

                LoginInformation = new LoginInfo();
                string text = JsonConvert.SerializeObject(LoginInformation, Formatting.Indented);
                Globals.SaveToTextFile(Globals.LoginInfoFilename, text);
            }

            LoadSettingsAndBotData();

            //Kimimaru: If the bot itself isn't in the bot data, add it as an admin!
            if (string.IsNullOrEmpty(LoginInformation.BotName) == false)
            {
                string botName = LoginInformation.BotName.ToLowerInvariant();
                User   botUser = null;
                if (BotData.Users.TryGetValue(botName, out botUser) == false)
                {
                    botUser       = new User();
                    botUser.Name  = botName;
                    botUser.Level = (int)AccessLevels.Levels.Admin;
                    BotData.Users.TryAdd(botName, botUser);

                    SaveBotData();
                }
            }

            ClientServiceTypes clientType = BotSettings.ClientSettings.ClientType;

            //Credentials don't matter if running through a terminal
            if (clientType != ClientServiceTypes.Terminal)
            {
                try
                {
                    Credentials = new ConnectionCredentials(LoginInformation.BotName, LoginInformation.Password);
                }
                catch (Exception exception)
                {
                    Console.WriteLine($"Invalid credentials: {exception.Message}");
                    Console.WriteLine("Cannot proceed. Please double check the login information in the data folder");
                    return;
                }
            }

            Console.WriteLine($"Setting up client service: {clientType}");

            //Set up client service
            //NOTE: Replace with something more scalable later, such as a factory
            switch (clientType)
            {
            case ClientServiceTypes.Terminal:
                ClientService = new TerminalClientService();
                break;

            case ClientServiceTypes.Twitch:
                ClientService = new TwitchClientService(Credentials, LoginInformation.ChannelName,
                                                        Globals.CommandIdentifier, Globals.CommandIdentifier, true);
                break;
            }

            //Initialize service
            ClientService.Initialize();

            UnsubscribeEvents();
            SubscribeEvents();

            //Set up message handler
            MsgHandler = new BotMessageHandler(ClientService, LoginInformation.ChannelName, BotSettings.MsgSettings.MessageCooldown);

            RoutineHandler = new BotRoutineHandler(ClientService);
            RoutineHandler.AddRoutine(new PeriodicMessageRoutine());
            RoutineHandler.AddRoutine(new CreditsGiveRoutine());
            RoutineHandler.AddRoutine(new ReconnectRoutine());
            RoutineHandler.AddRoutine(new ChatBotResponseRoutine());

            //Initialize controller input - validate the controller type first
            if (InputGlobals.IsVControllerSupported((InputGlobals.VControllerTypes)BotData.LastVControllerType) == false)
            {
                BotData.LastVControllerType = (int)InputGlobals.GetDefaultSupportedVControllerType();
            }

            InputGlobals.VControllerTypes vCType = (InputGlobals.VControllerTypes)BotData.LastVControllerType;
            Console.WriteLine($"Setting up virtual controller {vCType}");

            InputGlobals.SetVirtualController(vCType);

            Initialized = true;
        }