Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions TabletBot.Common/Attributes/Bot/CommandAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using JetBrains.Annotations;

namespace TabletBot.Common.Attributes.Bot
{
[AttributeUsage(AttributeTargets.Method)]
[MeansImplicitUse(ImplicitUseTargetFlags.Members)]
public class CommandAttribute : Attribute
{
public CommandAttribute(params string[] arguments)
Expand Down
1 change: 0 additions & 1 deletion TabletBot.Common/LogMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;

namespace TabletBot.Common
{
Expand Down
1 change: 0 additions & 1 deletion TabletBot.Common/Reflection/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Reflection;

namespace TabletBot.Common.Reflection
Expand Down
2 changes: 0 additions & 2 deletions TabletBot.Common/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Text.Json;
Expand All @@ -15,7 +14,6 @@ public sealed class Settings
private const ulong MODERATOR_ROLE_ID = 644180151755735060;
private const ulong MUTED_ROLE_ID = 715342682293010452;

public int DeleteDelay { set; get; } = 5000;
public ulong GuildID { set; get; } = MAIN_GUILD_ID;
public ulong LogMessageChannelID { set; get; } = LOG_MESSAGE_CHANNEL_ID;
public ulong ModeratorRoleID { set; get; } = MODERATOR_ROLE_ID;
Expand Down
4 changes: 4 additions & 0 deletions TabletBot.Common/TabletBot.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions TabletBot.Discord/Commands/Attributes/ModuleAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using JetBrains.Annotations;

namespace TabletBot.Discord.Commands.Attributes
{
[MeansImplicitUse(ImplicitUseTargetFlags.Members)]
public class ModuleAttribute : Attribute
{
}
}
8 changes: 8 additions & 0 deletions TabletBot.Discord/Commands/CommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,13 @@ public static EmbedFooterBuilder ToEmbedFooter(this IUser user, string textForma
IconUrl = user.GetAvatarUrl()
};
}

public static MessageReference ToReference(this IUserMessage message)
{
if (message.Channel is IGuildChannel guildChannel)
return new MessageReference(message.Id, message.Channel.Id, guildChannel.GuildId);

return new MessageReference(message.Id, message.Channel.Id);
}
}
}
21 changes: 21 additions & 0 deletions TabletBot.Discord/Commands/CommandModule.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using JetBrains.Annotations;

namespace TabletBot.Discord.Commands
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public class CommandModule : ModuleBase
{
protected override Task<IUserMessage> ReplyAsync(
string message = null,
bool isTTS = false,
Embed embed = null,
RequestOptions options = null,
AllowedMentions allowedMentions = null,
MessageReference messageReference = null,
MessageComponent components = null,
ISticker[] stickers = null,
Embed[] embeds = null
)
{
messageReference ??= Context.Message.ToReference();

return base.ReplyAsync(message, isTTS, embed, options, allowedMentions, messageReference, components,
stickers, embeds);
}
}
}
33 changes: 16 additions & 17 deletions TabletBot.Discord/Commands/GitHubCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
using Discord.Commands;
using Discord.WebSocket;
using Octokit;
using TabletBot.Discord.Commands.Attributes;
using TabletBot.Discord.Embeds;

namespace TabletBot.Discord.Commands
{
[Module]
public class GitHubCommands : CommandModule
{
private GitHubClient _gitHubClient;
private readonly GitHubClient _gitHubClient;
private readonly DiscordSocketClient _discordSocketClient;

public GitHubCommands(GitHubClient gitHubClient, DiscordSocketClient discordSocketClient)
Expand All @@ -23,16 +25,16 @@ public GitHubCommands(GitHubClient gitHubClient, DiscordSocketClient discordSock
_discordSocketClient = discordSocketClient;
}

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

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

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

var embed = new EmbedBuilder
{
Title = $"{RepositoryOwner}/{RepositoryName}",
Title = $"{REPOSITORY_OWNER}/{REPOSITORY_NAME}",
Timestamp = repo.PushedAt,
Url = repo.HtmlUrl,
ThumbnailUrl = _discordSocketClient.CurrentUser.GetAvatarUrl(),
Expand All @@ -64,43 +66,40 @@ from pr in await _gitHubClient.PullRequest.GetAllForRepository(repo.Id)
[Command("getpr", RunMode = RunMode.Async), Name("Get Pull Request"), Alias("pr"), Summary("Fetches pull request information.")]
public async Task GetPullRequest([Remainder] int id)
{
await Context.Message.DeleteAsync();
var message = await ReplyAsync($"Fetching pull request #{id}");
var pr = await _gitHubClient.PullRequest.Get(RepositoryOwner, RepositoryName, id);
var pr = await _gitHubClient.PullRequest.Get(REPOSITORY_OWNER, REPOSITORY_NAME, id);
var embed = GitHubEmbeds.GetPullRequestEmbed(pr);
await message.Update(embed);
}

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

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

string html;
using (var client = new HttpClient())
html = await client.GetStringAsync(url);

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

var embed = new EmbedBuilder
{
Title = string.Format("{0} ({1})", title, hash),
Title = $"{title} ({hash})",
Url = url,
Color = Color.Green
};
Expand Down
3 changes: 2 additions & 1 deletion TabletBot.Discord/Commands/HelpCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using Discord;
using Discord.Commands;
using TabletBot.Common;
using TabletBot.Discord.Commands.Attributes;

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

IEnumerable<ModuleInfo> modules =
Expand Down
37 changes: 9 additions & 28 deletions TabletBot.Discord/Commands/ModerationCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
using Discord;
using Discord.Commands;
using TabletBot.Common;
using TabletBot.Discord.Commands.Attributes;

namespace TabletBot.Discord.Commands
{
public class ModerationCommands : ModuleBase
[Module]
public class ModerationCommands : CommandModule
{
private readonly Bot _bot;
private readonly Settings _settings;
Expand All @@ -22,14 +24,17 @@ public async Task DeleteMessage(int count = 1)
{
await Context.Message.DeleteAsync();
var messages = await Context.Channel.GetMessagesAsync(count).FlattenAsync();
await (Context.Channel as ITextChannel).DeleteMessagesAsync(messages);
if (Context.Channel is ITextChannel textChannel)
await textChannel.DeleteMessagesAsync(messages);
else
await ReplyAsync("Unable to delete messages as the current channel is not a text channel.");
}

[Command("save", RunMode = RunMode.Async), Name("Force Save"), RequireOwner]
public async Task ForceSaveSettings()
{
await Context.Message.DeleteAsync();
await _settings.Write(Platform.SettingsFile);
await ReplyAsync("Settings force saved.", allowedMentions: AllowedMentions.None);
await Log.WriteAsync("Settings", $"{Context.Message.Author.Username} force-saved the configuration to {Platform.SettingsFile.FullName}");
}

Expand All @@ -44,32 +49,8 @@ public async Task ForceKillBot()
[Command("set-prefix", RunMode = RunMode.Async), Name("Set prefix"), RequireOwner]
public async Task SetPrefix([Remainder] string prefix)
{
await Context.Message.DeleteAsync();
_settings.CommandPrefix = prefix;
var message = await ReplyAsync(string.Format("Set the command prefix to `{0}`.", _settings.CommandPrefix));
message.DeleteDelayed(_settings.DeleteDelay);
}

[Command("set-reply-delete-delay", RunMode = RunMode.Async), Name("Set bot reply delete delay"), RequireOwner]
public async Task SetReplyDeleteDelay(TimeSpan delay)
{
await Context.Message.DeleteAsync();
_settings.DeleteDelay = (int)delay.TotalMilliseconds;

var embed = new EmbedBuilder
{
Title = "Set message delay",
Fields =
{
new EmbedFieldBuilder
{
Name = "Delay",
Value = _settings.DeleteDelay
}
}
};
var message = await ReplyAsync(embed: embed.Build());
message.DeleteDelayed(_settings.DeleteDelay);
await ReplyAsync($"Set the command prefix to `{_settings.CommandPrefix}`.");
}
}
}
15 changes: 5 additions & 10 deletions TabletBot.Discord/Commands/RoleCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using Discord.WebSocket;
using TabletBot.Common;
using TabletBot.Common.Store;
using TabletBot.Discord.Commands.Attributes;

namespace TabletBot.Discord.Commands
{
[Module]
public class RoleCommands : CommandModule
{
private readonly Settings _settings;
Expand All @@ -24,18 +26,14 @@ public RoleCommands(Settings settings, DiscordSocketClient discordSocketClient)
public async Task AddReactiveRole(IRole role, string emote)
{
var message = Context.Message.ReferencedMessage!;
var messageRef = new MessageReference(message.Id, message.Channel.Id);

var emoji = emote.GetEmote();
await message.AddReactionAsync(emoji);
var reactionRole = new RoleManagementMessageStore(message.Id, role.Id, emote);
_settings.ReactiveRoles.Add(reactionRole);
await _settings.Overwrite();

var reply = await ReplyAsync($"Reactive role added: {reactionRole.EmoteName}",
messageReference: messageRef);
reply.DeleteDelayed(_settings.DeleteDelay);
await Context.Message.DeleteAsync();
await ReplyAsync($"Reactive role added: {reactionRole.EmoteName}", messageReference: message.ToReference());
}

[Command("remove-react-role", RunMode = RunMode.Async), Name("Remove reactive role")]
Expand All @@ -51,15 +49,12 @@ public async Task RemoveReactiveRole(IRole role)
await message.RemoveReactionAsync(emoji, _discordSocketClient.CurrentUser);
await _settings.Overwrite();

var reply = await ReplyAsync($"Reactive role removed from: {reactiveRole.EmoteName}", messageReference: messageRef);
reply.DeleteDelayed(_settings.DeleteDelay);
await ReplyAsync($"Reactive role removed from: {reactiveRole.EmoteName}", messageReference: messageRef);
}
else
{
var reply = await ReplyAsync($"{role.Name} is not assigned as reactive to the referenced message.");
reply.DeleteDelayed(_settings.DeleteDelay);
await ReplyAsync($"{role.Name} is not assigned as reactive to the referenced message.");
}
await Context.Message.DeleteAsync();
}
}
}
Loading