1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-05-06 05:56:29 +03:00

Do not wrap result errors when returning them (#73)

This PR removes the wrapping of in-house created result errors with
`Result.FromError` (I did not know this was possible until today, lol),
which reduces code verbosity and makes it easier to read, and replaces
`ArgumentNullError` with `ArgumentInvalidError` when retrieving IDs from
a command context, which, in my opinion, is more correct.

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-07-28 21:58:55 +05:00 committed by GitHub
parent a6df26af67
commit f47ebe81c5
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 34 additions and 45 deletions

View file

@ -50,8 +50,7 @@ public class AboutCommandGroup : CommandGroup {
[UsedImplicitly]
public async Task<Result> ExecuteAboutAsync() {
if (!_context.TryGetContextIDs(out var guildId, out _, out _))
return Result.FromError(
new ArgumentNullError(nameof(_context), "Unable to retrieve necessary IDs from command context"));
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))

View file

@ -71,8 +71,7 @@ public class BanCommandGroup : CommandGroup {
[Description("Ban reason")] string reason,
[Description("Ban duration")] TimeSpan? duration = null) {
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
return Result.FromError(
new ArgumentNullError(nameof(_context), "Unable to retrieve necessary IDs from command context"));
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))
@ -186,8 +185,7 @@ public class BanCommandGroup : CommandGroup {
[Description("User to unban")] IUser target,
[Description("Unban reason")] string reason) {
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
return Result.FromError(
new ArgumentNullError(nameof(_context), "Unable to retrieve necessary IDs from command context"));
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))

View file

@ -61,8 +61,7 @@ public class ClearCommandGroup : CommandGroup {
[Description("Number of messages to remove (2-100)")] [MinValue(2)] [MaxValue(100)]
int amount) {
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
return Result.FromError(
new ArgumentNullError(nameof(_context), "Unable to retrieve necessary IDs from command context"));
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
var messagesResult = await _channelApi.GetChannelMessagesAsync(
channelId, limit: amount + 1, ct: CancellationToken);

View file

@ -66,8 +66,7 @@ public class KickCommandGroup : CommandGroup {
[Description("Member to kick")] IUser target,
[Description("Kick reason")] string reason) {
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
return Result.FromError(
new ArgumentNullError(nameof(_context), "Unable to retrieve necessary IDs from command context"));
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))

View file

@ -68,8 +68,7 @@ public class MuteCommandGroup : CommandGroup {
[Description("Mute reason")] string reason,
[Description("Mute duration")] TimeSpan duration) {
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
return Result.FromError(
new ArgumentNullError(nameof(_context), "Unable to retrieve necessary IDs from command context"));
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);
@ -162,8 +161,7 @@ public class MuteCommandGroup : CommandGroup {
[Description("Member to unmute")] IUser target,
[Description("Unmute reason")] string reason) {
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
return Result.FromError(
new ArgumentNullError(nameof(_context), "Unable to retrieve necessary IDs from command context"));
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);

View file

@ -53,8 +53,7 @@ public class PingCommandGroup : CommandGroup {
[UsedImplicitly]
public async Task<Result> ExecutePingAsync() {
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out _))
return Result.FromError(
new ArgumentNullError(nameof(_context), "Unable to retrieve necessary IDs from command context"));
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))

View file

@ -52,8 +52,7 @@ public class RemindCommandGroup : CommandGroup {
TimeSpan @in,
[Description("Reminder message")] string message) {
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
return Result.FromError(
new ArgumentNullError(nameof(_context), "Unable to retrieve necessary IDs from command context"));
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))

View file

@ -71,8 +71,7 @@ public class SettingsCommandGroup : CommandGroup {
public async Task<Result> ExecuteSettingsListAsync(
[Description("Settings list page")] int page) {
if (!_context.TryGetContextIDs(out var guildId, out _, out _))
return Result.FromError(
new ArgumentNullError(nameof(_context), "Unable to retrieve necessary IDs from command context"));
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))
@ -142,8 +141,7 @@ public class SettingsCommandGroup : CommandGroup {
string setting,
[Description("Setting value")] string value) {
if (!_context.TryGetContextIDs(out var guildId, out _, out _))
return Result.FromError(
new ArgumentNullError(nameof(_context), "Unable to retrieve necessary IDs from command context"));
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))