mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-05-05 21:46:28 +03:00
Add a new .editorconfig and reformat code (#76)
*I'll start working on features and bugfixes after this PR, I promise* very short summary: - no more braceless statements - braces are on new lines now - `sealed` on everything that can be `sealed` - no more awkwardly looking alignment of fields/parameters - no more `Service` suffix on service fields. yeah. - no more `else`s. who needs them? - code style is now enforced by CI --------- Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
4cb39a34b5
commit
84e730838b
39 changed files with 2917 additions and 623 deletions
|
@ -21,19 +21,21 @@ namespace Boyfriend.Commands;
|
|||
/// Handles the command to show information about this bot: /about.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class AboutCommandGroup : CommandGroup {
|
||||
private static readonly string[] Developers = { "Octol1ttle", "mctaylors", "neroduckale" };
|
||||
private readonly ICommandContext _context;
|
||||
private readonly GuildDataService _dataService;
|
||||
private readonly FeedbackService _feedbackService;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
public class AboutCommandGroup : CommandGroup
|
||||
{
|
||||
private static readonly string[] Developers = { "Octol1ttle", "mctaylors", "neroduckale" };
|
||||
private readonly ICommandContext _context;
|
||||
private readonly FeedbackService _feedback;
|
||||
private readonly GuildDataService _guildData;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
|
||||
public AboutCommandGroup(
|
||||
ICommandContext context, GuildDataService dataService,
|
||||
FeedbackService feedbackService, IDiscordRestUserAPI userApi) {
|
||||
ICommandContext context, GuildDataService guildData,
|
||||
FeedbackService feedback, IDiscordRestUserAPI userApi)
|
||||
{
|
||||
_context = context;
|
||||
_dataService = dataService;
|
||||
_feedbackService = feedbackService;
|
||||
_guildData = guildData;
|
||||
_feedback = feedback;
|
||||
_userApi = userApi;
|
||||
}
|
||||
|
||||
|
@ -48,24 +50,32 @@ public class AboutCommandGroup : CommandGroup {
|
|||
[RequireContext(ChannelContext.Guild)]
|
||||
[Description("Shows Boyfriend's developers")]
|
||||
[UsedImplicitly]
|
||||
public async Task<Result> ExecuteAboutAsync() {
|
||||
public async Task<Result> ExecuteAboutAsync()
|
||||
{
|
||||
if (!_context.TryGetContextIDs(out var guildId, out _, out _))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
||||
}
|
||||
|
||||
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
||||
if (!currentUserResult.IsDefined(out var currentUser))
|
||||
{
|
||||
return Result.FromError(currentUserResult);
|
||||
}
|
||||
|
||||
var cfg = await _dataService.GetSettings(guildId, CancellationToken);
|
||||
var cfg = await _guildData.GetSettings(guildId, CancellationToken);
|
||||
Messages.Culture = GuildSettings.Language.Get(cfg);
|
||||
|
||||
return await SendAboutBotAsync(currentUser, CancellationToken);
|
||||
}
|
||||
|
||||
private async Task<Result> SendAboutBotAsync(IUser currentUser, CancellationToken ct = default) {
|
||||
private async Task<Result> SendAboutBotAsync(IUser currentUser, CancellationToken ct = default)
|
||||
{
|
||||
var builder = new StringBuilder().AppendLine(Markdown.Bold(Messages.AboutTitleDevelopers));
|
||||
foreach (var dev in Developers)
|
||||
{
|
||||
builder.AppendLine($"@{dev} — {$"AboutDeveloper@{dev}".Localized()}");
|
||||
}
|
||||
|
||||
builder.AppendLine()
|
||||
.AppendLine(Markdown.Bold(Messages.AboutTitleWiki))
|
||||
|
@ -77,6 +87,6 @@ public class AboutCommandGroup : CommandGroup {
|
|||
.WithImageUrl("https://cdn.upload.systems/uploads/JFAaX5vr.png")
|
||||
.Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,23 +22,25 @@ namespace Boyfriend.Commands;
|
|||
/// Handles commands related to ban management: /ban and /unban.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class BanCommandGroup : CommandGroup {
|
||||
public class BanCommandGroup : CommandGroup
|
||||
{
|
||||
private readonly IDiscordRestChannelAPI _channelApi;
|
||||
private readonly ICommandContext _context;
|
||||
private readonly GuildDataService _dataService;
|
||||
private readonly FeedbackService _feedbackService;
|
||||
private readonly IDiscordRestGuildAPI _guildApi;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
private readonly UtilityService _utility;
|
||||
private readonly ICommandContext _context;
|
||||
private readonly FeedbackService _feedback;
|
||||
private readonly IDiscordRestGuildAPI _guildApi;
|
||||
private readonly GuildDataService _guildData;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
private readonly UtilityService _utility;
|
||||
|
||||
public BanCommandGroup(
|
||||
ICommandContext context, IDiscordRestChannelAPI channelApi, GuildDataService dataService,
|
||||
FeedbackService feedbackService, IDiscordRestGuildAPI guildApi, IDiscordRestUserAPI userApi,
|
||||
UtilityService utility) {
|
||||
ICommandContext context, IDiscordRestChannelAPI channelApi, GuildDataService guildData,
|
||||
FeedbackService feedback, IDiscordRestGuildAPI guildApi, IDiscordRestUserAPI userApi,
|
||||
UtilityService utility)
|
||||
{
|
||||
_context = context;
|
||||
_channelApi = channelApi;
|
||||
_dataService = dataService;
|
||||
_feedbackService = feedbackService;
|
||||
_guildData = guildData;
|
||||
_feedback = feedback;
|
||||
_guildApi = guildApi;
|
||||
_userApi = userApi;
|
||||
_utility = utility;
|
||||
|
@ -67,23 +69,35 @@ public class BanCommandGroup : CommandGroup {
|
|||
[Description("Ban user")]
|
||||
[UsedImplicitly]
|
||||
public async Task<Result> ExecuteBanAsync(
|
||||
[Description("User to ban")] IUser target,
|
||||
[Description("Ban reason")] string reason,
|
||||
[Description("Ban duration")] TimeSpan? duration = null) {
|
||||
[Description("User to ban")] IUser target,
|
||||
[Description("Ban reason")] string reason,
|
||||
[Description("Ban duration")] TimeSpan? duration = null)
|
||||
{
|
||||
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
||||
}
|
||||
|
||||
// The current user's avatar is used when sending error messages
|
||||
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
||||
if (!currentUserResult.IsDefined(out var currentUser))
|
||||
{
|
||||
return Result.FromError(currentUserResult);
|
||||
}
|
||||
|
||||
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
|
||||
if (!userResult.IsDefined(out var user))
|
||||
{
|
||||
return Result.FromError(userResult);
|
||||
}
|
||||
|
||||
var guildResult = await _guildApi.GetGuildAsync(guildId, ct: CancellationToken);
|
||||
if (!guildResult.IsDefined(out var guild))
|
||||
{
|
||||
return Result.FromError(guildResult);
|
||||
}
|
||||
|
||||
var data = await _dataService.GetData(guild.ID, CancellationToken);
|
||||
var data = await _guildData.GetData(guild.ID, CancellationToken);
|
||||
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
||||
|
||||
return await BanUserAsync(
|
||||
|
@ -91,39 +105,48 @@ public class BanCommandGroup : CommandGroup {
|
|||
}
|
||||
|
||||
private async Task<Result> BanUserAsync(
|
||||
IUser target, string reason, TimeSpan? duration, IGuild guild, GuildData data, Snowflake channelId,
|
||||
IUser user, IUser currentUser, CancellationToken ct = default) {
|
||||
IUser target, string reason, TimeSpan? duration, IGuild guild, GuildData data, Snowflake channelId,
|
||||
IUser user, IUser currentUser, CancellationToken ct = default)
|
||||
{
|
||||
var existingBanResult = await _guildApi.GetGuildBanAsync(guild.ID, target.ID, ct);
|
||||
if (existingBanResult.IsDefined()) {
|
||||
if (existingBanResult.IsDefined())
|
||||
{
|
||||
var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.UserAlreadyBanned, currentUser)
|
||||
.WithColour(ColorsList.Red).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(failedEmbed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct);
|
||||
}
|
||||
|
||||
var interactionResult
|
||||
= await _utility.CheckInteractionsAsync(guild.ID, user.ID, target.ID, "Ban", ct);
|
||||
if (!interactionResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(interactionResult);
|
||||
}
|
||||
|
||||
if (interactionResult.Entity is not null) {
|
||||
if (interactionResult.Entity is not null)
|
||||
{
|
||||
var errorEmbed = new EmbedBuilder().WithSmallTitle(interactionResult.Entity, currentUser)
|
||||
.WithColour(ColorsList.Red).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(errorEmbed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(errorEmbed, ct);
|
||||
}
|
||||
|
||||
var builder = new StringBuilder().AppendLine(string.Format(Messages.DescriptionActionReason, reason));
|
||||
if (duration is not null)
|
||||
{
|
||||
builder.Append(
|
||||
string.Format(
|
||||
Messages.DescriptionActionExpiresAt,
|
||||
Markdown.Timestamp(DateTimeOffset.UtcNow.Add(duration.Value))));
|
||||
}
|
||||
|
||||
var title = string.Format(Messages.UserBanned, target.GetTag());
|
||||
var description = builder.ToString();
|
||||
|
||||
var dmChannelResult = await _userApi.CreateDMAsync(target.ID, ct);
|
||||
if (dmChannelResult.IsDefined(out var dmChannel)) {
|
||||
if (dmChannelResult.IsDefined(out var dmChannel))
|
||||
{
|
||||
var dmEmbed = new EmbedBuilder().WithGuildTitle(guild)
|
||||
.WithTitle(Messages.YouWereBanned)
|
||||
.WithDescription(description)
|
||||
|
@ -133,7 +156,10 @@ public class BanCommandGroup : CommandGroup {
|
|||
.Build();
|
||||
|
||||
if (!dmEmbed.IsDefined(out var dmBuilt))
|
||||
{
|
||||
return Result.FromError(dmEmbed);
|
||||
}
|
||||
|
||||
await _channelApi.CreateMessageAsync(dmChannel.ID, embeds: new[] { dmBuilt }, ct: ct);
|
||||
}
|
||||
|
||||
|
@ -141,7 +167,10 @@ public class BanCommandGroup : CommandGroup {
|
|||
guild.ID, target.ID, reason: $"({user.GetTag()}) {reason}".EncodeHeader(),
|
||||
ct: ct);
|
||||
if (!banResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(banResult.Error);
|
||||
}
|
||||
|
||||
var memberData = data.GetMemberData(target.ID);
|
||||
memberData.BannedUntil
|
||||
= duration is not null ? DateTimeOffset.UtcNow.Add(duration.Value) : DateTimeOffset.MaxValue;
|
||||
|
@ -154,9 +183,11 @@ public class BanCommandGroup : CommandGroup {
|
|||
var logResult = _utility.LogActionAsync(
|
||||
data.Settings, channelId, user, title, description, target, ColorsList.Red, ct: ct);
|
||||
if (!logResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(logResult.Error);
|
||||
}
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -172,7 +203,7 @@ public class BanCommandGroup : CommandGroup {
|
|||
/// was unbanned and vice-versa.
|
||||
/// </returns>
|
||||
/// <seealso cref="ExecuteBanAsync" />
|
||||
/// <seealso cref="GuildUpdateService.TickGuildAsync"/>
|
||||
/// <seealso cref="GuildUpdateService.TickGuildAsync" />
|
||||
[Command("unban")]
|
||||
[DiscordDefaultMemberPermissions(DiscordPermission.BanMembers)]
|
||||
[DiscordDefaultDMPermission(false)]
|
||||
|
@ -182,20 +213,29 @@ public class BanCommandGroup : CommandGroup {
|
|||
[Description("Unban user")]
|
||||
[UsedImplicitly]
|
||||
public async Task<Result> ExecuteUnban(
|
||||
[Description("User to unban")] IUser target,
|
||||
[Description("Unban reason")] string reason) {
|
||||
[Description("User to unban")] IUser target,
|
||||
[Description("Unban reason")] string reason)
|
||||
{
|
||||
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
||||
}
|
||||
|
||||
// The current user's avatar is used when sending error messages
|
||||
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
||||
if (!currentUserResult.IsDefined(out var currentUser))
|
||||
{
|
||||
return Result.FromError(currentUserResult);
|
||||
}
|
||||
|
||||
// Needed to get the tag and avatar
|
||||
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
|
||||
if (!userResult.IsDefined(out var user))
|
||||
{
|
||||
return Result.FromError(userResult);
|
||||
}
|
||||
|
||||
var data = await _dataService.GetData(guildId, CancellationToken);
|
||||
var data = await _guildData.GetData(guildId, CancellationToken);
|
||||
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
||||
|
||||
return await UnbanUserAsync(
|
||||
|
@ -203,21 +243,25 @@ public class BanCommandGroup : CommandGroup {
|
|||
}
|
||||
|
||||
private async Task<Result> UnbanUserAsync(
|
||||
IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId, IUser user,
|
||||
IUser currentUser, CancellationToken ct = default) {
|
||||
IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId, IUser user,
|
||||
IUser currentUser, CancellationToken ct = default)
|
||||
{
|
||||
var existingBanResult = await _guildApi.GetGuildBanAsync(guildId, target.ID, ct);
|
||||
if (!existingBanResult.IsDefined()) {
|
||||
if (!existingBanResult.IsDefined())
|
||||
{
|
||||
var errorEmbed = new EmbedBuilder().WithSmallTitle(Messages.UserNotBanned, currentUser)
|
||||
.WithColour(ColorsList.Red).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(errorEmbed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(errorEmbed, ct);
|
||||
}
|
||||
|
||||
var unbanResult = await _guildApi.RemoveGuildBanAsync(
|
||||
guildId, target.ID, $"({user.GetTag()}) {reason}".EncodeHeader(),
|
||||
ct);
|
||||
if (!unbanResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(unbanResult.Error);
|
||||
}
|
||||
|
||||
var embed = new EmbedBuilder().WithSmallTitle(
|
||||
string.Format(Messages.UserUnbanned, target.GetTag()), target)
|
||||
|
@ -228,8 +272,10 @@ public class BanCommandGroup : CommandGroup {
|
|||
var logResult = _utility.LogActionAsync(
|
||||
data.Settings, channelId, user, title, description, target, ColorsList.Green, ct: ct);
|
||||
if (!logResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(logResult.Error);
|
||||
}
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,21 +22,23 @@ namespace Boyfriend.Commands;
|
|||
/// Handles the command to clear messages in a channel: /clear.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class ClearCommandGroup : CommandGroup {
|
||||
public class ClearCommandGroup : CommandGroup
|
||||
{
|
||||
private readonly IDiscordRestChannelAPI _channelApi;
|
||||
private readonly ICommandContext _context;
|
||||
private readonly GuildDataService _dataService;
|
||||
private readonly FeedbackService _feedbackService;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
private readonly UtilityService _utility;
|
||||
private readonly ICommandContext _context;
|
||||
private readonly FeedbackService _feedback;
|
||||
private readonly GuildDataService _guildData;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
private readonly UtilityService _utility;
|
||||
|
||||
public ClearCommandGroup(
|
||||
IDiscordRestChannelAPI channelApi, ICommandContext context, GuildDataService dataService,
|
||||
FeedbackService feedbackService, IDiscordRestUserAPI userApi, UtilityService utility) {
|
||||
IDiscordRestChannelAPI channelApi, ICommandContext context, GuildDataService guildData,
|
||||
FeedbackService feedback, IDiscordRestUserAPI userApi, UtilityService utility)
|
||||
{
|
||||
_channelApi = channelApi;
|
||||
_context = context;
|
||||
_dataService = dataService;
|
||||
_feedbackService = feedbackService;
|
||||
_guildData = guildData;
|
||||
_feedback = feedback;
|
||||
_userApi = userApi;
|
||||
_utility = utility;
|
||||
}
|
||||
|
@ -59,34 +61,47 @@ public class ClearCommandGroup : CommandGroup {
|
|||
[UsedImplicitly]
|
||||
public async Task<Result> ExecuteClear(
|
||||
[Description("Number of messages to remove (2-100)")] [MinValue(2)] [MaxValue(100)]
|
||||
int amount) {
|
||||
int amount)
|
||||
{
|
||||
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
||||
}
|
||||
|
||||
var messagesResult = await _channelApi.GetChannelMessagesAsync(
|
||||
channelId, limit: amount + 1, ct: CancellationToken);
|
||||
if (!messagesResult.IsDefined(out var messages))
|
||||
{
|
||||
return Result.FromError(messagesResult);
|
||||
}
|
||||
|
||||
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
|
||||
if (!userResult.IsDefined(out var user))
|
||||
{
|
||||
return Result.FromError(userResult);
|
||||
}
|
||||
|
||||
// The current user's avatar is used when sending messages
|
||||
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
||||
if (!currentUserResult.IsDefined(out var currentUser))
|
||||
{
|
||||
return Result.FromError(currentUserResult);
|
||||
}
|
||||
|
||||
var data = await _dataService.GetData(guildId, CancellationToken);
|
||||
var data = await _guildData.GetData(guildId, CancellationToken);
|
||||
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
||||
|
||||
return await ClearMessagesAsync(amount, data, channelId, messages, user, currentUser, CancellationToken);
|
||||
}
|
||||
|
||||
private async Task<Result> ClearMessagesAsync(
|
||||
int amount, GuildData data, Snowflake channelId, IReadOnlyList<IMessage> messages,
|
||||
IUser user, IUser currentUser, CancellationToken ct = default) {
|
||||
int amount, GuildData data, Snowflake channelId, IReadOnlyList<IMessage> messages,
|
||||
IUser user, IUser currentUser, CancellationToken ct = default)
|
||||
{
|
||||
var idList = new List<Snowflake>(messages.Count);
|
||||
var builder = new StringBuilder().AppendLine(Mention.Channel(channelId)).AppendLine();
|
||||
for (var i = messages.Count - 1; i >= 1; i--) { // '>= 1' to skip last message ('Boyfriend is thinking...')
|
||||
for (var i = messages.Count - 1; i >= 1; i--) // '>= 1' to skip last message ('Boyfriend is thinking...')
|
||||
{
|
||||
var message = messages[i];
|
||||
idList.Add(message.ID);
|
||||
builder.AppendLine(string.Format(Messages.MessageFrom, Mention.User(message.Author)));
|
||||
|
@ -99,16 +114,20 @@ public class ClearCommandGroup : CommandGroup {
|
|||
var deleteResult = await _channelApi.BulkDeleteMessagesAsync(
|
||||
channelId, idList, user.GetTag().EncodeHeader(), ct);
|
||||
if (!deleteResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(deleteResult.Error);
|
||||
}
|
||||
|
||||
var logResult = _utility.LogActionAsync(
|
||||
data.Settings, channelId, user, title, description, currentUser, ColorsList.Red, false, ct);
|
||||
if (!logResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(logResult.Error);
|
||||
}
|
||||
|
||||
var embed = new EmbedBuilder().WithSmallTitle(title, currentUser)
|
||||
.WithColour(ColorsList.Green).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,10 +11,12 @@ namespace Boyfriend.Commands.Events;
|
|||
/// Handles error logging for slash command groups.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent {
|
||||
public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent
|
||||
{
|
||||
private readonly ILogger<ErrorLoggingPostExecutionEvent> _logger;
|
||||
|
||||
public ErrorLoggingPostExecutionEvent(ILogger<ErrorLoggingPostExecutionEvent> logger) {
|
||||
public ErrorLoggingPostExecutionEvent(ILogger<ErrorLoggingPostExecutionEvent> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
@ -27,11 +29,15 @@ public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent {
|
|||
/// <param name="ct">The cancellation token for this operation. Unused.</param>
|
||||
/// <returns>A result which has succeeded.</returns>
|
||||
public Task<Result> AfterExecutionAsync(
|
||||
ICommandContext context, IResult commandResult, CancellationToken ct = default) {
|
||||
if (!commandResult.IsSuccess && !commandResult.Error.IsUserOrEnvironmentError()) {
|
||||
ICommandContext context, IResult commandResult, CancellationToken ct = default)
|
||||
{
|
||||
if (!commandResult.IsSuccess && !commandResult.Error.IsUserOrEnvironmentError())
|
||||
{
|
||||
_logger.LogWarning("Error in slash command execution.\n{ErrorMessage}", commandResult.Error.Message);
|
||||
if (commandResult.Error is ExceptionError exerr)
|
||||
{
|
||||
_logger.LogError(exerr.Exception, "An exception has been thrown");
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult(Result.FromSuccess());
|
||||
|
|
|
@ -11,10 +11,12 @@ namespace Boyfriend.Commands.Events;
|
|||
/// Handles error logging for slash commands that couldn't be successfully prepared.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class LoggingPreparationErrorEvent : IPreparationErrorEvent {
|
||||
public class LoggingPreparationErrorEvent : IPreparationErrorEvent
|
||||
{
|
||||
private readonly ILogger<LoggingPreparationErrorEvent> _logger;
|
||||
|
||||
public LoggingPreparationErrorEvent(ILogger<LoggingPreparationErrorEvent> logger) {
|
||||
public LoggingPreparationErrorEvent(ILogger<LoggingPreparationErrorEvent> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
@ -27,11 +29,15 @@ public class LoggingPreparationErrorEvent : IPreparationErrorEvent {
|
|||
/// <param name="ct">The cancellation token for this operation. Unused.</param>
|
||||
/// <returns>A result which has succeeded.</returns>
|
||||
public Task<Result> PreparationFailed(
|
||||
IOperationContext context, IResult preparationResult, CancellationToken ct = default) {
|
||||
if (!preparationResult.IsSuccess && !preparationResult.Error.IsUserOrEnvironmentError()) {
|
||||
IOperationContext context, IResult preparationResult, CancellationToken ct = default)
|
||||
{
|
||||
if (!preparationResult.IsSuccess && !preparationResult.Error.IsUserOrEnvironmentError())
|
||||
{
|
||||
_logger.LogWarning("Error in slash command preparation.\n{ErrorMessage}", preparationResult.Error.Message);
|
||||
if (preparationResult.Error is ExceptionError exerr)
|
||||
{
|
||||
_logger.LogError(exerr.Exception, "An exception has been thrown");
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult(Result.FromSuccess());
|
||||
|
|
|
@ -20,23 +20,25 @@ namespace Boyfriend.Commands;
|
|||
/// Handles the command to kick members of a guild: /kick.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class KickCommandGroup : CommandGroup {
|
||||
public class KickCommandGroup : CommandGroup
|
||||
{
|
||||
private readonly IDiscordRestChannelAPI _channelApi;
|
||||
private readonly ICommandContext _context;
|
||||
private readonly GuildDataService _dataService;
|
||||
private readonly FeedbackService _feedbackService;
|
||||
private readonly IDiscordRestGuildAPI _guildApi;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
private readonly UtilityService _utility;
|
||||
private readonly ICommandContext _context;
|
||||
private readonly FeedbackService _feedback;
|
||||
private readonly IDiscordRestGuildAPI _guildApi;
|
||||
private readonly GuildDataService _guildData;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
private readonly UtilityService _utility;
|
||||
|
||||
public KickCommandGroup(
|
||||
ICommandContext context, IDiscordRestChannelAPI channelApi, GuildDataService dataService,
|
||||
FeedbackService feedbackService, IDiscordRestGuildAPI guildApi, IDiscordRestUserAPI userApi,
|
||||
UtilityService utility) {
|
||||
ICommandContext context, IDiscordRestChannelAPI channelApi, GuildDataService guildData,
|
||||
FeedbackService feedback, IDiscordRestGuildAPI guildApi, IDiscordRestUserAPI userApi,
|
||||
UtilityService utility)
|
||||
{
|
||||
_context = context;
|
||||
_channelApi = channelApi;
|
||||
_dataService = dataService;
|
||||
_feedbackService = feedbackService;
|
||||
_guildData = guildData;
|
||||
_feedback = feedback;
|
||||
_guildApi = guildApi;
|
||||
_userApi = userApi;
|
||||
_utility = utility;
|
||||
|
@ -63,30 +65,43 @@ public class KickCommandGroup : CommandGroup {
|
|||
[Description("Kick member")]
|
||||
[UsedImplicitly]
|
||||
public async Task<Result> ExecuteKick(
|
||||
[Description("Member to kick")] IUser target,
|
||||
[Description("Kick reason")] string reason) {
|
||||
[Description("Member to kick")] IUser target,
|
||||
[Description("Kick reason")] string reason)
|
||||
{
|
||||
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
||||
}
|
||||
|
||||
// The current user's avatar is used when sending error messages
|
||||
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
||||
if (!currentUserResult.IsDefined(out var currentUser))
|
||||
{
|
||||
return Result.FromError(currentUserResult);
|
||||
}
|
||||
|
||||
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
|
||||
if (!userResult.IsDefined(out var user))
|
||||
{
|
||||
return Result.FromError(userResult);
|
||||
}
|
||||
|
||||
var guildResult = await _guildApi.GetGuildAsync(guildId, ct: CancellationToken);
|
||||
if (!guildResult.IsDefined(out var guild))
|
||||
{
|
||||
return Result.FromError(guildResult);
|
||||
}
|
||||
|
||||
var data = await _dataService.GetData(guildId, CancellationToken);
|
||||
var data = await _guildData.GetData(guildId, CancellationToken);
|
||||
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
||||
|
||||
var memberResult = await _guildApi.GetGuildMemberAsync(guildId, target.ID, CancellationToken);
|
||||
if (!memberResult.IsSuccess) {
|
||||
if (!memberResult.IsSuccess)
|
||||
{
|
||||
var embed = new EmbedBuilder().WithSmallTitle(Messages.UserNotFoundShort, currentUser)
|
||||
.WithColour(ColorsList.Red).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, CancellationToken);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, CancellationToken);
|
||||
}
|
||||
|
||||
return await KickUserAsync(target, reason, guild, channelId, data, user, currentUser, CancellationToken);
|
||||
|
@ -94,21 +109,26 @@ public class KickCommandGroup : CommandGroup {
|
|||
|
||||
private async Task<Result> KickUserAsync(
|
||||
IUser target, string reason, IGuild guild, Snowflake channelId, GuildData data, IUser user, IUser currentUser,
|
||||
CancellationToken ct = default) {
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var interactionResult
|
||||
= await _utility.CheckInteractionsAsync(guild.ID, user.ID, target.ID, "Kick", ct);
|
||||
if (!interactionResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(interactionResult);
|
||||
}
|
||||
|
||||
if (interactionResult.Entity is not null) {
|
||||
if (interactionResult.Entity is not null)
|
||||
{
|
||||
var failedEmbed = new EmbedBuilder().WithSmallTitle(interactionResult.Entity, currentUser)
|
||||
.WithColour(ColorsList.Red).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(failedEmbed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct);
|
||||
}
|
||||
|
||||
var dmChannelResult = await _userApi.CreateDMAsync(target.ID, ct);
|
||||
if (dmChannelResult.IsDefined(out var dmChannel)) {
|
||||
if (dmChannelResult.IsDefined(out var dmChannel))
|
||||
{
|
||||
var dmEmbed = new EmbedBuilder().WithGuildTitle(guild)
|
||||
.WithTitle(Messages.YouWereKicked)
|
||||
.WithDescription(string.Format(Messages.DescriptionActionReason, reason))
|
||||
|
@ -118,7 +138,10 @@ public class KickCommandGroup : CommandGroup {
|
|||
.Build();
|
||||
|
||||
if (!dmEmbed.IsDefined(out var dmBuilt))
|
||||
{
|
||||
return Result.FromError(dmEmbed);
|
||||
}
|
||||
|
||||
await _channelApi.CreateMessageAsync(dmChannel.ID, embeds: new[] { dmBuilt }, ct: ct);
|
||||
}
|
||||
|
||||
|
@ -126,7 +149,10 @@ public class KickCommandGroup : CommandGroup {
|
|||
guild.ID, target.ID, $"({user.GetTag()}) {reason}".EncodeHeader(),
|
||||
ct);
|
||||
if (!kickResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(kickResult.Error);
|
||||
}
|
||||
|
||||
data.GetMemberData(target.ID).Roles.Clear();
|
||||
|
||||
var title = string.Format(Messages.UserKicked, target.GetTag());
|
||||
|
@ -134,12 +160,14 @@ public class KickCommandGroup : CommandGroup {
|
|||
var logResult = _utility.LogActionAsync(
|
||||
data.Settings, channelId, user, title, description, target, ColorsList.Red, ct: ct);
|
||||
if (!logResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(logResult.Error);
|
||||
}
|
||||
|
||||
var embed = new EmbedBuilder().WithSmallTitle(
|
||||
string.Format(Messages.UserKicked, target.GetTag()), target)
|
||||
.WithColour(ColorsList.Green).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,20 +22,22 @@ namespace Boyfriend.Commands;
|
|||
/// Handles commands related to mute management: /mute and /unmute.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class MuteCommandGroup : CommandGroup {
|
||||
private readonly ICommandContext _context;
|
||||
private readonly GuildDataService _dataService;
|
||||
private readonly FeedbackService _feedbackService;
|
||||
public class MuteCommandGroup : CommandGroup
|
||||
{
|
||||
private readonly ICommandContext _context;
|
||||
private readonly FeedbackService _feedback;
|
||||
private readonly IDiscordRestGuildAPI _guildApi;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
private readonly UtilityService _utility;
|
||||
private readonly GuildDataService _guildData;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
private readonly UtilityService _utility;
|
||||
|
||||
public MuteCommandGroup(
|
||||
ICommandContext context, GuildDataService dataService, FeedbackService feedbackService,
|
||||
IDiscordRestGuildAPI guildApi, IDiscordRestUserAPI userApi, UtilityService utility) {
|
||||
ICommandContext context, GuildDataService guildData, FeedbackService feedback,
|
||||
IDiscordRestGuildAPI guildApi, IDiscordRestUserAPI userApi, UtilityService utility)
|
||||
{
|
||||
_context = context;
|
||||
_dataService = dataService;
|
||||
_feedbackService = feedbackService;
|
||||
_guildData = guildData;
|
||||
_feedback = feedback;
|
||||
_guildApi = guildApi;
|
||||
_userApi = userApi;
|
||||
_utility = utility;
|
||||
|
@ -64,30 +66,38 @@ public class MuteCommandGroup : CommandGroup {
|
|||
[Description("Mute member")]
|
||||
[UsedImplicitly]
|
||||
public async Task<Result> ExecuteMute(
|
||||
[Description("Member to mute")] IUser target,
|
||||
[Description("Mute reason")] string reason,
|
||||
[Description("Mute duration")] TimeSpan duration) {
|
||||
[Description("Member to mute")] IUser target,
|
||||
[Description("Mute reason")] string reason,
|
||||
[Description("Mute duration")] TimeSpan duration)
|
||||
{
|
||||
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
||||
}
|
||||
|
||||
// The current user's avatar is used when sending error messages
|
||||
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
||||
if (!currentUserResult.IsDefined(out var currentUser))
|
||||
{
|
||||
return Result.FromError(currentUserResult);
|
||||
}
|
||||
|
||||
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
|
||||
if (!userResult.IsDefined(out var user))
|
||||
{
|
||||
return Result.FromError(userResult);
|
||||
}
|
||||
|
||||
var data = await _dataService.GetData(guildId, CancellationToken);
|
||||
var data = await _guildData.GetData(guildId, CancellationToken);
|
||||
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
||||
|
||||
var memberResult = await _guildApi.GetGuildMemberAsync(guildId, target.ID, CancellationToken);
|
||||
if (!memberResult.IsSuccess) {
|
||||
if (!memberResult.IsSuccess)
|
||||
{
|
||||
var embed = new EmbedBuilder().WithSmallTitle(Messages.UserNotFoundShort, currentUser)
|
||||
.WithColour(ColorsList.Red).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, CancellationToken);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, CancellationToken);
|
||||
}
|
||||
|
||||
return await MuteUserAsync(
|
||||
|
@ -95,19 +105,23 @@ public class MuteCommandGroup : CommandGroup {
|
|||
}
|
||||
|
||||
private async Task<Result> MuteUserAsync(
|
||||
IUser target, string reason, TimeSpan duration, Snowflake guildId, GuildData data, Snowflake channelId,
|
||||
IUser user, IUser currentUser, CancellationToken ct = default) {
|
||||
IUser target, string reason, TimeSpan duration, Snowflake guildId, GuildData data, Snowflake channelId,
|
||||
IUser user, IUser currentUser, CancellationToken ct = default)
|
||||
{
|
||||
var interactionResult
|
||||
= await _utility.CheckInteractionsAsync(
|
||||
guildId, user.ID, target.ID, "Mute", ct);
|
||||
if (!interactionResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(interactionResult);
|
||||
}
|
||||
|
||||
if (interactionResult.Entity is not null) {
|
||||
if (interactionResult.Entity is not null)
|
||||
{
|
||||
var failedEmbed = new EmbedBuilder().WithSmallTitle(interactionResult.Entity, currentUser)
|
||||
.WithColour(ColorsList.Red).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(failedEmbed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct);
|
||||
}
|
||||
|
||||
var until = DateTimeOffset.UtcNow.Add(duration); // >:)
|
||||
|
@ -115,7 +129,9 @@ public class MuteCommandGroup : CommandGroup {
|
|||
guildId, target.ID, reason: $"({user.GetTag()}) {reason}".EncodeHeader(),
|
||||
communicationDisabledUntil: until, ct: ct);
|
||||
if (!muteResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(muteResult.Error);
|
||||
}
|
||||
|
||||
var title = string.Format(Messages.UserMuted, target.GetTag());
|
||||
var description = new StringBuilder().AppendLine(string.Format(Messages.DescriptionActionReason, reason))
|
||||
|
@ -126,13 +142,15 @@ public class MuteCommandGroup : CommandGroup {
|
|||
var logResult = _utility.LogActionAsync(
|
||||
data.Settings, channelId, user, title, description, target, ColorsList.Red, ct: ct);
|
||||
if (!logResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(logResult.Error);
|
||||
}
|
||||
|
||||
var embed = new EmbedBuilder().WithSmallTitle(
|
||||
string.Format(Messages.UserMuted, target.GetTag()), target)
|
||||
.WithColour(ColorsList.Green).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -148,7 +166,7 @@ public class MuteCommandGroup : CommandGroup {
|
|||
/// was unmuted and vice-versa.
|
||||
/// </returns>
|
||||
/// <seealso cref="ExecuteMute" />
|
||||
/// <seealso cref="GuildUpdateService.TickGuildAsync"/>
|
||||
/// <seealso cref="GuildUpdateService.TickGuildAsync" />
|
||||
[Command("unmute", "размут")]
|
||||
[DiscordDefaultMemberPermissions(DiscordPermission.ModerateMembers)]
|
||||
[DiscordDefaultDMPermission(false)]
|
||||
|
@ -158,30 +176,38 @@ public class MuteCommandGroup : CommandGroup {
|
|||
[Description("Unmute member")]
|
||||
[UsedImplicitly]
|
||||
public async Task<Result> ExecuteUnmute(
|
||||
[Description("Member to unmute")] IUser target,
|
||||
[Description("Unmute reason")] string reason) {
|
||||
[Description("Member to unmute")] IUser target,
|
||||
[Description("Unmute reason")] string reason)
|
||||
{
|
||||
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
||||
}
|
||||
|
||||
// The current user's avatar is used when sending error messages
|
||||
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
||||
if (!currentUserResult.IsDefined(out var currentUser))
|
||||
{
|
||||
return Result.FromError(currentUserResult);
|
||||
}
|
||||
|
||||
// Needed to get the tag and avatar
|
||||
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
|
||||
if (!userResult.IsDefined(out var user))
|
||||
{
|
||||
return Result.FromError(userResult);
|
||||
}
|
||||
|
||||
var data = await _dataService.GetData(guildId, CancellationToken);
|
||||
var data = await _guildData.GetData(guildId, CancellationToken);
|
||||
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
||||
|
||||
var memberResult = await _guildApi.GetGuildMemberAsync(guildId, target.ID, CancellationToken);
|
||||
if (!memberResult.IsSuccess) {
|
||||
if (!memberResult.IsSuccess)
|
||||
{
|
||||
var embed = new EmbedBuilder().WithSmallTitle(Messages.UserNotFoundShort, currentUser)
|
||||
.WithColour(ColorsList.Red).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, CancellationToken);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, CancellationToken);
|
||||
}
|
||||
|
||||
return await UnmuteUserAsync(
|
||||
|
@ -189,38 +215,46 @@ public class MuteCommandGroup : CommandGroup {
|
|||
}
|
||||
|
||||
private async Task<Result> UnmuteUserAsync(
|
||||
IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId, IUser user,
|
||||
IUser currentUser, CancellationToken ct = default) {
|
||||
IUser target, string reason, Snowflake guildId, GuildData data, Snowflake channelId, IUser user,
|
||||
IUser currentUser, CancellationToken ct = default)
|
||||
{
|
||||
var interactionResult
|
||||
= await _utility.CheckInteractionsAsync(
|
||||
guildId, user.ID, target.ID, "Unmute", ct);
|
||||
if (!interactionResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(interactionResult);
|
||||
}
|
||||
|
||||
if (interactionResult.Entity is not null) {
|
||||
if (interactionResult.Entity is not null)
|
||||
{
|
||||
var failedEmbed = new EmbedBuilder().WithSmallTitle(interactionResult.Entity, currentUser)
|
||||
.WithColour(ColorsList.Red).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(failedEmbed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct);
|
||||
}
|
||||
|
||||
var unmuteResult = await _guildApi.ModifyGuildMemberAsync(
|
||||
guildId, target.ID, $"({user.GetTag()}) {reason}".EncodeHeader(),
|
||||
communicationDisabledUntil: null, ct: ct);
|
||||
if (!unmuteResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(unmuteResult.Error);
|
||||
}
|
||||
|
||||
var title = string.Format(Messages.UserUnmuted, target.GetTag());
|
||||
var description = string.Format(Messages.DescriptionActionReason, reason);
|
||||
var logResult = _utility.LogActionAsync(
|
||||
data.Settings, channelId, user, title, description, target, ColorsList.Green, ct: ct);
|
||||
if (!logResult.IsSuccess)
|
||||
{
|
||||
return Result.FromError(logResult.Error);
|
||||
}
|
||||
|
||||
var embed = new EmbedBuilder().WithSmallTitle(
|
||||
string.Format(Messages.UserUnmuted, target.GetTag()), target)
|
||||
.WithColour(ColorsList.Green).Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,22 +21,24 @@ namespace Boyfriend.Commands;
|
|||
/// Handles the command to get the time taken for the gateway to respond to the last heartbeat: /ping
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class PingCommandGroup : CommandGroup {
|
||||
public class PingCommandGroup : CommandGroup
|
||||
{
|
||||
private readonly IDiscordRestChannelAPI _channelApi;
|
||||
private readonly DiscordGatewayClient _client;
|
||||
private readonly ICommandContext _context;
|
||||
private readonly GuildDataService _dataService;
|
||||
private readonly FeedbackService _feedbackService;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
private readonly DiscordGatewayClient _client;
|
||||
private readonly ICommandContext _context;
|
||||
private readonly FeedbackService _feedback;
|
||||
private readonly GuildDataService _guildData;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
|
||||
public PingCommandGroup(
|
||||
IDiscordRestChannelAPI channelApi, ICommandContext context, DiscordGatewayClient client,
|
||||
GuildDataService dataService, FeedbackService feedbackService, IDiscordRestUserAPI userApi) {
|
||||
IDiscordRestChannelAPI channelApi, ICommandContext context, DiscordGatewayClient client,
|
||||
GuildDataService guildData, FeedbackService feedback, IDiscordRestUserAPI userApi)
|
||||
{
|
||||
_channelApi = channelApi;
|
||||
_context = context;
|
||||
_client = client;
|
||||
_dataService = dataService;
|
||||
_feedbackService = feedbackService;
|
||||
_guildData = guildData;
|
||||
_feedback = feedback;
|
||||
_userApi = userApi;
|
||||
}
|
||||
|
||||
|
@ -51,29 +53,39 @@ public class PingCommandGroup : CommandGroup {
|
|||
[DiscordDefaultDMPermission(false)]
|
||||
[RequireContext(ChannelContext.Guild)]
|
||||
[UsedImplicitly]
|
||||
public async Task<Result> ExecutePingAsync() {
|
||||
public async Task<Result> ExecutePingAsync()
|
||||
{
|
||||
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out _))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
||||
}
|
||||
|
||||
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
||||
if (!currentUserResult.IsDefined(out var currentUser))
|
||||
{
|
||||
return Result.FromError(currentUserResult);
|
||||
}
|
||||
|
||||
var cfg = await _dataService.GetSettings(guildId, CancellationToken);
|
||||
var cfg = await _guildData.GetSettings(guildId, CancellationToken);
|
||||
Messages.Culture = GuildSettings.Language.Get(cfg);
|
||||
|
||||
return await SendLatencyAsync(channelId, currentUser, CancellationToken);
|
||||
}
|
||||
|
||||
private async Task<Result> SendLatencyAsync(
|
||||
Snowflake channelId, IUser currentUser, CancellationToken ct = default) {
|
||||
Snowflake channelId, IUser currentUser, CancellationToken ct = default)
|
||||
{
|
||||
var latency = _client.Latency.TotalMilliseconds;
|
||||
if (latency is 0) {
|
||||
if (latency is 0)
|
||||
{
|
||||
// No heartbeat has occurred, estimate latency from local time and "Boyfriend is thinking..." message
|
||||
var lastMessageResult = await _channelApi.GetChannelMessagesAsync(
|
||||
channelId, limit: 1, ct: ct);
|
||||
if (!lastMessageResult.IsDefined(out var lastMessage))
|
||||
{
|
||||
return Result.FromError(lastMessageResult);
|
||||
}
|
||||
|
||||
latency = DateTimeOffset.UtcNow.Subtract(lastMessage.Single().Timestamp).TotalMilliseconds;
|
||||
}
|
||||
|
||||
|
@ -84,6 +96,6 @@ public class PingCommandGroup : CommandGroup {
|
|||
.WithCurrentTimestamp()
|
||||
.Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,18 +21,20 @@ namespace Boyfriend.Commands;
|
|||
/// Handles the command to manage reminders: /remind
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class RemindCommandGroup : CommandGroup {
|
||||
private readonly ICommandContext _context;
|
||||
private readonly GuildDataService _dataService;
|
||||
private readonly FeedbackService _feedbackService;
|
||||
public class RemindCommandGroup : CommandGroup
|
||||
{
|
||||
private readonly ICommandContext _context;
|
||||
private readonly FeedbackService _feedback;
|
||||
private readonly GuildDataService _guildData;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
|
||||
public RemindCommandGroup(
|
||||
ICommandContext context, GuildDataService dataService, FeedbackService feedbackService,
|
||||
IDiscordRestUserAPI userApi) {
|
||||
ICommandContext context, GuildDataService guildData, FeedbackService feedback,
|
||||
IDiscordRestUserAPI userApi)
|
||||
{
|
||||
_context = context;
|
||||
_dataService = dataService;
|
||||
_feedbackService = feedbackService;
|
||||
_guildData = guildData;
|
||||
_feedback = feedback;
|
||||
_userApi = userApi;
|
||||
}
|
||||
|
||||
|
@ -50,27 +52,34 @@ public class RemindCommandGroup : CommandGroup {
|
|||
public async Task<Result> ExecuteReminderAsync(
|
||||
[Description("After what period of time mention the reminder")]
|
||||
TimeSpan @in,
|
||||
[Description("Reminder message")] string message) {
|
||||
[Description("Reminder message")] string message)
|
||||
{
|
||||
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
||||
}
|
||||
|
||||
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
|
||||
if (!userResult.IsDefined(out var user))
|
||||
{
|
||||
return Result.FromError(userResult);
|
||||
}
|
||||
|
||||
var data = await _dataService.GetData(guildId, CancellationToken);
|
||||
var data = await _guildData.GetData(guildId, CancellationToken);
|
||||
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
||||
|
||||
return await AddReminderAsync(@in, message, data, channelId, user, CancellationToken);
|
||||
}
|
||||
|
||||
private async Task<Result> AddReminderAsync(
|
||||
TimeSpan @in, string message, GuildData data,
|
||||
Snowflake channelId, IUser user, CancellationToken ct = default) {
|
||||
TimeSpan @in, string message, GuildData data,
|
||||
Snowflake channelId, IUser user, CancellationToken ct = default)
|
||||
{
|
||||
var remindAt = DateTimeOffset.UtcNow.Add(@in);
|
||||
|
||||
data.GetMemberData(user.ID).Reminders.Add(
|
||||
new Reminder {
|
||||
new Reminder
|
||||
{
|
||||
At = remindAt,
|
||||
Channel = channelId.Value,
|
||||
Text = message
|
||||
|
@ -81,6 +90,6 @@ public class RemindCommandGroup : CommandGroup {
|
|||
.WithColour(ColorsList.Green)
|
||||
.Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,8 +23,10 @@ namespace Boyfriend.Commands;
|
|||
/// Handles the commands to list and modify per-guild settings: /settings and /settings list.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class SettingsCommandGroup : CommandGroup {
|
||||
private static readonly IOption[] AllOptions = {
|
||||
public class SettingsCommandGroup : CommandGroup
|
||||
{
|
||||
private static readonly IOption[] AllOptions =
|
||||
{
|
||||
GuildSettings.Language,
|
||||
GuildSettings.WelcomeMessage,
|
||||
GuildSettings.ReceiveStartupMessages,
|
||||
|
@ -41,17 +43,18 @@ public class SettingsCommandGroup : CommandGroup {
|
|||
GuildSettings.EventEarlyNotificationOffset
|
||||
};
|
||||
|
||||
private readonly ICommandContext _context;
|
||||
private readonly GuildDataService _dataService;
|
||||
private readonly FeedbackService _feedbackService;
|
||||
private readonly ICommandContext _context;
|
||||
private readonly FeedbackService _feedback;
|
||||
private readonly GuildDataService _guildData;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
|
||||
public SettingsCommandGroup(
|
||||
ICommandContext context, GuildDataService dataService,
|
||||
FeedbackService feedbackService, IDiscordRestUserAPI userApi) {
|
||||
ICommandContext context, GuildDataService guildData,
|
||||
FeedbackService feedback, IDiscordRestUserAPI userApi)
|
||||
{
|
||||
_context = context;
|
||||
_dataService = dataService;
|
||||
_feedbackService = feedbackService;
|
||||
_guildData = guildData;
|
||||
_feedback = feedback;
|
||||
_userApi = userApi;
|
||||
}
|
||||
|
||||
|
@ -69,21 +72,28 @@ public class SettingsCommandGroup : CommandGroup {
|
|||
[Description("Shows settings list for this server")]
|
||||
[UsedImplicitly]
|
||||
public async Task<Result> ExecuteSettingsListAsync(
|
||||
[Description("Settings list page")] int page) {
|
||||
[Description("Settings list page")] int page)
|
||||
{
|
||||
if (!_context.TryGetContextIDs(out var guildId, out _, out _))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
||||
}
|
||||
|
||||
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
||||
if (!currentUserResult.IsDefined(out var currentUser))
|
||||
{
|
||||
return Result.FromError(currentUserResult);
|
||||
}
|
||||
|
||||
var cfg = await _dataService.GetSettings(guildId, CancellationToken);
|
||||
var cfg = await _guildData.GetSettings(guildId, CancellationToken);
|
||||
Messages.Culture = GuildSettings.Language.Get(cfg);
|
||||
|
||||
return await SendSettingsListAsync(cfg, currentUser, page, CancellationToken);
|
||||
}
|
||||
|
||||
private async Task<Result> SendSettingsListAsync(JsonNode cfg, IUser currentUser, int page, CancellationToken ct = default) {
|
||||
private async Task<Result> SendSettingsListAsync(JsonNode cfg, IUser currentUser, int page,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var description = new StringBuilder();
|
||||
var footer = new StringBuilder();
|
||||
|
||||
|
@ -93,38 +103,43 @@ public class SettingsCommandGroup : CommandGroup {
|
|||
var lastOptionOnPage = Math.Min(optionsPerPage * page, AllOptions.Length);
|
||||
var firstOptionOnPage = optionsPerPage * page - optionsPerPage;
|
||||
|
||||
if (firstOptionOnPage >= AllOptions.Length) {
|
||||
var embed = new EmbedBuilder().WithSmallTitle(Messages.PageNotFound, currentUser)
|
||||
if (firstOptionOnPage >= AllOptions.Length)
|
||||
{
|
||||
var errorEmbed = new EmbedBuilder().WithSmallTitle(Messages.PageNotFound, currentUser)
|
||||
.WithDescription(string.Format(Messages.PagesAllowed, Markdown.Bold(totalPages.ToString())))
|
||||
.WithColour(ColorsList.Red)
|
||||
.Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
||||
} else {
|
||||
footer.Append($"{Messages.Page} {page}/{totalPages} ");
|
||||
for (var i = 0; i < totalPages; i++) footer.Append(i + 1 == page ? "●" : "○");
|
||||
|
||||
for (var i = firstOptionOnPage; i < lastOptionOnPage; i++) {
|
||||
var optionName = AllOptions[i].Name;
|
||||
var optionValue = AllOptions[i].Display(cfg);
|
||||
|
||||
description.AppendLine($"- {$"Settings{optionName}".Localized()}")
|
||||
.Append($" - {Markdown.InlineCode(optionName)}: ")
|
||||
.AppendLine(optionValue);
|
||||
}
|
||||
|
||||
var embed = new EmbedBuilder().WithSmallTitle(Messages.SettingsListTitle, currentUser)
|
||||
.WithDescription(description.ToString())
|
||||
.WithColour(ColorsList.Default)
|
||||
.WithFooter(footer.ToString())
|
||||
.Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(errorEmbed, ct);
|
||||
}
|
||||
|
||||
footer.Append($"{Messages.Page} {page}/{totalPages} ");
|
||||
for (var i = 0; i < totalPages; i++)
|
||||
{
|
||||
footer.Append(i + 1 == page ? "●" : "○");
|
||||
}
|
||||
|
||||
for (var i = firstOptionOnPage; i < lastOptionOnPage; i++)
|
||||
{
|
||||
var optionName = AllOptions[i].Name;
|
||||
var optionValue = AllOptions[i].Display(cfg);
|
||||
|
||||
description.AppendLine($"- {$"Settings{optionName}".Localized()}")
|
||||
.Append($" - {Markdown.InlineCode(optionName)}: ")
|
||||
.AppendLine(optionValue);
|
||||
}
|
||||
|
||||
var embed = new EmbedBuilder().WithSmallTitle(Messages.SettingsListTitle, currentUser)
|
||||
.WithDescription(description.ToString())
|
||||
.WithColour(ColorsList.Default)
|
||||
.WithFooter(footer.ToString())
|
||||
.Build();
|
||||
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A slash command that modifies per-guild GuildSettings.
|
||||
/// A slash command that modifies per-guild GuildSettings.
|
||||
/// </summary>
|
||||
/// <param name="setting">The setting to modify.</param>
|
||||
/// <param name="value">The new value of the setting.</param>
|
||||
|
@ -139,33 +154,40 @@ public class SettingsCommandGroup : CommandGroup {
|
|||
public async Task<Result> ExecuteSettingsAsync(
|
||||
[Description("The setting whose value you want to change")]
|
||||
string setting,
|
||||
[Description("Setting value")] string value) {
|
||||
[Description("Setting value")] string value)
|
||||
{
|
||||
if (!_context.TryGetContextIDs(out var guildId, out _, out _))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
||||
}
|
||||
|
||||
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
||||
if (!currentUserResult.IsDefined(out var currentUser))
|
||||
{
|
||||
return Result.FromError(currentUserResult);
|
||||
}
|
||||
|
||||
var data = await _dataService.GetData(guildId, CancellationToken);
|
||||
var data = await _guildData.GetData(guildId, CancellationToken);
|
||||
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
||||
|
||||
return await EditSettingAsync(setting, value, data, currentUser, CancellationToken);
|
||||
}
|
||||
|
||||
private async Task<Result> EditSettingAsync(
|
||||
string setting, string value, GuildData data, IUser currentUser, CancellationToken ct = default) {
|
||||
string setting, string value, GuildData data, IUser currentUser, CancellationToken ct = default)
|
||||
{
|
||||
var option = AllOptions.Single(
|
||||
o => string.Equals(setting, o.Name, StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
var setResult = option.Set(data.Settings, value);
|
||||
if (!setResult.IsSuccess) {
|
||||
if (!setResult.IsSuccess)
|
||||
{
|
||||
var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.SettingNotChanged, currentUser)
|
||||
.WithDescription(setResult.Error.Message)
|
||||
.WithColour(ColorsList.Red)
|
||||
.Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(failedEmbed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct);
|
||||
}
|
||||
|
||||
var builder = new StringBuilder();
|
||||
|
@ -179,6 +201,6 @@ public class SettingsCommandGroup : CommandGroup {
|
|||
.WithColour(ColorsList.Green)
|
||||
.Build();
|
||||
|
||||
return await _feedbackService.SendContextualEmbedResultAsync(embed, ct);
|
||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue