From d079e684bd4ce1adc4f25523fc2f21c40546ca10 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 13 Oct 2022 22:27:12 +0200 Subject: [PATCH] Clubleave errors clarified. Moved enum results to their own files --- src/NadekoBot/Modules/Xp/Club/Club.cs | 80 ++++++++++--------- .../Modules/Xp/Club/ClubBanResult.cs | 10 +++ .../Modules/Xp/Club/ClubLeaveResult.cs | 8 ++ src/NadekoBot/Modules/Xp/Club/ClubService.cs | 26 ++---- .../Modules/Xp/Club/ClubUnbanResult.cs | 8 ++ src/NadekoBot/Modules/Xp/Club/IClubService.cs | 2 +- .../strings/responses/responses.en-US.json | 3 +- 7 files changed, 77 insertions(+), 60 deletions(-) create mode 100644 src/NadekoBot/Modules/Xp/Club/ClubBanResult.cs create mode 100644 src/NadekoBot/Modules/Xp/Club/ClubLeaveResult.cs create mode 100644 src/NadekoBot/Modules/Xp/Club/ClubUnbanResult.cs diff --git a/src/NadekoBot/Modules/Xp/Club/Club.cs b/src/NadekoBot/Modules/Xp/Club/Club.cs index d8333c132..3473250ed 100644 --- a/src/NadekoBot/Modules/Xp/Club/Club.cs +++ b/src/NadekoBot/Modules/Xp/Club/Club.cs @@ -38,7 +38,7 @@ public partial class Xp public async Task ClubAdmin([Leftover] IUser toAdmin) { var admin = await _service.ToggleAdminAsync(ctx.User, toAdmin); - + if (admin is null) await ReplyErrorLocalizedAsync(strs.club_admin_error); else if (admin is true) @@ -46,6 +46,7 @@ public partial class Xp else await ReplyConfirmLocalizedAsync(strs.club_admin_remove(Format.Bold(toAdmin.ToString()))); } + [Cmd] public async Task ClubCreate([Leftover] string clubName) { @@ -62,7 +63,7 @@ public partial class Xp await ReplyErrorLocalizedAsync(strs.club_create_error_name); return; } - + if (succ is false) { await ReplyErrorLocalizedAsync(strs.club_create_error); @@ -102,27 +103,27 @@ public partial class Xp page => { var embed = _eb.Create() - .WithOkColor() - .WithTitle($"{club}") - .WithDescription(GetText(strs.level_x(lvl.Level + $" ({club.Xp} xp)"))) - .AddField(GetText(strs.desc), - string.IsNullOrWhiteSpace(club.Description) ? "-" : club.Description) - .AddField(GetText(strs.owner), club.Owner.ToString(), true) - // .AddField(GetText(strs.level_req), club.MinimumLevelReq.ToString(), true) - .AddField(GetText(strs.members), - string.Join("\n", - users.Skip(page * 10) - .Take(10) - .Select(x => - { - var l = new LevelStats(x.TotalXp); - var lvlStr = Format.Bold($" ⟪{l.Level}⟫"); - if (club.OwnerId == x.Id) - return x + "🌟" + lvlStr; - if (x.IsClubAdmin) - return x + "⭐" + lvlStr; - return x + lvlStr; - }))); + .WithOkColor() + .WithTitle($"{club}") + .WithDescription(GetText(strs.level_x(lvl.Level + $" ({club.Xp} xp)"))) + .AddField(GetText(strs.desc), + string.IsNullOrWhiteSpace(club.Description) ? "-" : club.Description) + .AddField(GetText(strs.owner), club.Owner.ToString(), true) + // .AddField(GetText(strs.level_req), club.MinimumLevelReq.ToString(), true) + .AddField(GetText(strs.members), + string.Join("\n", + users.Skip(page * 10) + .Take(10) + .Select(x => + { + var l = new LevelStats(x.TotalXp); + var lvlStr = Format.Bold($" ⟪{l.Level}⟫"); + if (club.OwnerId == x.Id) + return x + "🌟" + lvlStr; + if (x.IsClubAdmin) + return x + "⭐" + lvlStr; + return x + lvlStr; + }))); if (Uri.IsWellFormedUriString(club.ImageUrl, UriKind.Absolute)) return embed.WithThumbnailUrl(club.ImageUrl); @@ -187,9 +188,9 @@ public partial class Xp .Select(x => x.ToString())); return _eb.Create() - .WithTitle(GetText(strs.club_bans_for(club.ToString()))) - .WithDescription(toShow) - .WithOkColor(); + .WithTitle(GetText(strs.club_bans_for(club.ToString()))) + .WithDescription(toShow) + .WithOkColor(); }, bans.Length, 10); @@ -213,9 +214,9 @@ public partial class Xp var toShow = string.Join("\n", apps.Skip(page * 10).Take(10).Select(x => x.ToString())); return _eb.Create() - .WithTitle(GetText(strs.club_apps_for(club.ToString()))) - .WithDescription(toShow) - .WithOkColor(); + .WithTitle(GetText(strs.club_apps_for(club.ToString()))) + .WithDescription(toShow) + .WithOkColor(); }, apps.Length, 10); @@ -242,7 +243,6 @@ public partial class Xp await ReplyErrorLocalizedAsync(strs.club_insuff_lvl); else if (result == ClubApplyResult.AlreadyInAClub) await ReplyErrorLocalizedAsync(strs.club_already_in); - } [Cmd] @@ -263,10 +263,14 @@ public partial class Xp [Cmd] public async Task Clubleave() { - if (_service.LeaveClub(ctx.User)) + var res = _service.LeaveClub(ctx.User); + + if (res == ClubLeaveResult.Success) await ReplyConfirmLocalizedAsync(strs.club_left); + else if (res == ClubLeaveResult.NotInAClub) + await ReplyErrorLocalizedAsync(strs.club_not_in_a_club); else - await ReplyErrorLocalizedAsync(strs.club_not_in_club); + await ReplyErrorLocalizedAsync(strs.club_owner_cant_leave); } [Cmd] @@ -322,7 +326,7 @@ public partial class Xp public Task ClubUnBan([Leftover] string userName) { var result = _service.UnBan(ctx.User.Id, userName, out var club); - + if (result == ClubUnbanResult.Success) { return ReplyConfirmLocalizedAsync(strs.club_user_unbanned(Format.Bold(userName), @@ -345,12 +349,12 @@ public partial class Xp desc = string.IsNullOrWhiteSpace(desc) ? "-" : desc; - + var eb = _eb.Create(ctx) - .WithAuthor(ctx.User) - .WithTitle(GetText(strs.club_desc_update)) - .WithOkColor() - .WithDescription(desc); + .WithAuthor(ctx.User) + .WithTitle(GetText(strs.club_desc_update)) + .WithOkColor() + .WithDescription(desc); await ctx.Channel.EmbedAsync(eb); } diff --git a/src/NadekoBot/Modules/Xp/Club/ClubBanResult.cs b/src/NadekoBot/Modules/Xp/Club/ClubBanResult.cs new file mode 100644 index 000000000..cf3c3d411 --- /dev/null +++ b/src/NadekoBot/Modules/Xp/Club/ClubBanResult.cs @@ -0,0 +1,10 @@ +namespace NadekoBot.Modules.Xp.Services; + +public enum ClubBanResult +{ + Success, + NotOwnerOrAdmin, + WrongUser, + Unbannable, + +} \ No newline at end of file diff --git a/src/NadekoBot/Modules/Xp/Club/ClubLeaveResult.cs b/src/NadekoBot/Modules/Xp/Club/ClubLeaveResult.cs new file mode 100644 index 000000000..25ca1ad9f --- /dev/null +++ b/src/NadekoBot/Modules/Xp/Club/ClubLeaveResult.cs @@ -0,0 +1,8 @@ +namespace NadekoBot.Modules.Xp.Services; + +public enum ClubLeaveResult +{ + Success, + OwnerCantLeave, + NotInAClub +} \ No newline at end of file diff --git a/src/NadekoBot/Modules/Xp/Club/ClubService.cs b/src/NadekoBot/Modules/Xp/Club/ClubService.cs index f4f09a6dc..87a6fe2e7 100644 --- a/src/NadekoBot/Modules/Xp/Club/ClubService.cs +++ b/src/NadekoBot/Modules/Xp/Club/ClubService.cs @@ -178,17 +178,19 @@ public class ClubService : INService, IClubService return uow.Clubs.GetByOwnerOrAdmin(ownerUserId); } - public bool LeaveClub(IUser user) + public ClubLeaveResult LeaveClub(IUser user) { using var uow = _db.GetDbContext(); var du = uow.GetOrCreateUser(user, x => x.Include(u => u.Club)); - if (du.Club is null || du.Club.OwnerId == du.Id) - return false; + if (du.Club is null) + return ClubLeaveResult.NotInAClub; + if (du.Club.OwnerId == du.Id) + return ClubLeaveResult.OwnerCantLeave; du.Club = null; du.IsClubAdmin = false; uow.SaveChanges(); - return true; + return ClubLeaveResult.Success; } public bool SetDescription(ulong userId, string desc) @@ -298,20 +300,4 @@ public class ClubService : INService, IClubService using var uow = _db.GetDbContext(); return uow.Clubs.GetClubLeaderboardPage(page); } -} - -public enum ClubUnbanResult -{ - Success, - NotOwnerOrAdmin, - WrongUser -} - -public enum ClubBanResult -{ - Success, - NotOwnerOrAdmin, - WrongUser, - Unbannable, - } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Xp/Club/ClubUnbanResult.cs b/src/NadekoBot/Modules/Xp/Club/ClubUnbanResult.cs new file mode 100644 index 000000000..a74a73d84 --- /dev/null +++ b/src/NadekoBot/Modules/Xp/Club/ClubUnbanResult.cs @@ -0,0 +1,8 @@ +namespace NadekoBot.Modules.Xp.Services; + +public enum ClubUnbanResult +{ + Success, + NotOwnerOrAdmin, + WrongUser +} \ No newline at end of file diff --git a/src/NadekoBot/Modules/Xp/Club/IClubService.cs b/src/NadekoBot/Modules/Xp/Club/IClubService.cs index 37312a2e1..41ed9f48c 100644 --- a/src/NadekoBot/Modules/Xp/Club/IClubService.cs +++ b/src/NadekoBot/Modules/Xp/Club/IClubService.cs @@ -13,7 +13,7 @@ public interface IClubService ClubApplyResult ApplyToClub(IUser user, ClubInfo club); bool AcceptApplication(ulong clubOwnerUserId, string userName, out DiscordUser discordUser); ClubInfo? GetClubWithBansAndApplications(ulong ownerUserId); - bool LeaveClub(IUser user); + ClubLeaveResult LeaveClub(IUser user); bool SetDescription(ulong userId, string? desc); bool Disband(ulong userId, out ClubInfo club); ClubBanResult Ban(ulong bannerId, string userName, out ClubInfo club); diff --git a/src/NadekoBot/data/strings/responses/responses.en-US.json b/src/NadekoBot/data/strings/responses/responses.en-US.json index c392f5034..66e6b8d69 100644 --- a/src/NadekoBot/data/strings/responses/responses.en-US.json +++ b/src/NadekoBot/data/strings/responses/responses.en-US.json @@ -849,7 +849,8 @@ "club_accepted": "Accepted user {0} to the club.", "club_accept_error": "User not found", "club_left": "You've left the club.", - "club_not_in_club": "You are not in a club, or you're trying to leave the club you're the owner of.", + "club_not_in_a_club": "You are not in a club.", + "club_owner_cant_leave": "Club owner can't leave the club - you must either transfer ownership or disband the club.", "club_user_kick": "User {0} kicked from {1} club.", "club_user_not_in_club": "{0} is not in a club.", "club_user_kick_fail": "Error kicking. You're either not the club owner, or that user is not in your club.",