Exemple #1
0
        public async Task CarbonCode(CommandContext ctx, string theme, [RemainingText] string code)
        {
            if (theme != null && code == null)
            {
                await ctx.Channel.SendMessageAsync("Using this command requires you to specify a theme, use `ds carbon-themes` to get a list of all available themes");

                return;
            }

            if (code == null)
            {
                await ctx.Channel.SendMessageAsync("You need to give a code block");

                return;
            }

            var cs1 = code.IndexOf("```") + 3;

            cs1 = code.IndexOf('\n', cs1) + 1;
            var cs2 = code.LastIndexOf("```");

            if (cs1 == -1 || cs2 == -1)
            {
                await ctx.Channel.SendErrorAsync("Error!", "You need to wrap the code into a code block.");

                return;
            }

            var cs = code.Substring(cs1, cs2 - cs1);

            cs = cs.Remove(cs.LastIndexOf("\n"));
            var csUrl = WebUtility.UrlEncode(cs);

            string validTheme = CarbonHandler.ThemeMatcher(theme);

            var baseUrl = "https://carbon.now.sh/";
            var url     = @$ "{baseUrl}?l=auto&t={validTheme}&code={csUrl}";

            string title = $"Code Snippet by {ctx.Member.Username}";

            await ctx.Channel.SendCarbonCodeAsync(title, url);
        }