示例#1
0
        protected override async Task PerformInternal(UserViewModel user, IEnumerable <string> arguments)
        {
            string replacementText = await this.ReplaceStringWithSpecialModifiers(this.SpecialIdentifierReplacement, user, arguments);

            if (this.SpecialIdentifierShouldProcessMath)
            {
                try
                {
                    // Process Math
                    CalculationEngine engine = new CalculationEngine();
                    engine.AddFunction("random", Random);
                    engine.AddFunction("randomrange", RandomRange);

                    double result = engine.Calculate(replacementText);
                    replacementText = result.ToString();
                }
                catch (Exception ex)
                {
                    // Calculation failed, log and set to 0
                    Logger.Log(ex, false, false);
                    replacementText = "0";
                }
            }

            if (this.MakeGloballyUsable)
            {
                SpecialIdentifierStringBuilder.AddCustomSpecialIdentifier(this.SpecialIdentifierName, replacementText);
            }
            else
            {
                this.extraSpecialIdentifiers[this.SpecialIdentifierName] = replacementText;
            }
        }
示例#2
0
        protected override async Task PerformInternal(UserViewModel user, IEnumerable <string> arguments)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                using (HttpResponseMessage response = await httpClient.GetAsync(await this.ReplaceStringWithSpecialModifiers(this.Url, user, arguments, encode: true)))
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string webRequestResult = await response.Content.ReadAsStringAsync();

                        if (!string.IsNullOrEmpty(webRequestResult))
                        {
                            if (this.ResponseAction == WebRequestResponseActionTypeEnum.Chat)
                            {
                                if (ChannelSession.Chat != null)
                                {
                                    await ChannelSession.Chat.SendMessage(await this.ReplaceSpecialIdentifiers(this.ResponseChatText, user, arguments, webRequestResult));
                                }
                            }
                            else if (this.ResponseAction == WebRequestResponseActionTypeEnum.Command)
                            {
                                CommandBase command = ChannelSession.AllCommands.FirstOrDefault(c => c.ID.Equals(this.ResponseCommandID));

#pragma warning disable CS0612 // Type or member is obsolete
                                if (command == null && !string.IsNullOrEmpty(this.ResponseCommandName))
                                {
                                    command = ChannelSession.Settings.ChatCommands.FirstOrDefault(c => c.Name.Equals(this.ResponseCommandName));
                                    if (command != null)
                                    {
                                        this.ResponseCommandID = command.ID;
                                    }
                                }
                                this.ResponseCommandName = null;
#pragma warning restore CS0612 // Type or member is obsolete

                                if (command != null)
                                {
                                    string argumentsText    = (this.ResponseCommandArgumentsText != null) ? this.ResponseCommandArgumentsText : string.Empty;
                                    string commandArguments = await this.ReplaceSpecialIdentifiers(argumentsText, user, arguments, webRequestResult);

                                    command.AddSpecialIdentifiers(this.GetAdditiveSpecialIdentifiers());
                                    await command.Perform(user, commandArguments.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries));
                                }
                            }
                            else if (this.ResponseAction == WebRequestResponseActionTypeEnum.SpecialIdentifier)
                            {
                                string replacementText = await this.ReplaceStringWithSpecialModifiers(webRequestResult, user, arguments);

                                SpecialIdentifierStringBuilder.AddCustomSpecialIdentifier(this.SpecialIdentifierName, replacementText);
                            }
                        }
                    }
                }
            }
        }
        protected override async Task PerformInternal(UserViewModel user, IEnumerable <string> arguments)
        {
            string replacementText = await this.ReplaceStringWithSpecialModifiers(this.SpecialIdentifierReplacement, user, arguments);

            if (this.SpecialIdentifierShouldProcessMath)
            {
                try
                {
                    replacementText = replacementText.Replace("random(", "customrandom(");

                    // Process Math
                    CalculationEngine engine = new CalculationEngine(new System.Globalization.CultureInfo("en-US"));
                    engine.AddFunction("customrandom", Random);
                    engine.AddFunction("randomrange", RandomRange);

                    double result = engine.Calculate(replacementText);
                    replacementText = result.ToString();
                }
                catch (Exception ex)
                {
                    // Calculation failed, log and set to 0
                    Logger.Log(ex);
                    replacementText = "0";
                }
            }
            else
            {
                replacementText = await this.ProcessStringFunction(replacementText, "removespaces", (text) => { return(Task.FromResult(text.Replace(" ", string.Empty))); });

                replacementText = await this.ProcessStringFunction(replacementText, "removecommas", (text) => { return(Task.FromResult(text.Replace(",", string.Empty))); });

                replacementText = await this.ProcessStringFunction(replacementText, "tolower", (text) => { return(Task.FromResult(text.ToLower())); });

                replacementText = await this.ProcessStringFunction(replacementText, "toupper", (text) => { return(Task.FromResult(text.ToUpper())); });

                replacementText = await this.ProcessStringFunction(replacementText, "urlencode", (text) => { return(Task.FromResult(HttpUtility.UrlEncode(text))); });
            }

            if (this.MakeGloballyUsable)
            {
                SpecialIdentifierStringBuilder.AddCustomSpecialIdentifier(this.SpecialIdentifierName, replacementText);
            }
            else
            {
                this.extraSpecialIdentifiers[this.SpecialIdentifierName] = replacementText;
            }
        }
示例#4
0
        protected override async Task PerformInternal(UserViewModel user, IEnumerable <string> arguments)
        {
            if (ChannelSession.Services.TranslationService != null)
            {
                string text = await this.ReplaceStringWithSpecialModifiers(this.Text, user, arguments);

                string translationResult = await ChannelSession.Services.TranslationService.Translate(this.Culture, text, this.AllowProfanity);

                if (string.IsNullOrEmpty(translationResult))
                {
                    translationResult = this.Text;
                }

                if (!string.IsNullOrEmpty(translationResult))
                {
                    if (string.IsNullOrEmpty(await ModerationHelper.ShouldBeModerated(user, translationResult)))
                    {
                        if (this.ResponseAction == TranslationResponseActionTypeEnum.Chat)
                        {
                            if (ChannelSession.Chat != null)
                            {
                                await ChannelSession.Chat.SendMessage(await this.ReplaceSpecialIdentifiers(this.ResponseChatText, user, arguments, translationResult));
                            }
                        }
                        else if (this.ResponseAction == TranslationResponseActionTypeEnum.Command)
                        {
                            CommandBase command = ChannelSession.AllEnabledCommands.FirstOrDefault(c => c.ID.Equals(this.ResponseCommandID));
                            if (command != null)
                            {
                                string argumentsText    = (this.ResponseCommandArgumentsText != null) ? this.ResponseCommandArgumentsText : string.Empty;
                                string commandArguments = await this.ReplaceSpecialIdentifiers(this.ResponseChatText, user, arguments, translationResult);

                                await command.Perform(user, commandArguments.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), this.GetExtraSpecialIdentifiers());
                            }
                        }
                        else if (this.ResponseAction == TranslationResponseActionTypeEnum.SpecialIdentifier)
                        {
                            string replacementText = await this.ReplaceStringWithSpecialModifiers(translationResult, user, arguments);

                            SpecialIdentifierStringBuilder.AddCustomSpecialIdentifier(this.SpecialIdentifierName, replacementText);
                        }
                    }
                }
            }
        }
示例#5
0
        protected override async Task PerformInternal(UserViewModel user, IEnumerable <string> arguments)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                using (HttpResponseMessage response = await httpClient.GetAsync(await this.ReplaceStringWithSpecialModifiers(this.Url, user, arguments)))
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string webRequestResult = await response.Content.ReadAsStringAsync();

                        if (!string.IsNullOrEmpty(webRequestResult))
                        {
                            if (this.ResponseAction == WebRequestResponseActionTypeEnum.Chat)
                            {
                                if (ChannelSession.Chat != null)
                                {
                                    await ChannelSession.Chat.SendMessage(await this.ReplaceSpecialIdentifiers(this.ResponseChatText, user, arguments, webRequestResult));
                                }
                            }
                            else if (this.ResponseAction == WebRequestResponseActionTypeEnum.Command)
                            {
                                ChatCommand command = ChannelSession.Settings.ChatCommands.FirstOrDefault(c => c.Name.Equals(this.ResponseCommandName));
                                if (command != null)
                                {
                                    string argumentsText = (this.ResponseCommandArgumentsText != null) ? this.ResponseCommandArgumentsText : string.Empty;
                                    SpecialIdentifierStringBuilder siString = new SpecialIdentifierStringBuilder(argumentsText);
                                    siString.ReplaceSpecialIdentifier(WebRequestAction.ResponseSpecialIdentifier, webRequestResult);
                                    await command.Perform(user, siString.ToString().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries));
                                }
                            }
                            else if (this.ResponseAction == WebRequestResponseActionTypeEnum.SpecialIdentifier)
                            {
                                string replacementText = await this.ReplaceStringWithSpecialModifiers(webRequestResult, user, arguments);

                                SpecialIdentifierStringBuilder.AddCustomSpecialIdentifier(this.SpecialIdentifierName, replacementText);
                            }
                        }
                    }
                }
            }
        }
示例#6
0
        protected override async Task PerformInternal(UserViewModel user, IEnumerable <string> arguments)
        {
            string filePath = (await this.ReplaceStringWithSpecialModifiers(this.FilePath, user, arguments));

            if (this.FileActionType == FileActionTypeEnum.SaveToFile || this.FileActionType == FileActionTypeEnum.AppendToFile)
            {
                filePath = filePath.ToFilePathString();

                string textToWrite = (!string.IsNullOrEmpty(this.TransferText)) ? this.TransferText : string.Empty;
                textToWrite = await this.ReplaceStringWithSpecialModifiers(textToWrite, user, arguments);

                if (this.FileActionType == FileActionTypeEnum.SaveToFile)
                {
                    await ChannelSession.Services.FileService.SaveFile(filePath, textToWrite);
                }
                else if (this.FileActionType == FileActionTypeEnum.AppendToFile)
                {
                    string dataToWrite = textToWrite;
                    if (!string.IsNullOrEmpty(await ChannelSession.Services.FileService.ReadFile(filePath)))
                    {
                        dataToWrite = Environment.NewLine + dataToWrite;
                    }
                    await ChannelSession.Services.FileService.AppendFile(filePath, dataToWrite);
                }
            }
            else
            {
                SpecialIdentifierStringBuilder.RemoveCustomSpecialIdentifier(this.TransferText);

                string data = await ChannelSession.Services.FileService.ReadFile(filePath);

                if (!string.IsNullOrEmpty(data))
                {
                    if (this.FileActionType == FileActionTypeEnum.ReadSpecificLineFromFile || this.FileActionType == FileActionTypeEnum.ReadRandomLineFromFile ||
                        this.FileActionType == FileActionTypeEnum.RemoveSpecificLineFromFile || this.FileActionType == FileActionTypeEnum.RemoveRandomLineFromFile)
                    {
                        List <string> lines = new List <string>(data.Split(new string[] { Environment.NewLine, "\n" }, StringSplitOptions.RemoveEmptyEntries));
                        if (lines.Count > 0)
                        {
                            int lineIndex = -1;
                            if (this.FileActionType == FileActionTypeEnum.ReadSpecificLineFromFile || this.FileActionType == FileActionTypeEnum.RemoveSpecificLineFromFile)
                            {
                                if (!string.IsNullOrEmpty(this.LineIndexToRead))
                                {
                                    string lineToRead = await this.ReplaceStringWithSpecialModifiers(this.LineIndexToRead, user, arguments);

                                    if (int.TryParse(lineToRead, out lineIndex))
                                    {
                                        lineIndex = lineIndex - 1;
                                        if (lineIndex >= 0 && lineIndex < lines.Count)
                                        {
                                            data = lines[lineIndex];
                                        }
                                        else
                                        {
                                            return;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                lineIndex = RandomHelper.GenerateRandomNumber(lines.Count);
                                data      = lines[lineIndex];
                            }

                            if (this.FileActionType == FileActionTypeEnum.RemoveSpecificLineFromFile || this.FileActionType == FileActionTypeEnum.RemoveRandomLineFromFile)
                            {
                                if (lineIndex >= 0)
                                {
                                    lines.RemoveAt(lineIndex);
                                    await ChannelSession.Services.FileService.SaveFile(filePath, string.Join(Environment.NewLine, lines));
                                }
                            }
                        }
                        else
                        {
                            return;
                        }
                    }

                    data = await this.ReplaceStringWithSpecialModifiers(data, user, arguments);

                    SpecialIdentifierStringBuilder.AddCustomSpecialIdentifier(this.TransferText, data);
                }
            }
        }
示例#7
0
        protected override async Task PerformInternal(UserViewModel user, IEnumerable <string> arguments)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                using (HttpResponseMessage response = await httpClient.GetAsync(await this.ReplaceStringWithSpecialModifiers(this.Url, user, arguments, encode: true)))
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string webRequestResult = await response.Content.ReadAsStringAsync();

                        if (!string.IsNullOrEmpty(webRequestResult))
                        {
                            if (this.ResponseAction == WebRequestResponseActionTypeEnum.Chat)
                            {
                                if (ChannelSession.Chat != null)
                                {
                                    await ChannelSession.Chat.SendMessage(await this.ReplaceSpecialIdentifiers(this.ResponseChatText, user, arguments, webRequestResult));
                                }
                            }
                            else if (this.ResponseAction == WebRequestResponseActionTypeEnum.Command)
                            {
                                CommandBase command = ChannelSession.AllCommands.FirstOrDefault(c => c.ID.Equals(this.ResponseCommandID));

#pragma warning disable CS0612 // Type or member is obsolete
                                if (command == null && !string.IsNullOrEmpty(this.ResponseCommandName))
                                {
                                    command = ChannelSession.Settings.ChatCommands.FirstOrDefault(c => c.Name.Equals(this.ResponseCommandName));
                                    if (command != null)
                                    {
                                        this.ResponseCommandID = command.ID;
                                    }
                                }
                                this.ResponseCommandName = null;
#pragma warning restore CS0612 // Type or member is obsolete

                                if (command != null)
                                {
                                    string argumentsText    = (this.ResponseCommandArgumentsText != null) ? this.ResponseCommandArgumentsText : string.Empty;
                                    string commandArguments = await this.ReplaceSpecialIdentifiers(argumentsText, user, arguments, webRequestResult);

                                    command.AddSpecialIdentifiers(this.GetAdditiveSpecialIdentifiers());
                                    await command.Perform(user, commandArguments.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries));
                                }
                            }
                            else if (this.ResponseAction == WebRequestResponseActionTypeEnum.SpecialIdentifier)
                            {
                                string replacementText = await this.ReplaceStringWithSpecialModifiers(webRequestResult, user, arguments);

                                SpecialIdentifierStringBuilder.AddCustomSpecialIdentifier(this.SpecialIdentifierName, replacementText);
                            }
                            else if (this.ResponseAction == WebRequestResponseActionTypeEnum.JSONToSpecialIdentifiers)
                            {
                                try
                                {
                                    JObject jobj = JObject.Parse(webRequestResult);
                                    if (this.JSONToSpecialIdentifiers != null)
                                    {
                                        foreach (var kvp in this.JSONToSpecialIdentifiers)
                                        {
                                            string[] splits = kvp.Key.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
                                            if (splits.Count() > 0)
                                            {
                                                JToken currentToken = jobj;
                                                for (int i = 0; i < splits.Count(); i++)
                                                {
                                                    if (currentToken[splits[i]] == null)
                                                    {
                                                        currentToken = null;
                                                        break;
                                                    }
                                                    currentToken = currentToken[splits[i]];
                                                }

                                                if (currentToken != null)
                                                {
                                                    string replacementText = await this.ReplaceStringWithSpecialModifiers(currentToken.ToString(), user, arguments);

                                                    SpecialIdentifierStringBuilder.AddCustomSpecialIdentifier(kvp.Value, replacementText);
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MixItUp.Base.Util.Logger.Log(ex);
                                }
                            }
                        }
                    }
                }
            }
        }
        protected override async Task PerformInternal(UserViewModel user, IEnumerable <string> arguments)
        {
            string replacementText = await this.ReplaceStringWithSpecialModifiers(this.SpecialIdentifierReplacement, user, arguments);

            SpecialIdentifierStringBuilder.AddCustomSpecialIdentifier(this.SpecialIdentifierName, replacementText);
        }
示例#9
0
        protected override async Task PerformInternal(UserViewModel user, IEnumerable <string> arguments)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("User-Agent", $"MixItUp/{Assembly.GetEntryAssembly().GetName().Version.ToString()} (Web call from Mix It Up; https://mixitupapp.com; [email protected])");

                using (HttpResponseMessage response = await httpClient.GetAsync(await this.ReplaceStringWithSpecialModifiers(this.Url, user, arguments, encode: true)))
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string webRequestResult = await response.Content.ReadAsStringAsync();

                        if (!string.IsNullOrEmpty(webRequestResult))
                        {
                            string decodedWebRequestResult = HttpUtility.HtmlDecode(webRequestResult);
                            if (this.ResponseAction == WebRequestResponseActionTypeEnum.Chat)
                            {
                                if (ChannelSession.Chat != null)
                                {
                                    await ChannelSession.Chat.SendMessage(await this.ReplaceSpecialIdentifiers(this.ResponseChatText, user, arguments, decodedWebRequestResult));
                                }
                            }
                            else if (this.ResponseAction == WebRequestResponseActionTypeEnum.Command)
                            {
                                CommandBase command = ChannelSession.AllEnabledCommands.FirstOrDefault(c => c.ID.Equals(this.ResponseCommandID));

#pragma warning disable CS0612 // Type or member is obsolete
                                if (command == null && !string.IsNullOrEmpty(this.ResponseCommandName))
                                {
                                    command = ChannelSession.Settings.ChatCommands.FirstOrDefault(c => c.Name.Equals(this.ResponseCommandName));
                                    if (command != null)
                                    {
                                        this.ResponseCommandID = command.ID;
                                    }
                                }
                                this.ResponseCommandName = null;
#pragma warning restore CS0612 // Type or member is obsolete

                                if (command != null)
                                {
                                    string argumentsText    = (this.ResponseCommandArgumentsText != null) ? this.ResponseCommandArgumentsText : string.Empty;
                                    string commandArguments = await this.ReplaceSpecialIdentifiers(argumentsText, user, arguments, decodedWebRequestResult);

                                    await command.Perform(user, commandArguments.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries), this.GetExtraSpecialIdentifiers());
                                }
                            }
                            else if (this.ResponseAction == WebRequestResponseActionTypeEnum.SpecialIdentifier)
                            {
                                string replacementText = await this.ReplaceStringWithSpecialModifiers(decodedWebRequestResult, user, arguments);

                                SpecialIdentifierStringBuilder.AddCustomSpecialIdentifier(this.SpecialIdentifierName, replacementText);
                            }
                            else if (this.ResponseAction == WebRequestResponseActionTypeEnum.JSONToSpecialIdentifiers)
                            {
                                try
                                {
                                    JToken jToken = JToken.Parse(webRequestResult);
                                    if (this.JSONToSpecialIdentifiers != null)
                                    {
                                        foreach (var kvp in this.JSONToSpecialIdentifiers)
                                        {
                                            string[] splits = kvp.Key.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
                                            if (splits.Count() > 0)
                                            {
                                                JToken currentToken = jToken;
                                                for (int i = 0; i < splits.Count(); i++)
                                                {
                                                    if (currentToken is JObject)
                                                    {
                                                        JObject jobjToken = (JObject)currentToken;
                                                        if (jobjToken.ContainsKey(splits[i]))
                                                        {
                                                            currentToken = jobjToken[splits[i]];
                                                        }
                                                        else
                                                        {
                                                            currentToken = null;
                                                            break;
                                                        }
                                                    }
                                                    else if (currentToken is JArray)
                                                    {
                                                        JArray jarrayToken = (JArray)currentToken;
                                                        if (int.TryParse(splits[i], out int index) && index >= 0 && index < jarrayToken.Count)
                                                        {
                                                            currentToken = jarrayToken[index];
                                                        }
                                                        else
                                                        {
                                                            currentToken = null;
                                                            break;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        currentToken = null;
                                                        break;
                                                    }
                                                }

                                                if (currentToken != null)
                                                {
                                                    string replacementText = await this.ReplaceStringWithSpecialModifiers(HttpUtility.HtmlDecode(currentToken.ToString()), user, arguments);

                                                    SpecialIdentifierStringBuilder.AddCustomSpecialIdentifier(kvp.Value, replacementText);
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MixItUp.Base.Util.Logger.Log(ex);
                                }
                            }
                        }
                    }
                }
            }
        }