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

Commit 320dd3e

Browse files
Merge pull request #62 from InfinityGhost/reply-to-commands
Reply to commands rather than deleting original message
2 parents 111dc25 + 0021c46 commit 320dd3e

26 files changed

Lines changed: 142 additions & 141 deletions

TabletBot.Common/Attributes/Bot/CommandAttribute.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using JetBrains.Annotations;
23

34
namespace TabletBot.Common.Attributes.Bot
45
{
56
[AttributeUsage(AttributeTargets.Method)]
7+
[MeansImplicitUse(ImplicitUseTargetFlags.Members)]
68
public class CommandAttribute : Attribute
79
{
810
public CommandAttribute(params string[] arguments)

TabletBot.Common/LogMessage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Linq;
32

43
namespace TabletBot.Common
54
{

TabletBot.Common/Reflection/Extensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Linq;
32
using System.Reflection;
43

54
namespace TabletBot.Common.Reflection

TabletBot.Common/Settings.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.ObjectModel;
32
using System.IO;
43
using System.Text.Json;
@@ -15,7 +14,6 @@ public sealed class Settings
1514
private const ulong MODERATOR_ROLE_ID = 644180151755735060;
1615
private const ulong MUTED_ROLE_ID = 715342682293010452;
1716

18-
public int DeleteDelay { set; get; } = 5000;
1917
public ulong GuildID { set; get; } = MAIN_GUILD_ID;
2018
public ulong LogMessageChannelID { set; get; } = LOG_MESSAGE_CHANNEL_ID;
2119
public ulong ModeratorRoleID { set; get; } = MODERATOR_ROLE_ID;

TabletBot.Common/TabletBot.Common.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
<TargetFramework>net5.0</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" />
9+
</ItemGroup>
10+
711
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using JetBrains.Annotations;
3+
4+
namespace TabletBot.Discord.Commands.Attributes
5+
{
6+
[MeansImplicitUse(ImplicitUseTargetFlags.Members)]
7+
public class ModuleAttribute : Attribute
8+
{
9+
}
10+
}

TabletBot.Discord/Commands/CommandExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,13 @@ public static EmbedFooterBuilder ToEmbedFooter(this IUser user, string textForma
8484
IconUrl = user.GetAvatarUrl()
8585
};
8686
}
87+
88+
public static MessageReference ToReference(this IUserMessage message)
89+
{
90+
if (message.Channel is IGuildChannel guildChannel)
91+
return new MessageReference(message.Id, message.Channel.Id, guildChannel.GuildId);
92+
93+
return new MessageReference(message.Id, message.Channel.Id);
94+
}
8795
}
8896
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
1+
using System.Threading.Tasks;
2+
using Discord;
13
using Discord.Commands;
4+
using JetBrains.Annotations;
25

36
namespace TabletBot.Discord.Commands
47
{
8+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
59
public class CommandModule : ModuleBase
610
{
11+
protected override Task<IUserMessage> ReplyAsync(
12+
string message = null,
13+
bool isTTS = false,
14+
Embed embed = null,
15+
RequestOptions options = null,
16+
AllowedMentions allowedMentions = null,
17+
MessageReference messageReference = null,
18+
MessageComponent components = null,
19+
ISticker[] stickers = null,
20+
Embed[] embeds = null
21+
)
22+
{
23+
messageReference ??= Context.Message.ToReference();
24+
25+
return base.ReplyAsync(message, isTTS, embed, options, allowedMentions, messageReference, components,
26+
stickers, embeds);
27+
}
728
}
829
}

TabletBot.Discord/Commands/GitHubCommands.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
using Discord.Commands;
99
using Discord.WebSocket;
1010
using Octokit;
11+
using TabletBot.Discord.Commands.Attributes;
1112
using TabletBot.Discord.Embeds;
1213

1314
namespace TabletBot.Discord.Commands
1415
{
16+
[Module]
1517
public class GitHubCommands : CommandModule
1618
{
17-
private GitHubClient _gitHubClient;
19+
private readonly GitHubClient _gitHubClient;
1820
private readonly DiscordSocketClient _discordSocketClient;
1921

2022
public GitHubCommands(GitHubClient gitHubClient, DiscordSocketClient discordSocketClient)
@@ -23,16 +25,16 @@ public GitHubCommands(GitHubClient gitHubClient, DiscordSocketClient discordSock
2325
_discordSocketClient = discordSocketClient;
2426
}
2527

26-
private const string RepositoryOwner = "InfinityGhost";
27-
private const string RepositoryName = "OpenTabletDriver";
28+
private const string REPOSITORY_OWNER = "InfinityGhost";
29+
private const string REPOSITORY_NAME = "OpenTabletDriver";
2830
private static readonly Regex ArtifactRegex = new Regex("<.+?href=\"/InfinityGhost/OpenTabletDriver/suites/(?<Suite>.+?)/artifacts/(?<Artifact>.+?)\">(?<Name>.+?)</.+?>");
2931
private static readonly Regex CommitRegex = new Regex("href=\".+?commit/(?<SHA>.+?)/.+?/.+?\"");
3032

3133
[Command("overview", RunMode = RunMode.Async), Name("Overview"), Alias("info"), Summary("Shows an overview of the repository.")]
3234
public async Task GetRepositoryOverview()
3335
{
34-
var message = await ReplyAsync($"Getting overview for {RepositoryOwner}/{RepositoryName}...");
35-
var repo = await _gitHubClient.Repository.Get(RepositoryOwner, RepositoryName);
36+
var message = await ReplyAsync($"Getting overview for {REPOSITORY_OWNER}/{REPOSITORY_NAME}...");
37+
var repo = await _gitHubClient.Repository.Get(REPOSITORY_OWNER, REPOSITORY_NAME);
3638

3739
IEnumerable<Issue> issues =
3840
from issue in await _gitHubClient.Issue.GetAllForRepository(repo.Id)
@@ -46,7 +48,7 @@ from pr in await _gitHubClient.PullRequest.GetAllForRepository(repo.Id)
4648

4749
var embed = new EmbedBuilder
4850
{
49-
Title = $"{RepositoryOwner}/{RepositoryName}",
51+
Title = $"{REPOSITORY_OWNER}/{REPOSITORY_NAME}",
5052
Timestamp = repo.PushedAt,
5153
Url = repo.HtmlUrl,
5254
ThumbnailUrl = _discordSocketClient.CurrentUser.GetAvatarUrl(),
@@ -64,43 +66,40 @@ from pr in await _gitHubClient.PullRequest.GetAllForRepository(repo.Id)
6466
[Command("getpr", RunMode = RunMode.Async), Name("Get Pull Request"), Alias("pr"), Summary("Fetches pull request information.")]
6567
public async Task GetPullRequest([Remainder] int id)
6668
{
67-
await Context.Message.DeleteAsync();
6869
var message = await ReplyAsync($"Fetching pull request #{id}");
69-
var pr = await _gitHubClient.PullRequest.Get(RepositoryOwner, RepositoryName, id);
70+
var pr = await _gitHubClient.PullRequest.Get(REPOSITORY_OWNER, REPOSITORY_NAME, id);
7071
var embed = GitHubEmbeds.GetPullRequestEmbed(pr);
7172
await message.Update(embed);
7273
}
7374

7475
[Command("getissue", RunMode = RunMode.Async), Name("Get Issue"), Alias("issue"), Summary("Fetches issue information.")]
7576
public async Task GetIssue([Remainder] int id)
7677
{
77-
await Context.Message.DeleteAsync();
7878
var message = await ReplyAsync($"Fetching issue #{id}");
79-
var issue = await _gitHubClient.Issue.Get(RepositoryOwner, RepositoryName, id);
79+
var issue = await _gitHubClient.Issue.Get(REPOSITORY_OWNER, REPOSITORY_NAME, id);
8080
var embed = GitHubEmbeds.GetIssueEmbed(issue);
8181
await message.Update(embed);
8282
}
8383

8484
[Command("getartifacts", RunMode = RunMode.Async), Name("Get Artifacts"), Alias("artifacts", "artifact"), Summary("Returns all artifacts for a workflow from its URL.")]
8585
public async Task GetArtifacts([Remainder] string url)
8686
{
87-
await Context.Message.DeleteAsync();
8887
var message = await ReplyAsync("Fetching artifacts...");
8988

9089
string html;
9190
using (var client = new HttpClient())
9291
html = await client.GetStringAsync(url);
9392

94-
IEnumerable<Match> artifacts = await Task<MatchCollection>.Run(() => ArtifactRegex.Matches(html));
95-
Match commitMatch = await Task<Match>.Run(() => CommitRegex.Match(html));
96-
string sha = commitMatch.Groups["SHA"].Value;
97-
string hash = string.Concat(sha.Take(7));
98-
var commit = await _gitHubClient.Git.Commit.Get(RepositoryOwner, RepositoryName, sha);
93+
IEnumerable<Match> artifacts = await Task.Run(() => ArtifactRegex.Matches(html));
94+
var commitMatch = await Task.Run(() => CommitRegex.Match(html));
95+
var sha = commitMatch.Groups["SHA"].Value;
96+
var hash = string.Concat(sha.Take(7));
97+
var commit = await _gitHubClient.Git.Commit.Get(REPOSITORY_OWNER, REPOSITORY_NAME, sha);
9998
var title = commit.Message.Split(Environment.NewLine).First();
10099

101100
var embed = new EmbedBuilder
102101
{
103-
Title = string.Format("{0} ({1})", title, hash),
102+
Title = $"{title} ({hash})",
104103
Url = url,
105104
Color = Color.Green
106105
};

TabletBot.Discord/Commands/HelpCommands.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
using Discord;
66
using Discord.Commands;
77
using TabletBot.Common;
8+
using TabletBot.Discord.Commands.Attributes;
89

910
namespace TabletBot.Discord.Commands
1011
{
12+
[Module]
1113
public class HelpCommands : CommandModule
1214
{
1315
private readonly Settings _settings;
@@ -29,7 +31,6 @@ public HelpCommands(Settings settings, CommandService commands, IServiceProvider
2931
[Command("help", RunMode = RunMode.Async), Name("Help"), Summary("Lists all commands available.")]
3032
public async Task ListCommands()
3133
{
32-
await Context.Message.DeleteAsync();
3334
var message = await ReplyAsync("Fetching help...");
3435

3536
IEnumerable<ModuleInfo> modules =

0 commit comments

Comments
 (0)