Exemplo n.º 1
0
        public async Task <IActionResult> Get([Range(1, 1024)] int width, [Range(1, 1024)] int height, [Required] string color, string caption)
        {
            if (!Rgba32.TryParseHex(color, out Rgba32 rgba))
            {
                return(BadRequest("Cannot parse color"));
            }

            var image = new Image <Rgba32>(width, height);

            image.Mutate(ctx => ctx.Fill(rgba));

            if (!string.IsNullOrWhiteSpace(caption))
            {
                const float fontSize = 75;

                Font font = _fontFamily.CreateFont(fontSize);
                TextGraphicsOptions textOptions = new()
                {
                    TextOptions = new()
                    {
                        HorizontalAlignment = HorizontalAlignment.Center,
                    }
                };

                float textX = width / 2F;
                float textY = 7;

                image.Mutate(x => x.DrawText(textOptions, caption, font, _colorCalculator.GetTextColor(rgba), new PointF(textX, textY)));
            }

            var stream = new MemoryStream();
            await image.SaveAsJpegAsync(stream, HttpContext.RequestAborted);

            stream.Seek(0, SeekOrigin.Begin);

            return(File(stream, "image/jpeg"));
        }
    }
Exemplo n.º 2
0
        public async Task OnInlineQuery(InlineQuery q)
        {
            // Stores the string requested by the user
            string request = q.Query;

            if (string.IsNullOrEmpty(request))
            {
                return;
            }

            // An array that stores colors from the request
            float[] colors;
            bool    fromHex = false;

            try
            {
                if (Rgba32.TryParseHex(request, out Rgba32 rgba))
                {
                    colors  = new[] { (float)rgba.R, rgba.G, rgba.B };
                    fromHex = true;
                }
                else
                {
                    colors = ColorUtils.GetColorsFromString(request);
                }
            }
            catch
            {
                Log.Warning($"Can't parse colors from string: [{request}]");
                return;
            }

            // Inline card list
            var result = new List <InlineQueryResultBase>();

            foreach (ColorSpace colorSpace in _colorSpaces.Where(x => x.Verify(colors)))
            {
                if (fromHex && colorSpace.Name != "RGB")
                {
                    continue;
                }

                float[] formattedColors = colorSpace.ConvertToImageSharpFormat(colors);
                Rgba32  colorInRgb;
                try
                {
                    colorInRgb = _colorSpacesManager.CreateRgba32(colorSpace.Name, formattedColors);
                }
                catch (ArgumentException)
                {
                    continue;
                }

                string resultId = Utilities.GetRandomHexNumber(8);

                var(card, finalMessage) = _cardProcessor.ProcessInlineCardForColorSpace(resultId, formattedColors, colorInRgb, colorSpace);

                result.Add(card);
                _resultsStorage[resultId] = finalMessage;
            }

            try
            {
                await Bot.Client.AnswerInlineQueryAsync(q.Id, result, 0, true);
            }
            catch (Exception e)
            {
                Log.Error("Got exception on answering inline query", e);
            }

            Log.Information("Send answer with " + result.Count + " results to " + q.From.Id);
        }
Exemplo n.º 3
0
        private void MigrateBotConfig(DbConnection conn)
        {
            using (var checkMigratedCommand = conn.CreateCommand())
            {
                checkMigratedCommand.CommandText =
                    "UPDATE BotConfig SET HasMigratedBotSettings = 1 WHERE HasMigratedBotSettings = 0;";
                var changedRows = checkMigratedCommand.ExecuteNonQuery();
                if (changedRows == 0)
                {
                    return;
                }
            }

            Log.Information("Migrating bot settings...");

            var blockedCommands = new HashSet <string>();

            using (var cmdCom = conn.CreateCommand())
            {
                cmdCom.CommandText = $"SELECT Name from BlockedCmdOrMdl WHERE BotConfigId is not NULL";
                var cmdReader = cmdCom.ExecuteReader();
                while (cmdReader.Read())
                {
                    blockedCommands.Add(cmdReader.GetString(0));
                }
            }

            var blockedModules = new HashSet <string>();

            using (var mdlCom = conn.CreateCommand())
            {
                mdlCom.CommandText = $"SELECT Name from BlockedCmdOrMdl WHERE BotConfigId is NULL";
                var mdlReader = mdlCom.ExecuteReader();
                while (mdlReader.Read())
                {
                    blockedModules.Add(mdlReader.GetString(0));
                }
            }

            using var com   = conn.CreateCommand();
            com.CommandText = $@"SELECT DefaultPrefix, ForwardMessages, ForwardToAllOwners,
OkColor, ErrorColor, ConsoleOutputType, DMHelpString, HelpString, RotatingStatuses, Locale, GroupGreets
FROM BotConfig";

            using var reader = com.ExecuteReader();
            if (!reader.Read())
            {
                return;
            }

            _bss.ModifyConfig((x) =>
            {
                x.Prefix             = reader.GetString(0);
                x.ForwardMessages    = reader.GetBoolean(1);
                x.ForwardToAllOwners = reader.GetBoolean(2);
                x.Color = new ColorConfig()
                {
                    Ok = Rgba32.TryParseHex(reader.GetString(3), out var okColor)
                        ? okColor
                        : Rgba32.ParseHex("00e584"),
                    Error = Rgba32.TryParseHex(reader.GetString(4), out var errorColor)
                        ? errorColor
                        : Rgba32.ParseHex("ee281f"),
                };
                x.ConsoleOutputType = (ConsoleOutputType)reader.GetInt32(5);
                x.DmHelpText        = reader.IsDBNull(6) ? string.Empty : reader.GetString(6);
                x.HelpText          = reader.IsDBNull(7) ? string.Empty : reader.GetString(7);
                x.RotateStatuses    = reader.GetBoolean(8);
                try
                {
                    x.DefaultLocale = new CultureInfo(reader.GetString(9));
                }
                catch
                {
                    x.DefaultLocale = new CultureInfo("en-US");
                }

                x.GroupGreets      = reader.GetBoolean(10);
                x.Blocked.Commands = blockedCommands;
                x.Blocked.Modules  = blockedModules;
            });

            Log.Information("Data written to data/bot.yml");
        }