mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-12 10:18:27 -04:00
Added appropriate error messages for every club related command. Removed obsolete error messages.
This commit is contained in:
@@ -4,6 +4,7 @@ using LinqToDB.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NadekoBot.Db;
|
||||
using NadekoBot.Db.Models;
|
||||
using OneOf;
|
||||
|
||||
namespace NadekoBot.Modules.Xp.Services;
|
||||
|
||||
@@ -18,15 +19,8 @@ public class ClubService : INService, IClubService
|
||||
_httpFactory = httpFactory;
|
||||
}
|
||||
|
||||
public enum CLubCreateResult
|
||||
{
|
||||
Success,
|
||||
AlreadyInAClub,
|
||||
NameTaken,
|
||||
InsufficientLevel,
|
||||
}
|
||||
|
||||
public async Task<bool?> CreateClubAsync(IUser user, string clubName)
|
||||
public async Task<ClubCreateResult> CreateClubAsync(IUser user, string clubName)
|
||||
{
|
||||
//must be lvl 5 and must not be in a club already
|
||||
|
||||
@@ -34,11 +28,14 @@ public class ClubService : INService, IClubService
|
||||
var du = uow.GetOrCreateUser(user);
|
||||
var xp = new LevelStats(du.TotalXp);
|
||||
|
||||
if (xp.Level < 5 || du.ClubId is not null)
|
||||
return false;
|
||||
if (xp.Level < 5)
|
||||
return ClubCreateResult.InsufficientLevel;
|
||||
|
||||
if (du.ClubId is not null)
|
||||
return ClubCreateResult.AlreadyInAClub;
|
||||
|
||||
if (await uow.Clubs.AnyAsyncEF(x => x.Name == clubName))
|
||||
return null;
|
||||
return ClubCreateResult.NameTaken;
|
||||
|
||||
du.IsClubAdmin = true;
|
||||
du.Club = new()
|
||||
@@ -52,23 +49,20 @@ public class ClubService : INService, IClubService
|
||||
await uow.GetTable<ClubApplicants>()
|
||||
.DeleteAsync(x => x.UserId == du.Id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public enum ClubTransferError
|
||||
{
|
||||
NotOwner,
|
||||
TargetNotMember
|
||||
return ClubCreateResult.Success;
|
||||
}
|
||||
|
||||
public ClubInfo TransferClub(IUser from, IUser newOwner)
|
||||
public OneOf<ClubInfo, ClubTransferError> TransferClub(IUser from, IUser newOwner)
|
||||
{
|
||||
using var uow = _db.GetDbContext();
|
||||
var club = uow.Clubs.GetByOwner(@from.Id);
|
||||
var newOwnerUser = uow.GetOrCreateUser(newOwner);
|
||||
|
||||
if (club is null || club.Owner.UserId != from.Id || !club.Members.Contains(newOwnerUser))
|
||||
return null;
|
||||
if (club is null || club.Owner.UserId != from.Id)
|
||||
return ClubTransferError.NotOwner;
|
||||
|
||||
if (!club.Members.Contains(newOwnerUser))
|
||||
return ClubTransferError.TargetNotMember;
|
||||
|
||||
club.Owner.IsClubAdmin = true; // old owner will stay as admin
|
||||
newOwnerUser.IsClubAdmin = true;
|
||||
@@ -76,30 +70,25 @@ public class ClubService : INService, IClubService
|
||||
uow.SaveChanges();
|
||||
return club;
|
||||
}
|
||||
|
||||
public enum ToggleAdminResult
|
||||
{
|
||||
AddedAdmin,
|
||||
RemovedAdmin,
|
||||
TargetNotMember,
|
||||
CanTargetThyself,
|
||||
}
|
||||
|
||||
public async Task<bool?> ToggleAdminAsync(IUser owner, IUser toAdmin)
|
||||
public async Task<ToggleAdminResult> ToggleAdminAsync(IUser owner, IUser toAdmin)
|
||||
{
|
||||
if (owner.Id == toAdmin.Id)
|
||||
return ToggleAdminResult.CantTargetThyself;
|
||||
|
||||
await using var uow = _db.GetDbContext();
|
||||
var club = uow.Clubs.GetByOwner(owner.Id);
|
||||
var adminUser = uow.GetOrCreateUser(toAdmin);
|
||||
|
||||
if (club is null || club.Owner.UserId != owner.Id || !club.Members.Contains(adminUser))
|
||||
return null;
|
||||
|
||||
if (club.OwnerId == adminUser.Id)
|
||||
return true;
|
||||
|
||||
if (club is null)
|
||||
return ToggleAdminResult.NotOwner;
|
||||
|
||||
if(!club.Members.Contains(adminUser))
|
||||
return ToggleAdminResult.TargetNotMember;
|
||||
|
||||
var newState = adminUser.IsClubAdmin = !adminUser.IsClubAdmin;
|
||||
await uow.SaveChangesAsync();
|
||||
return newState;
|
||||
return newState ? ToggleAdminResult.AddedAdmin : ToggleAdminResult.RemovedAdmin;
|
||||
}
|
||||
|
||||
public ClubInfo GetClubByMember(IUser user)
|
||||
@@ -108,35 +97,31 @@ public class ClubService : INService, IClubService
|
||||
var member = uow.Clubs.GetByMember(user.Id);
|
||||
return member;
|
||||
}
|
||||
|
||||
public enum SetClubIconResult
|
||||
{
|
||||
Success,
|
||||
InvalidFiletype,
|
||||
TooLarge,
|
||||
NotOwner,
|
||||
}
|
||||
|
||||
public async Task<bool> SetClubIconAsync(ulong ownerUserId, string url)
|
||||
public async Task<SetClubIconResult> SetClubIconAsync(ulong ownerUserId, string url)
|
||||
{
|
||||
if (url is not null)
|
||||
{
|
||||
using var http = _httpFactory.CreateClient();
|
||||
using var temp = await http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
|
||||
if (!temp.IsImage() || temp.GetContentLength() > 5.Megabytes().Bytes)
|
||||
return false;
|
||||
|
||||
if (!temp.IsImage())
|
||||
return SetClubIconResult.InvalidFileType;
|
||||
|
||||
if (temp.GetContentLength() > 5.Megabytes().Bytes)
|
||||
return SetClubIconResult.TooLarge;
|
||||
}
|
||||
|
||||
await using var uow = _db.GetDbContext();
|
||||
var club = uow.Clubs.GetByOwner(ownerUserId);
|
||||
|
||||
if (club is null)
|
||||
return false;
|
||||
return SetClubIconResult.NotOwner;
|
||||
|
||||
club.ImageUrl = url;
|
||||
await uow.SaveChangesAsync();
|
||||
|
||||
return true;
|
||||
return SetClubIconResult.Success;
|
||||
}
|
||||
|
||||
public bool GetClubByName(string clubName, out ClubInfo club)
|
||||
@@ -175,25 +160,19 @@ public class ClubService : INService, IClubService
|
||||
return ClubApplyResult.Success;
|
||||
}
|
||||
|
||||
public enum ClubAcceptResult
|
||||
{
|
||||
Accepted,
|
||||
NotOwnerOrAdmin,
|
||||
NoSuchApplicant,
|
||||
}
|
||||
|
||||
public bool AcceptApplication(ulong clubOwnerUserId, string userName, out DiscordUser discordUser)
|
||||
public ClubAcceptResult AcceptApplication(ulong clubOwnerUserId, string userName, out DiscordUser discordUser)
|
||||
{
|
||||
discordUser = null;
|
||||
using var uow = _db.GetDbContext();
|
||||
var club = uow.Clubs.GetByOwnerOrAdmin(clubOwnerUserId);
|
||||
if (club is null)
|
||||
return false;
|
||||
return ClubAcceptResult.NotOwnerOrAdmin;
|
||||
|
||||
var applicant =
|
||||
club.Applicants.FirstOrDefault(x => x.User.ToString().ToUpperInvariant() == userName.ToUpperInvariant());
|
||||
if (applicant is null)
|
||||
return false;
|
||||
return ClubAcceptResult.NoSuchApplicant;
|
||||
|
||||
applicant.User.Club = club;
|
||||
applicant.User.IsClubAdmin = false;
|
||||
@@ -205,7 +184,7 @@ public class ClubService : INService, IClubService
|
||||
|
||||
discordUser = applicant.User;
|
||||
uow.SaveChanges();
|
||||
return true;
|
||||
return ClubAcceptResult.Accepted;
|
||||
}
|
||||
|
||||
public ClubInfo GetClubWithBansAndApplications(ulong ownerUserId)
|
||||
@@ -305,27 +284,20 @@ public class ClubService : INService, IClubService
|
||||
return ClubUnbanResult.Success;
|
||||
}
|
||||
|
||||
public enum ClubKickResult
|
||||
{
|
||||
Success,
|
||||
NotOwnerOrAdmin,
|
||||
TargetNotAMember,
|
||||
Hierarchy
|
||||
}
|
||||
|
||||
public bool Kick(ulong kickerId, string userName, out ClubInfo club)
|
||||
public ClubKickResult Kick(ulong kickerId, string userName, out ClubInfo club)
|
||||
{
|
||||
using var uow = _db.GetDbContext();
|
||||
club = uow.Clubs.GetByOwnerOrAdmin(kickerId);
|
||||
if (club is null)
|
||||
return false;
|
||||
return ClubKickResult.NotOwnerOrAdmin;
|
||||
|
||||
var usr = club.Members.FirstOrDefault(x => x.ToString().ToUpperInvariant() == userName.ToUpperInvariant());
|
||||
if (usr is null)
|
||||
return false;
|
||||
return ClubKickResult.TargetNotAMember;
|
||||
|
||||
if (club.OwnerId == usr.Id || (usr.IsClubAdmin && club.Owner.UserId != kickerId))
|
||||
return false;
|
||||
return ClubKickResult.Hierarchy;
|
||||
|
||||
club.Members.Remove(usr);
|
||||
var app = club.Applicants.FirstOrDefault(x => x.UserId == usr.Id);
|
||||
@@ -333,7 +305,7 @@ public class ClubService : INService, IClubService
|
||||
club.Applicants.Remove(app);
|
||||
uow.SaveChanges();
|
||||
|
||||
return true;
|
||||
return ClubKickResult.Success;
|
||||
}
|
||||
|
||||
public List<ClubInfo> GetClubLeaderboardPage(int page)
|
||||
|
Reference in New Issue
Block a user