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

Commit fc65c53

Browse files
Merge pull request #72 from InfinityGhost/feature/github-code-ref
Add support for file references in messages
2 parents 2c534f2 + 6618fbf commit fc65c53

6 files changed

Lines changed: 70 additions & 9 deletions

File tree

TabletBot.Common/GuildSettings.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

TabletBot.Discord/BotServiceCollection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static IServiceCollection Build(Settings settings, State state, DiscordSo
3131
.AddSingleton<CommandService>()
3232
// Message watchers
3333
.AddWatcher<CommandMessageWatcher>()
34+
.AddWatcher<CodeMessageWatcher>()
3435
.AddWatcher<IssueMessageWatcher>()
3536
.AddWatcher<SpamMessageWatcher>()
3637
.AddWatcher<ModMailMessageWatcher>()

TabletBot.Discord/Formatting.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text;
23
using Discord;
34

45
namespace TabletBot.Discord
@@ -18,5 +19,13 @@ public static class Formatting
1819
public static string CodeString(string text) => $"{CODE_AFFIX}{text}{CODE_AFFIX}";
1920
public static string CodeBlock(string text, string? lang = null) =>
2021
$"{CODE_BLOCK}{lang}{Environment.NewLine}{text}{Environment.NewLine}{CODE_BLOCK}";
22+
23+
public static void AppendCodeBlock(this StringBuilder stringBuilder, string[] lines, string? lang = null)
24+
{
25+
stringBuilder.AppendLine(CODE_BLOCK + lang);
26+
foreach (var line in lines)
27+
stringBuilder.AppendLine(line);
28+
stringBuilder.AppendLine(CODE_BLOCK);
29+
}
2130
}
2231
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using System.Text.RegularExpressions;
5+
using System.Threading.Tasks;
6+
using Discord;
7+
using Octokit;
8+
using TabletBot.Discord.Commands;
9+
10+
namespace TabletBot.Discord.Watchers.GitHub
11+
{
12+
public class CodeMessageWatcher : IMessageWatcher
13+
{
14+
private readonly GitHubClient _client;
15+
16+
public CodeMessageWatcher(GitHubClient client)
17+
{
18+
_client = client;
19+
}
20+
21+
private static readonly Regex CodeRefRegex = new Regex(
22+
@"https:\/\/github.com\/(?<Owner>.+?)\/(?<Repo>.+?)\/blob\/(?<GitRef>.+?)\/(?<Path>.+?)#L(?<StartLine>.+?)(?:-L(?<EndLine>.+?))?($| |>)",
23+
RegexOptions.Compiled
24+
);
25+
26+
public async Task Receive(IMessage message)
27+
{
28+
var match = CodeRefRegex.Match(message.Content);
29+
if (message is IUserMessage userMessage && match.Success)
30+
{
31+
using (message.Channel.EnterTypingState())
32+
{
33+
var owner = match.Groups["Owner"].Value;
34+
var repo = match.Groups["Repo"].Value;
35+
var gitRef = match.Groups["GitRef"].Value;
36+
var path = match.Groups["Path"].Value;
37+
var startLine = int.Parse(match.Groups["StartLine"].Value) - 1;
38+
var endLine = match.Groups["EndLine"].Success ? int.Parse(match.Groups["EndLine"].Value) : startLine + 1;
39+
40+
var url = $"https://raw.githubusercontent.com/{owner}/{repo}/{gitRef}/{path}";
41+
var rawContent = await _client.Connection.GetHtml(new Uri(url));
42+
43+
var fileContent = rawContent.Body.Split(Environment.NewLine);
44+
var lines = fileContent[startLine..endLine];
45+
46+
var extension = Path.GetExtension(path).Replace(".", string.Empty);
47+
48+
var sb = new StringBuilder();
49+
sb.AppendLine(Formatting.CodeString(path));
50+
sb.AppendCodeBlock(lines, extension);
51+
52+
await message.Channel.SendMessageAsync(sb.ToString(), messageReference: userMessage.ToReference());
53+
}
54+
}
55+
}
56+
57+
public Task Deleted(Cacheable<IMessage, ulong> message, Cacheable<IMessageChannel, ulong> channel) =>
58+
Task.CompletedTask;
59+
}
60+
}

TabletBot/TabletBot.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
</ItemGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.3.0-alpha.19577.1" />
109
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19577.1" />
1110
<PackageReference Include="System.CommandLine.Rendering" Version="0.3.0-alpha.21216.1" />
1211
<PackageReference Include="System.Reactive.Core" Version="5.0.0" />

deps.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
(fetchNuGet { pname = "Octokit"; version = "0.51.0"; sha256 = "1qmmw3vva026lz70xw68pwv6m18ry53a6z5didy2b4isd5bnrs09"; })
1919
(fetchNuGet { pname = "System.Collections.Immutable"; version = "5.0.0"; sha256 = "1kvcllagxz2q92g81zkz81djkn2lid25ayjfgjalncyc68i15p0r"; })
2020
(fetchNuGet { pname = "System.CommandLine"; version = "2.0.0-beta1.21216.1"; sha256 = "10p3i10sh9aarqfzac3bgmv9zabb8m5c2aylf3chwlh71kp9l63l"; })
21-
(fetchNuGet { pname = "System.CommandLine.DragonFruit"; version = "0.3.0-alpha.19577.1"; sha256 = "09xv1ii0irr69snc9bjzkdg17ccasc3ib40ydakqqwf50lk5za0x"; })
2221
(fetchNuGet { pname = "System.CommandLine.Experimental"; version = "0.3.0-alpha.19577.1"; sha256 = "1qrj0pvz5zvw0a12sn8xc9i1bh58dsgkdq8gjinzj10l4aa38g1a"; })
2322
(fetchNuGet { pname = "System.CommandLine.Rendering"; version = "0.3.0-alpha.21216.1"; sha256 = "0kwrjm1i4xc9vgjid1l4wlxddk92fsyypjzg36zs8qxcw07kh9mw"; })
2423
(fetchNuGet { pname = "System.Interactive.Async"; version = "5.0.0"; sha256 = "00flf80ahpyhrsny2kfl9bsyh1mxmsdsigpq3rzhaps9wgdbmzjz"; })

0 commit comments

Comments
 (0)