Ejemplo n.º 1
0
        public async Task RunAsync(
            [Remainder]
            [Summary("The code to paste.")]
            string code)
        {
            string url;
            string error = null;

            try
            {
                url = await _service.UploadCodeAsync(Context.Message, code);
            }
            catch (WebException ex)
            {
                url   = null;
                error = ex.Message;
            }

            if (url != null)
            {
                var embed = _service.BuildEmbed(Context.User, code, url);

                await ReplyAsync(Context.User.Mention, false, embed);

                await Context.Message.DeleteAsync();
            }
            else
            {
                await ReplyAsync(error);
            }
        }
        private async Task <string> UploadLogAsync(IMessage logMessage, IEmote countedEmote, IEnumerable <IUserMessage> messages)
        {
            messages = messages.Where(message => message.Reactions.ContainsKey(countedEmote));
            var allEntries = string.Join('\n', messages.Select(d => $"{d.Reactions[countedEmote].ReactionCount} reactions: {d.GetJumpUrl()}"));

            return(await _pasteService.UploadCodeAsync($"All entries for contest held on {DateTimeOffset.UtcNow}\nResults here: {logMessage.GetJumpUrl()}\n\n{allEntries}"));
        }
Ejemplo n.º 3
0
        public async Task GetCampers(
            [Summary("Input the start when to get the Campers. " +
                     "'9pm' will work and will fetch from 9:00-9:30pm.")]
            DateTimeOffset start,
            [Summary("The message that should be in their message to be included.")]
            string filter)
        {
            var end  = start.AddMinutes(30);
            var user = await _db.Users.FindAsync(Context.User.Id);

            var text = await _campingService.DownloadAttachment(Context);

            var messages = _campingService.ExtractMessages(text)
                           .Where(m => m.Time >= start && m.Time <= end)
                           .Where(m => string.IsNullOrWhiteSpace(filter) ? true : m.Content.Contains(filter, StringComparison.OrdinalIgnoreCase))
                           .GroupBy(m => m.Channel).Select(g => g.First())
                           .OrderBy(m => m.Time);

            var sb = new StringBuilder();

            foreach (var message in messages)
            {
                sb.AppendLine($"[{message.Time:t}] **{message.Channel.Trim()}**: {message.Content.Trim()}");
            }

            await ReplyAsync(await _paste.UploadCodeAsync(sb.ToString()));
        }
Ejemplo n.º 4
0
        private async Task <Embed> BuildEmbedAsync(IReadOnlyCollection <TagSummary> tags, IUser ownerUser = null, IGuild ownerGuild = null, IRole ownerRole = null)
        {
            var orderedTags = tags.OrderBy(x => x.Name);

            var ownerName = ownerUser?.Username
                            ?? ownerGuild?.Name
                            ?? ownerRole?.Name;

            var ownerImage = ownerUser?.GetDefiniteAvatarUrl()
                             ?? ownerGuild?.IconUrl;

            var builder = new EmbedBuilder();

            builder
            .WithAuthor(ownerName, ownerImage)
            .WithColor(Color.DarkPurple)
            .WithDescription(tags.Count > 0 ? null : "No tags.")
            .WithTimestamp(DateTimeOffset.Now)
            .WithTitle("Tags");

            const int tagsToDisplay = 5;

            foreach (var tag in orderedTags.Take(tagsToDisplay))
            {
                builder.AddField(x => x.WithName(tag.Name)
                                 .WithValue($"{tag.Uses} uses"));
            }

            if (tags.Count > tagsToDisplay)
            {
                var pasteContent = BuildPaste(orderedTags);

                var fieldName = $"and {tags.Count - tagsToDisplay} more";

                try
                {
                    var pasteLink = await CodePasteService.UploadCodeAsync(pasteContent, "txt");

                    builder.AddField(x => x.WithName(fieldName)
                                     .WithValue($"[View at {pasteLink}]({pasteLink})"));
                }
                catch (WebException ex)
                {
                    builder.AddField(x => x.WithName(fieldName)
                                     .WithValue(ex.Message));
                }
            }

            return(builder.Build());
        }
Ejemplo n.º 5
0
        public void Emit(LogEvent logEvent)
        {
            const int DiscordStringTruncateLength = 1000;

            var formattedMessage = logEvent.RenderMessage(_formatProvider);
            var webhookClient    = new DiscordWebhookClient(_webhookId, _webhookToken);

            var message = new EmbedBuilder()
                          .WithAuthor("DiscordLogger")
                          .WithTitle("Modix")
                          .WithTimestamp(DateTimeOffset.UtcNow)
                          .WithColor(Color.Red);

            try
            {
                var messagePayload = $"{formattedMessage}\n{logEvent.Exception?.Message}";

                message.AddField(new EmbedFieldBuilder()
                                 .WithIsInline(false)
                                 .WithName($"LogLevel: {logEvent.Level}")
                                 .WithValue(Format.Code(messagePayload.TruncateTo(DiscordStringTruncateLength))));

                var eventAsJson = JsonConvert.SerializeObject(logEvent, _jsonSerializerSettings);

                var url = CodePasteService.UploadCodeAsync(eventAsJson, "json").GetAwaiter().GetResult();

                message.AddField(new EmbedFieldBuilder()
                                 .WithIsInline(false)
                                 .WithName("Full Log Event")
                                 .WithValue($"[view on paste.mod.gg]({url})"));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unable to upload log event. {ex}");

                var stackTracePayload = $"{formattedMessage}\n{logEvent.Exception?.ToString().TruncateTo(DiscordStringTruncateLength)}".TruncateTo(DiscordStringTruncateLength);

                message.AddField(new EmbedFieldBuilder()
                                 .WithIsInline(false)
                                 .WithName("Stack Trace")
                                 .WithValue(Format.Code(stackTracePayload)));

                message.AddField(new EmbedFieldBuilder()
                                 .WithIsInline(false)
                                 .WithName("Upload Failure Exception")
                                 .WithValue(Format.Code($"{ex.ToString().TruncateTo(DiscordStringTruncateLength)}")));
            }
            webhookClient.SendMessageAsync(string.Empty, embeds: new[] { message.Build() }, username: "******");
        }
Ejemplo n.º 6
0
        public static async Task UploadToServiceIfBiggerThan(this EmbedBuilder embed, string content, string contentType, uint size, CodePasteService service)
        {
            if (content.Length > size)
            {
                try
                {
                    var resultLink = await service.UploadCodeAsync(content, contentType);

                    embed.AddField(a => a.WithName("More...").WithValue($"[View on Hastebin]({resultLink})"));
                }
                catch (WebException we)
                {
                    embed.AddField(a => a.WithName("More...").WithValue(we.Message));
                }
            }
        }
Ejemplo n.º 7
0
        private async Task UploadMessage(IUserMessage arg)
        {
            try
            {
                var url = await _service.UploadCodeAsync(arg);

                var embed = _service.BuildEmbed(arg.Author, arg.Content, url);

                await arg.Channel.SendMessageAsync(arg.Author.Mention, false, embed);

                await arg.DeleteAsync();
            }
            catch (WebException ex)
            {
                await arg.Channel.SendMessageAsync($"I would have reuploaded your long message, but: {ex.Message}");
            }
        }