Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 84e6260

Browse files
Merge pull request #66 from InfinityGhost/embed-improvements
Improve appearance of embeds
2 parents 862c89b + 63b0acc commit 84e6260

7 files changed

Lines changed: 77 additions & 52 deletions

File tree

TabletBot.Discord/Commands/GitHubCommands.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ from pr in await _gitHubClient.PullRequest.GetAllForRepository(repo.Id)
6767
public async Task GetPullRequest([Remainder] int id)
6868
{
6969
var message = await ReplyAsync($"Fetching pull request #{id}");
70-
var pr = await _gitHubClient.PullRequest.Get(REPOSITORY_OWNER, REPOSITORY_NAME, id);
71-
var embed = GitHubEmbeds.GetPullRequestEmbed(pr);
70+
var pr = await _gitHubClient.Issue.Get(REPOSITORY_OWNER, REPOSITORY_NAME, id);
71+
var embed = GitHubEmbeds.GetEmbed(pr);
7272
await message.Update(embed);
7373
}
7474

@@ -77,7 +77,7 @@ public async Task GetIssue([Remainder] int id)
7777
{
7878
var message = await ReplyAsync($"Fetching issue #{id}");
7979
var issue = await _gitHubClient.Issue.Get(REPOSITORY_OWNER, REPOSITORY_NAME, id);
80-
var embed = GitHubEmbeds.GetIssueEmbed(issue);
80+
var embed = GitHubEmbeds.GetEmbed(issue);
8181
await message.Update(embed);
8282
}
8383

TabletBot.Discord/Commands/SnippetCommands.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,7 @@ public async Task ExportSnippet(string prefix)
146146

147147
if (Snippets.FirstOrDefault(s => s.Snippet == prefix) is SnippetStore snippet)
148148
{
149-
var sb = new StringBuilder();
150-
sb.AppendLine(Formatting.CODE_BLOCK);
151-
sb.AppendLine(snippet.Content);
152-
sb.AppendLine(Formatting.CODE_BLOCK);
153-
154-
await ReplyAsync(sb.ToString());
149+
await ReplyAsync(Formatting.CodeBlock(snippet.Content));
155150
}
156151
else
157152
{
Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,75 @@
1-
using Discord;
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Discord;
24
using Octokit;
35

46
namespace TabletBot.Discord.Embeds
57
{
68
public static class GitHubEmbeds
79
{
8-
public static Embed GetIssueEmbed(Issue issue)
10+
private const uint OPEN_COLOR = 0x238636;
11+
private const uint RESOLVED_COLOR = 0x8957e5;
12+
private const uint CLOSED_COLOR = 0xda3633;
13+
14+
public static EmbedBuilder GetEmbed(Issue? issue)
915
{
10-
var embed = new EmbedBuilder
16+
if (issue == null)
17+
{
18+
return new EmbedBuilder()
19+
{
20+
Color = CLOSED_COLOR,
21+
Description = "No issue or pull request was found."
22+
};
23+
}
24+
25+
return new EmbedBuilder
1126
{
12-
Title = string.Format("{0} #{1}", issue.Title, issue.Number),
13-
Timestamp = issue.CreatedAt,
27+
Title = $"{issue.Title} #{issue.Number}",
1428
Url = issue.HtmlUrl,
15-
Footer = new EmbedFooterBuilder
29+
Description = issue.Body ?? string.Empty,
30+
Color = GetColor(issue),
31+
Fields = GetFields(issue).ToList(),
32+
Author = new EmbedAuthorBuilder
1633
{
17-
Text = string.Format("{0} opened this issue on {1}", issue.User.Login, issue.CreatedAt),
34+
Name = issue.User.Login,
35+
Url = issue.User.HtmlUrl,
1836
IconUrl = issue.User.AvatarUrl
19-
},
20-
Description = issue.Body
37+
}
2138
};
22-
return embed.Build();
2339
}
2440

25-
public static Embed GetPullRequestEmbed(PullRequest pr)
41+
private static uint GetColor(Issue issue)
2642
{
27-
var embed = new EmbedBuilder
43+
if (issue.ClosedAt == null)
44+
return OPEN_COLOR;
45+
46+
if (issue.PullRequest != null)
47+
return issue.PullRequest.Merged ? RESOLVED_COLOR : CLOSED_COLOR;
48+
49+
return RESOLVED_COLOR;
50+
}
51+
52+
private static IEnumerable<EmbedFieldBuilder> GetFields(Issue issue)
53+
{
54+
if (issue.Milestone != null)
2855
{
29-
Title = $"{pr.Title} #{pr.Number}",
30-
Timestamp = pr.UpdatedAt,
31-
Url = pr.HtmlUrl,
32-
Footer = new EmbedFooterBuilder
56+
yield return new EmbedFieldBuilder
3357
{
34-
Text = $"{pr.User?.Login} opened this pull request on {pr.CreatedAt}",
35-
IconUrl = pr.User?.AvatarUrl
36-
},
37-
Description = pr.Body ?? string.Empty
38-
};
39-
return embed.Build();
58+
Name = "Milestone",
59+
Value = Formatting.UrlString(issue.Milestone.Title, issue.Milestone.HtmlUrl),
60+
IsInline = true
61+
};
62+
}
63+
64+
if (issue.Labels.Any())
65+
{
66+
yield return new EmbedFieldBuilder
67+
{
68+
Name = "Labels",
69+
Value = string.Join(", ", issue.Labels.Select(l => Formatting.CodeString(l.Name))),
70+
IsInline = true
71+
};
72+
}
4073
}
4174
}
4275
}

TabletBot.Discord/Embeds/SnippetEmbeds.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public static bool GetSnippetEmbed(Settings settings, string? prefix, out EmbedB
2525
{
2626
Color = Color.Red,
2727
Title = "Failed to show snippet",
28-
Description = $"Failed to show the `{prefix}` snippet." + Environment.NewLine
29-
+ "Verify that you have spelled it correctly."
28+
Description = $"Failed to show the `{prefix}` snippet." + Environment.NewLine + "Verify that you have spelled it correctly."
3029
};
3130
return false;
3231
}

TabletBot.Discord/Formatting.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Discord;
23

34
namespace TabletBot.Discord
@@ -11,6 +12,11 @@ public static class Formatting
1112
public const string QUOTE_PREFIX = "> ";
1213
public const string CODE_BLOCK = "```";
1314

14-
public static string UrlString(IAttachment attachment) => $"[{attachment.Filename}]({attachment.Url})";
15+
public static string UrlString(IAttachment attachment) => UrlString(attachment.Filename, attachment.Url);
16+
public static string UrlString(string text, string url) => $"[{text}]({url})";
17+
18+
public static string CodeString(string text) => $"{CODE_AFFIX}{text}{CODE_AFFIX}";
19+
public static string CodeBlock(string text, string? lang = null) =>
20+
$"{CODE_BLOCK}{lang}{Environment.NewLine}{text}{Environment.NewLine}{CODE_BLOCK}";
1521
}
1622
}

TabletBot.Discord/SlashCommands/SnippetSlashCommands.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,8 @@ private async Task ExportSnippet(SocketSlashCommand command)
215215
if (Snippets.FirstOrDefault(s => s.Snippet == snippet) is SnippetStore store)
216216
{
217217
var sb = new StringBuilder();
218-
sb.AppendLine(Formatting.CODE_AFFIX + store.Title + Formatting.CODE_AFFIX);
219-
sb.AppendLine(Formatting.CODE_BLOCK);
220-
sb.AppendLine(store.Content.Replace(Environment.NewLine, @"\n"));
221-
sb.AppendLine(Formatting.CODE_BLOCK);
218+
sb.AppendLine(Formatting.CodeString(store.Title));
219+
sb.AppendLine(Formatting.CodeBlock(store.Content.Replace(Environment.NewLine, @"\n")));
222220

223221
await command.FollowupAsync(sb.ToString());
224222
}

TabletBot.Discord/Watchers/GitHub/IssueMessageWatcher.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System.Collections;
12
using System.Collections.Generic;
3+
using System.Dynamic;
24
using System.Linq;
35
using System.Text.RegularExpressions;
46
using System.Threading.Tasks;
@@ -50,25 +52,17 @@ public async Task Receive(IMessage message)
5052

5153
private async Task ReplyWithEmbeds(IUserMessage message, IList<uint> refs)
5254
{
53-
var issues = await _gitHubClient.Issue.GetAllForRepository(OWNER, NAME);
54-
var prs = await _gitHubClient.PullRequest.GetAllForRepository(OWNER, NAME);
55-
var embeds = GetEmbedsForRefs(issues, prs, refs).ToArray();
55+
var embeds = new Embed[refs.Count];
5656

57-
await message.Channel.SendMessageAsync(embeds: embeds, messageReference: message.ToReference());
58-
}
59-
60-
private IEnumerable<Embed> GetEmbedsForRefs(IReadOnlyList<Issue> issues, IReadOnlyList<PullRequest> prs, IList<uint> refs)
61-
{
6257
for (var i = 0; i < refs.Count && i < _settings.GitHubIssueRefLimit; i++)
6358
{
64-
var issueRef = refs[i];
59+
var issueRef = (int)refs[i];
60+
var issue = await _gitHubClient.Issue.Get(OWNER, NAME, issueRef);
6561

66-
if (issues.FirstOrDefault(s => s.Number == issueRef) is Issue issue)
67-
{
68-
var pr = prs.FirstOrDefault(pr => pr.Number == issue.Number);
69-
yield return pr == null ? GitHubEmbeds.GetIssueEmbed(issue) : GitHubEmbeds.GetPullRequestEmbed(pr);
70-
}
62+
embeds[i] = GitHubEmbeds.GetEmbed(issue).Build();
7163
}
64+
65+
await message.Channel.SendMessageAsync(embeds: embeds.ToArray(), messageReference: message.ToReference());
7266
}
7367

7468
private static IEnumerable<uint>? GetIssueRefNumbers(string message)

0 commit comments

Comments
 (0)