1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 08:51:12 +03:00

Use Result.Success property instead of Result.FromSuccess() (#283)

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2024-03-21 20:55:34 +05:00 committed by GitHub
parent 309d900067
commit a80debf1b1
Signed by: GitHub
GPG key ID: B5690EEEBB952194
18 changed files with 59 additions and 54 deletions

View file

@ -20,8 +20,8 @@ namespace Octobot.Commands.Events;
[UsedImplicitly]
public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent
{
private readonly ILogger<ErrorLoggingPostExecutionEvent> _logger;
private readonly IFeedbackService _feedback;
private readonly ILogger<ErrorLoggingPostExecutionEvent> _logger;
private readonly IDiscordRestUserAPI _userApi;
public ErrorLoggingPostExecutionEvent(ILogger<ErrorLoggingPostExecutionEvent> logger, IFeedbackService feedback,
@ -53,7 +53,7 @@ public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent
if (result.IsSuccess)
{
return Result.FromSuccess();
return Result.Success;
}
var botResult = await _userApi.GetCurrentUserAsync(ct);

View file

@ -33,6 +33,6 @@ public class LoggingPreparationErrorEvent : IPreparationErrorEvent
{
_logger.LogResult(preparationResult, "Error in slash command preparation.");
return Task.FromResult(Result.FromSuccess());
return Task.FromResult(Result.Success);
}
}

View file

@ -118,7 +118,8 @@ public class MuteCommandGroup : CommandGroup
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: CancellationToken);
}
return await MuteUserAsync(executor, target, reason, duration, guildId, data, channelId, bot, CancellationToken);
return await MuteUserAsync(executor, target, reason, duration, guildId, data, channelId, bot,
CancellationToken);
}
private async Task<Result> MuteUserAsync(
@ -143,14 +144,16 @@ public class MuteCommandGroup : CommandGroup
var until = DateTimeOffset.UtcNow.Add(duration); // >:)
var muteMethodResult = await SelectMuteMethodAsync(executor, target, reason, duration, guildId, data, bot, until, ct);
var muteMethodResult =
await SelectMuteMethodAsync(executor, target, reason, duration, guildId, data, bot, until, ct);
if (!muteMethodResult.IsSuccess)
{
return ResultExtensions.FromError(muteMethodResult);
}
var title = string.Format(Messages.UserMuted, target.GetTag());
var description = new StringBuilder().AppendBulletPointLine(string.Format(Messages.DescriptionActionReason, reason))
var description = new StringBuilder()
.AppendBulletPointLine(string.Format(Messages.DescriptionActionReason, reason))
.AppendBulletPoint(string.Format(
Messages.DescriptionActionExpiresAt, Markdown.Timestamp(until))).ToString();
@ -348,11 +351,12 @@ public class MuteCommandGroup : CommandGroup
}
private async Task<Result> RemoveMuteRoleAsync(
IUser executor, IUser target, string reason, Snowflake guildId, MemberData memberData, CancellationToken ct = default)
IUser executor, IUser target, string reason, Snowflake guildId, MemberData memberData,
CancellationToken ct = default)
{
if (memberData.MutedUntil is null)
{
return Result.FromSuccess();
return Result.Success;
}
var unmuteResult = await _guildApi.ModifyGuildMemberAsync(
@ -372,7 +376,7 @@ public class MuteCommandGroup : CommandGroup
{
if (communicationDisabledUntil is null)
{
return Result.FromSuccess();
return Result.Success;
}
var unmuteResult = await _guildApi.ModifyGuildMemberAsync(

View file

@ -20,7 +20,7 @@ public sealed class BoolOption : Option<bool>
}
settings[Name] = value;
return Result.FromSuccess();
return Result.Success;
}
private static bool TryParseBool(string from, out bool value)

View file

@ -35,7 +35,13 @@ public class Option<T> : IOption
public virtual Result Set(JsonNode settings, string from)
{
settings[Name] = from;
return Result.FromSuccess();
return Result.Success;
}
public Result Reset(JsonNode settings)
{
settings[Name] = null;
return Result.Success;
}
/// <summary>
@ -48,10 +54,4 @@ public class Option<T> : IOption
var property = settings[Name];
return property != null ? property.GetValue<T>() : DefaultValue;
}
public Result Reset(JsonNode settings)
{
settings[Name] = null;
return Result.FromSuccess();
}
}

View file

@ -32,7 +32,7 @@ public sealed partial class SnowflakeOption : Option<Snowflake>
}
settings[Name] = parsed;
return Result.FromSuccess();
return Result.Success;
}
[GeneratedRegex("[^0-9]")]

View file

@ -22,6 +22,6 @@ public sealed class TimeSpanOption : Option<TimeSpan>
}
settings[Name] = span.ToString();
return Result.FromSuccess();
return Result.Success;
}
}

View file

@ -32,7 +32,7 @@ public static class CollectionExtensions
{
return list.Count switch
{
0 => Result.FromSuccess(),
0 => Result.Success,
1 => list[0],
_ => new AggregateError(list.Cast<IResult>().ToArray())
};

View file

@ -22,7 +22,7 @@ public static class GuildScheduledEventExtensions
}
return scheduledEvent.ScheduledEndTime.AsOptional().IsDefined(out endTime)
? Result.FromSuccess()
? Result.Success
: new ArgumentNullError(nameof(scheduledEvent.ScheduledEndTime));
}
}

View file

@ -42,7 +42,7 @@ public class GuildLoadedResponder : IResponder<IGuildCreate>
{
if (!gatewayEvent.Guild.IsT0) // Guild is not IAvailableGuild
{
return Result.FromSuccess();
return Result.Success;
}
var guild = gatewayEvent.Guild.AsT0;
@ -76,12 +76,12 @@ public class GuildLoadedResponder : IResponder<IGuildCreate>
if (!GuildSettings.ReceiveStartupMessages.Get(cfg))
{
return Result.FromSuccess();
return Result.Success;
}
if (GuildSettings.PrivateFeedbackChannel.Get(cfg).Empty())
{
return Result.FromSuccess();
return Result.Success;
}
Messages.Culture = GuildSettings.Language.Get(cfg);

View file

@ -54,7 +54,7 @@ public class GuildMemberJoinedResponder : IResponder<IGuildMemberAdd>
if (GuildSettings.WelcomeMessagesChannel.Get(cfg).Empty()
|| GuildSettings.WelcomeMessage.Get(cfg) is "off" or "disable" or "disabled")
{
return Result.FromSuccess();
return Result.Success;
}
Messages.Culture = GuildSettings.Language.Get(cfg);
@ -85,7 +85,7 @@ public class GuildMemberJoinedResponder : IResponder<IGuildMemberAdd>
{
if (!GuildSettings.ReturnRolesOnRejoin.Get(cfg))
{
return Result.FromSuccess();
return Result.Success;
}
var assignRoles = new List<Snowflake>();

View file

@ -38,13 +38,13 @@ public class GuildMemberLeftResponder : IResponder<IGuildMemberRemove>
var memberData = data.GetOrCreateMemberData(user.ID);
if (memberData.BannedUntil is not null || memberData.Kicked)
{
return Result.FromSuccess();
return Result.Success;
}
if (GuildSettings.WelcomeMessagesChannel.Get(cfg).Empty()
|| GuildSettings.LeaveMessage.Get(cfg) is "off" or "disable" or "disabled")
{
return Result.FromSuccess();
return Result.Success;
}
Messages.Culture = GuildSettings.Language.Get(cfg);

View file

@ -33,6 +33,6 @@ public class GuildUnloadedResponder : IResponder<IGuildDelete>
_logger.LogInformation("Unloaded guild {GuildId}", guildId);
}
return Task.FromResult(Result.FromSuccess());
return Task.FromResult(Result.Success);
}
}

View file

@ -39,13 +39,13 @@ public class MessageDeletedResponder : IResponder<IMessageDelete>
{
if (!gatewayEvent.GuildID.IsDefined(out var guildId))
{
return Result.FromSuccess();
return Result.Success;
}
var cfg = await _guildData.GetSettings(guildId, ct);
if (GuildSettings.PrivateFeedbackChannel.Get(cfg).Empty())
{
return Result.FromSuccess();
return Result.Success;
}
var messageResult = await _channelApi.GetChannelMessageAsync(gatewayEvent.ChannelID, gatewayEvent.ID, ct);
@ -56,7 +56,7 @@ public class MessageDeletedResponder : IResponder<IMessageDelete>
if (string.IsNullOrWhiteSpace(message.Content))
{
return Result.FromSuccess();
return Result.Success;
}
var auditLogResult = await _auditLogApi.GetGuildAuditLogAsync(

View file

@ -48,28 +48,28 @@ public class MessageEditedResponder : IResponder<IMessageUpdate>
if (!gatewayEvent.GuildID.IsDefined(out var guildId))
{
return Result.FromSuccess();
return Result.Success;
}
if (gatewayEvent.Author.IsDefined(out var author) && author.IsBot.OrDefault(false))
{
return Result.FromSuccess();
return Result.Success;
}
if (!gatewayEvent.EditedTimestamp.IsDefined(out var timestamp))
{
return Result.FromSuccess(); // The message wasn't actually edited
return Result.Success; // The message wasn't actually edited
}
if (!gatewayEvent.Content.IsDefined(out var newContent))
{
return Result.FromSuccess();
return Result.Success;
}
var cfg = await _guildData.GetSettings(guildId, ct);
if (GuildSettings.PrivateFeedbackChannel.Get(cfg).Empty())
{
return Result.FromSuccess();
return Result.Success;
}
var cacheKey = new KeyHelpers.MessageCacheKey(channelId, messageId);
@ -78,12 +78,12 @@ public class MessageEditedResponder : IResponder<IMessageUpdate>
if (!messageResult.IsDefined(out var message))
{
_ = _channelApi.GetChannelMessageAsync(channelId, messageId, ct);
return Result.FromSuccess();
return Result.Success;
}
if (message.Content == newContent)
{
return Result.FromSuccess();
return Result.Success;
}
// Custom event responders are called earlier than responders responsible for message caching

View file

@ -34,6 +34,6 @@ public class MessageCreateResponder : IResponder<IMessageCreate>
"лан" => "https://i.ibb.co/VYH2QLc/lan.jpg",
_ => default(Optional<string>)
});
return Task.FromResult(Result.FromSuccess());
return Task.FromResult(Result.Success);
}
}

View file

@ -121,7 +121,7 @@ public sealed partial class MemberUpdateService : BackgroundService
if (!canInteract)
{
return Result.FromSuccess();
return Result.Success;
}
var autoUnmuteResult = await TryAutoUnmuteAsync(guildId, id, data, ct);
@ -148,14 +148,14 @@ public sealed partial class MemberUpdateService : BackgroundService
{
if (data.BannedUntil is null || DateTimeOffset.UtcNow <= data.BannedUntil)
{
return Result.FromSuccess();
return Result.Success;
}
var existingBanResult = await _guildApi.GetGuildBanAsync(guildId, id, ct);
if (!existingBanResult.IsDefined())
{
data.BannedUntil = null;
return Result.FromSuccess();
return Result.Success;
}
var unbanResult = await _guildApi.RemoveGuildBanAsync(
@ -173,7 +173,7 @@ public sealed partial class MemberUpdateService : BackgroundService
{
if (data.MutedUntil is null || DateTimeOffset.UtcNow <= data.MutedUntil)
{
return Result.FromSuccess();
return Result.Success;
}
var unmuteResult = await _guildApi.ModifyGuildMemberAsync(
@ -209,7 +209,7 @@ public sealed partial class MemberUpdateService : BackgroundService
if (!usernameChanged)
{
return Result.FromSuccess();
return Result.Success;
}
var newNickname = string.Concat(characterList.ToArray());
@ -230,12 +230,13 @@ public sealed partial class MemberUpdateService : BackgroundService
{
if (DateTimeOffset.UtcNow < reminder.At)
{
return Result.FromSuccess();
return Result.Success;
}
var builder = new StringBuilder()
.AppendBulletPointLine(string.Format(Messages.DescriptionReminder, Markdown.InlineCode(reminder.Text)))
.AppendBulletPointLine(string.Format(Messages.DescriptionActionJumpToMessage, $"https://discord.com/channels/{guildId.Value}/{reminder.ChannelId}/{reminder.MessageId}"));
.AppendBulletPointLine(string.Format(Messages.DescriptionActionJumpToMessage,
$"https://discord.com/channels/{guildId.Value}/{reminder.ChannelId}/{reminder.MessageId}"));
var embed = new EmbedBuilder().WithSmallTitle(
string.Format(Messages.Reminder, user.GetTag()), user)
@ -251,6 +252,6 @@ public sealed partial class MemberUpdateService : BackgroundService
}
data.Reminders.Remove(reminder);
return Result.FromSuccess();
return Result.Success;
}
}

View file

@ -147,7 +147,7 @@ public sealed class ScheduledEventUpdateService : BackgroundService
|| eventData.EarlyNotificationSent
|| DateTimeOffset.UtcNow < scheduledEvent.ScheduledStartTime - offset)
{
return Result.FromSuccess();
return Result.Success;
}
var sendResult = await SendEarlyEventNotificationAsync(scheduledEvent, data, ct);
@ -182,7 +182,7 @@ public sealed class ScheduledEventUpdateService : BackgroundService
{
if (GuildSettings.EventNotificationChannel.Get(settings).Empty())
{
return Result.FromSuccess();
return Result.Success;
}
if (!scheduledEvent.Creator.IsDefined(out var creator))
@ -283,7 +283,7 @@ public sealed class ScheduledEventUpdateService : BackgroundService
if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
{
return Result.FromSuccess();
return Result.Success;
}
var embedDescriptionResult = scheduledEvent.EntityType switch
@ -324,7 +324,7 @@ public sealed class ScheduledEventUpdateService : BackgroundService
if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
{
data.ScheduledEvents.Remove(eventData.Id);
return Result.FromSuccess();
return Result.Success;
}
var completedEmbed = new EmbedBuilder()
@ -356,7 +356,7 @@ public sealed class ScheduledEventUpdateService : BackgroundService
if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
{
data.ScheduledEvents.Remove(eventData.Id);
return Result.FromSuccess();
return Result.Success;
}
var embed = new EmbedBuilder()
@ -409,7 +409,7 @@ public sealed class ScheduledEventUpdateService : BackgroundService
{
if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
{
return Result.FromSuccess();
return Result.Success;
}
var contentResult = await _utility.GetEventNotificationMentions(