Exemple #1
0
        public async Task FormatCodeAsync([Summary("ID of a message, or pasted code."), Remainder] string input)
        {
            if (FilterService.ContainsBlockedWord(input))
            {
                return;
            }

            var userMention = $"<@{Context.User.Id}>";
            var firstWord   = input.Split(" ")[0];

            // If a language is entered, remove it from input
            if (TryDetectLanguage(firstWord, out string language))
            {
                input     = input.Remove(0, language.Length + 1);               // Length + 1 for removing the whitespace as well, faster than calling .Trim();
                firstWord = input.Split(" ")[0];
            }

            // If an id is entered, try assigning the target message's content to input
            if (TryDetectMessageFromId(firstWord, out string content, out ulong messageId, out ulong messageAuthorId))
            {
                input = RemoveBacktics(content);
                var user = await Context.Guild.GetUserAsync(messageAuthorId);

                userMention = $"{user.Username}";
                if (CanDeleteOriginal(messageId))
                {
                    await Context.Channel.DeleteMessageAsync(messageId);
                }
            }
        public async Task MathCommandAsync([Summary("The LaTeX input."), Remainder] string input)
        {
            if (FilterService.ContainsBlockedWord(input))
            {
                return;
            }

            bool canOverride = (Context.User as IGuildUser).GetPermissionLevel(Context) >= PermissionLevel.Guru;
            int  remaining   = MathService.LatexTimeoutRemaining(Context.User);

            if (!canOverride && remaining > 0)
            {
                throw new TimeoutException($"You need to wait {TimeSpan.FromMilliseconds(remaining).Humanize(2, minUnit: TimeUnit.Second)} before you can use this command again!");
            }

            MathService.UpdateLatexTimeout(Context.User);

            if (!TryRender(input, out Image originalImage, out string errorMessage))
            {
                await new EmbedBuilder()
                .WithDescription(Format.Code(errorMessage))
                .WithColor(Discord.Color.Red)
                .Build()
                .SendToChannel(Context.Channel);
                return;
            }

            using Image image         = AddPadding(originalImage);
            await using Stream stream = GetImageStream(image);

            await Context.Channel.SendFileAsync(stream, "equation.png");

            image.Dispose();
        }