mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
WIP db provider support for Mysql and Postgres
This commit is contained in:
@@ -7,7 +7,7 @@ namespace NadekoBot.Modules.Xp;
|
||||
public partial class Xp
|
||||
{
|
||||
[Group]
|
||||
public partial class Club : NadekoModule<ClubService>
|
||||
public partial class Club : NadekoModule<IClubService>
|
||||
{
|
||||
private readonly XpService _xps;
|
||||
|
||||
@@ -19,35 +19,33 @@ public partial class Xp
|
||||
{
|
||||
var club = _service.TransferClub(ctx.User, newOwner);
|
||||
|
||||
if (club is not null)
|
||||
if (club is null)
|
||||
{
|
||||
await ReplyConfirmLocalizedAsync(strs.club_transfered(Format.Bold(club.Name),
|
||||
Format.Bold(newOwner.ToString())));
|
||||
await ReplyErrorLocalizedAsync(strs.club_transfer_failed);
|
||||
}
|
||||
else
|
||||
await ReplyErrorLocalizedAsync(strs.club_transfer_failed);
|
||||
{
|
||||
await ReplyConfirmLocalizedAsync(
|
||||
strs.club_transfered(
|
||||
Format.Bold(club.Name),
|
||||
Format.Bold(newOwner.ToString())
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
public async partial Task ClubAdmin([Leftover] IUser toAdmin)
|
||||
{
|
||||
bool admin;
|
||||
try
|
||||
{
|
||||
admin = _service.ToggleAdmin(ctx.User, toAdmin);
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
var admin = await _service.ToggleAdminAsync(ctx.User, toAdmin);
|
||||
|
||||
if (admin is null)
|
||||
await ReplyErrorLocalizedAsync(strs.club_admin_error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (admin)
|
||||
else if (admin is true)
|
||||
await ReplyConfirmLocalizedAsync(strs.club_admin_add(Format.Bold(toAdmin.ToString())));
|
||||
else
|
||||
await ReplyConfirmLocalizedAsync(strs.club_admin_remove(Format.Bold(toAdmin.ToString())));
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
public async partial Task ClubCreate([Leftover] string clubName)
|
||||
{
|
||||
@@ -57,20 +55,28 @@ public partial class Xp
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_service.CreateClub(ctx.User, clubName, out var club))
|
||||
var succ = await _service.CreateClubAsync(ctx.User, clubName);
|
||||
|
||||
if (succ is null)
|
||||
{
|
||||
await ReplyErrorLocalizedAsync(strs.club_create_error_name);
|
||||
return;
|
||||
}
|
||||
|
||||
if (succ is false)
|
||||
{
|
||||
await ReplyErrorLocalizedAsync(strs.club_create_error);
|
||||
return;
|
||||
}
|
||||
|
||||
await ReplyConfirmLocalizedAsync(strs.club_created(Format.Bold(club.ToString())));
|
||||
await ReplyConfirmLocalizedAsync(strs.club_created(Format.Bold(clubName)));
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
public async partial Task ClubIcon([Leftover] string url = null)
|
||||
{
|
||||
if ((!Uri.IsWellFormedUriString(url, UriKind.Absolute) && url is not null)
|
||||
|| !await _service.SetClubIcon(ctx.User.Id, url is null ? null : new Uri(url)))
|
||||
|| !await _service.SetClubIconAsync(ctx.User.Id, url))
|
||||
{
|
||||
await ReplyErrorLocalizedAsync(strs.club_icon_error);
|
||||
return;
|
||||
@@ -82,7 +88,7 @@ public partial class Xp
|
||||
private async Task InternalClubInfoAsync(ClubInfo club)
|
||||
{
|
||||
var lvl = new LevelStats(club.Xp);
|
||||
var users = club.Users.OrderByDescending(x =>
|
||||
var users = club.Members.OrderByDescending(x =>
|
||||
{
|
||||
var l = new LevelStats(x.TotalXp).Level;
|
||||
if (club.OwnerId == x.Id)
|
||||
@@ -102,7 +108,7 @@ public partial class 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.level_req), club.MinimumLevelReq.ToString(), true)
|
||||
.AddField(GetText(strs.members),
|
||||
string.Join("\n",
|
||||
users.Skip(page * 10)
|
||||
@@ -123,7 +129,7 @@ public partial class Xp
|
||||
|
||||
return embed;
|
||||
},
|
||||
club.Users.Count,
|
||||
club.Members.Count,
|
||||
10);
|
||||
}
|
||||
|
||||
@@ -187,7 +193,6 @@ public partial class Xp
|
||||
10);
|
||||
}
|
||||
|
||||
|
||||
[Cmd]
|
||||
public partial Task ClubApps(int page = 1)
|
||||
{
|
||||
@@ -310,19 +315,10 @@ public partial class Xp
|
||||
return ReplyErrorLocalizedAsync(strs.club_user_unban_fail);
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
public async partial Task ClubLevelReq(int level)
|
||||
{
|
||||
if (_service.ChangeClubLevelReq(ctx.User.Id, level))
|
||||
await ReplyConfirmLocalizedAsync(strs.club_level_req_changed(Format.Bold(level.ToString())));
|
||||
else
|
||||
await ReplyErrorLocalizedAsync(strs.club_level_req_change_error);
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
public async partial Task ClubDescription([Leftover] string desc = null)
|
||||
{
|
||||
if (_service.ChangeClubDescription(ctx.User.Id, desc))
|
||||
if (_service.SetDescription(ctx.User.Id, desc))
|
||||
await ReplyConfirmLocalizedAsync(strs.club_desc_updated(Format.Bold(desc ?? "-")));
|
||||
else
|
||||
await ReplyErrorLocalizedAsync(strs.club_desc_update_failed);
|
||||
@@ -332,7 +328,7 @@ public partial class Xp
|
||||
public async partial Task ClubDisband()
|
||||
{
|
||||
if (_service.Disband(ctx.User.Id, out var club))
|
||||
await ReplyConfirmLocalizedAsync(strs.club_disbanded(Format.Bold(club.ToString())));
|
||||
await ReplyConfirmLocalizedAsync(strs.club_disbanded(Format.Bold(club.Name)));
|
||||
else
|
||||
await ReplyErrorLocalizedAsync(strs.club_disband_error);
|
||||
}
|
||||
|
@@ -1,10 +1,13 @@
|
||||
#nullable disable
|
||||
using LinqToDB;
|
||||
using LinqToDB.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NadekoBot.Db;
|
||||
using NadekoBot.Db.Models;
|
||||
|
||||
namespace NadekoBot.Modules.Xp.Services;
|
||||
|
||||
public class ClubService : INService
|
||||
public class ClubService : INService, IClubService
|
||||
{
|
||||
private readonly DbService _db;
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
@@ -15,46 +18,42 @@ public class ClubService : INService
|
||||
_httpFactory = httpFactory;
|
||||
}
|
||||
|
||||
public bool CreateClub(IUser user, string clubName, out ClubInfo club)
|
||||
public async Task<bool?> CreateClubAsync(IUser user, string clubName)
|
||||
{
|
||||
//must be lvl 5 and must not be in a club already
|
||||
|
||||
club = null;
|
||||
using var uow = _db.GetDbContext();
|
||||
await using var uow = _db.GetDbContext();
|
||||
var du = uow.GetOrCreateUser(user);
|
||||
uow.SaveChanges();
|
||||
var xp = new LevelStats(du.TotalXp);
|
||||
|
||||
if (xp.Level >= 5 && du.Club is null)
|
||||
{
|
||||
du.IsClubAdmin = true;
|
||||
du.Club = new()
|
||||
{
|
||||
Name = clubName,
|
||||
Discrim = uow.Clubs.GetNextDiscrim(clubName),
|
||||
Owner = du
|
||||
};
|
||||
uow.Clubs.Add(du.Club);
|
||||
uow.SaveChanges();
|
||||
}
|
||||
else
|
||||
|
||||
if (xp.Level < 5 || du.ClubId is not null)
|
||||
return false;
|
||||
|
||||
uow.Set<ClubApplicants>().RemoveRange(uow.Set<ClubApplicants>().AsQueryable().Where(x => x.UserId == du.Id));
|
||||
club = du.Club;
|
||||
uow.SaveChanges();
|
||||
if (await uow.Clubs.AnyAsyncEF(x => x.Name == clubName))
|
||||
return null;
|
||||
|
||||
du.IsClubAdmin = true;
|
||||
du.Club = new()
|
||||
{
|
||||
Name = clubName,
|
||||
Owner = du
|
||||
};
|
||||
uow.Clubs.Add(du.Club);
|
||||
await uow.SaveChangesAsync();
|
||||
|
||||
await uow.GetTable<ClubApplicants>()
|
||||
.DeleteAsync(x => x.UserId == du.Id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public ClubInfo TransferClub(IUser from, IUser newOwner)
|
||||
{
|
||||
ClubInfo club;
|
||||
using var uow = _db.GetDbContext();
|
||||
club = uow.Clubs.GetByOwner(from.Id);
|
||||
var club = uow.Clubs.GetByOwner(@from.Id);
|
||||
var newOwnerUser = uow.GetOrCreateUser(newOwner);
|
||||
|
||||
if (club is null || club.Owner.UserId != from.Id || !club.Users.Contains(newOwnerUser))
|
||||
if (club is null || club.Owner.UserId != from.Id || !club.Members.Contains(newOwnerUser))
|
||||
return null;
|
||||
|
||||
club.Owner.IsClubAdmin = true; // old owner will stay as admin
|
||||
@@ -64,21 +63,20 @@ public class ClubService : INService
|
||||
return club;
|
||||
}
|
||||
|
||||
public bool ToggleAdmin(IUser owner, IUser toAdmin)
|
||||
public async Task<bool?> ToggleAdminAsync(IUser owner, IUser toAdmin)
|
||||
{
|
||||
bool newState;
|
||||
using var uow = _db.GetDbContext();
|
||||
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.Users.Contains(adminUser))
|
||||
throw new InvalidOperationException();
|
||||
if (club is null || club.Owner.UserId != owner.Id || !club.Members.Contains(adminUser))
|
||||
return null;
|
||||
|
||||
if (club.OwnerId == adminUser.Id)
|
||||
return true;
|
||||
|
||||
newState = adminUser.IsClubAdmin = !adminUser.IsClubAdmin;
|
||||
uow.SaveChanges();
|
||||
var newState = adminUser.IsClubAdmin = !adminUser.IsClubAdmin;
|
||||
await uow.SaveChangesAsync();
|
||||
return newState;
|
||||
}
|
||||
|
||||
@@ -89,13 +87,13 @@ public class ClubService : INService
|
||||
return member;
|
||||
}
|
||||
|
||||
public async Task<bool> SetClubIcon(ulong ownerUserId, Uri url)
|
||||
public async Task<bool> 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.GetImageSize() > 11)
|
||||
if (!temp.IsImage() || temp.GetContentLength() > 5.Megabytes().Bytes)
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -105,30 +103,18 @@ public class ClubService : INService
|
||||
if (club is null)
|
||||
return false;
|
||||
|
||||
club.ImageUrl = url.ToString();
|
||||
uow.SaveChanges();
|
||||
club.ImageUrl = url;
|
||||
await uow.SaveChangesAsync();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool GetClubByName(string clubName, out ClubInfo club)
|
||||
{
|
||||
club = null;
|
||||
var arr = clubName.Split('#');
|
||||
if (arr.Length < 2 || !int.TryParse(arr[arr.Length - 1], out var discrim))
|
||||
return false;
|
||||
|
||||
//incase club has # in it
|
||||
var name = string.Concat(arr.Except(new[] { arr[arr.Length - 1] }));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
return false;
|
||||
|
||||
using var uow = _db.GetDbContext();
|
||||
club = uow.Clubs.GetByName(name, discrim);
|
||||
if (club is null)
|
||||
return false;
|
||||
return true;
|
||||
club = uow.Clubs.GetByName(clubName);
|
||||
|
||||
return club is not null;
|
||||
}
|
||||
|
||||
public bool ApplyToClub(IUser user, ClubInfo club)
|
||||
@@ -138,7 +124,6 @@ public class ClubService : INService
|
||||
uow.SaveChanges();
|
||||
|
||||
if (du.Club is not null
|
||||
|| new LevelStats(du.TotalXp).Level < club.MinimumLevelReq
|
||||
|| club.Bans.Any(x => x.UserId == du.Id)
|
||||
|| club.Applicants.Any(x => x.UserId == du.Id))
|
||||
//user banned or a member of a club, or already applied,
|
||||
@@ -202,23 +187,7 @@ public class ClubService : INService
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ChangeClubLevelReq(ulong userId, int level)
|
||||
{
|
||||
if (level < 5)
|
||||
return false;
|
||||
|
||||
using var uow = _db.GetDbContext();
|
||||
var club = uow.Clubs.GetByOwner(userId);
|
||||
if (club is null)
|
||||
return false;
|
||||
|
||||
club.MinimumLevelReq = level;
|
||||
uow.SaveChanges();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ChangeClubDescription(ulong userId, string desc)
|
||||
public bool SetDescription(ulong userId, string desc)
|
||||
{
|
||||
using var uow = _db.GetDbContext();
|
||||
var club = uow.Clubs.GetByOwner(userId);
|
||||
@@ -250,7 +219,7 @@ public class ClubService : INService
|
||||
if (club is null)
|
||||
return false;
|
||||
|
||||
var usr = club.Users.FirstOrDefault(x => x.ToString().ToUpperInvariant() == userName.ToUpperInvariant())
|
||||
var usr = club.Members.FirstOrDefault(x => x.ToString().ToUpperInvariant() == userName.ToUpperInvariant())
|
||||
?? club.Applicants
|
||||
.FirstOrDefault(x => x.User.ToString().ToUpperInvariant() == userName.ToUpperInvariant())
|
||||
?.User;
|
||||
@@ -266,7 +235,7 @@ public class ClubService : INService
|
||||
Club = club,
|
||||
User = usr
|
||||
});
|
||||
club.Users.Remove(usr);
|
||||
club.Members.Remove(usr);
|
||||
|
||||
var app = club.Applicants.FirstOrDefault(x => x.UserId == usr.Id);
|
||||
if (app is not null)
|
||||
@@ -301,14 +270,14 @@ public class ClubService : INService
|
||||
if (club is null)
|
||||
return false;
|
||||
|
||||
var usr = club.Users.FirstOrDefault(x => x.ToString().ToUpperInvariant() == userName.ToUpperInvariant());
|
||||
var usr = club.Members.FirstOrDefault(x => x.ToString().ToUpperInvariant() == userName.ToUpperInvariant());
|
||||
if (usr is null)
|
||||
return false;
|
||||
|
||||
if (club.OwnerId == usr.Id || (usr.IsClubAdmin && club.Owner.UserId != kickerId))
|
||||
return false;
|
||||
|
||||
club.Users.Remove(usr);
|
||||
club.Members.Remove(usr);
|
||||
var app = club.Applicants.FirstOrDefault(x => x.UserId == usr.Id);
|
||||
if (app is not null)
|
||||
club.Applicants.Remove(app);
|
||||
|
23
src/NadekoBot/Modules/Xp/Club/IClubService.cs
Normal file
23
src/NadekoBot/Modules/Xp/Club/IClubService.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using NadekoBot.Db.Models;
|
||||
|
||||
namespace NadekoBot.Modules.Xp.Services;
|
||||
|
||||
public interface IClubService
|
||||
{
|
||||
Task<bool?> CreateClubAsync(IUser user, string clubName);
|
||||
ClubInfo? TransferClub(IUser from, IUser newOwner);
|
||||
Task<bool?> ToggleAdminAsync(IUser owner, IUser toAdmin);
|
||||
ClubInfo? GetClubByMember(IUser user);
|
||||
Task<bool> SetClubIconAsync(ulong ownerUserId, string? url);
|
||||
bool GetClubByName(string clubName, out ClubInfo club);
|
||||
bool ApplyToClub(IUser user, ClubInfo club);
|
||||
bool AcceptApplication(ulong clubOwnerUserId, string userName, out DiscordUser discordUser);
|
||||
ClubInfo? GetClubWithBansAndApplications(ulong ownerUserId);
|
||||
bool LeaveClub(IUser user);
|
||||
bool SetDescription(ulong userId, string? desc);
|
||||
bool Disband(ulong userId, out ClubInfo club);
|
||||
bool Ban(ulong bannerId, string userName, out ClubInfo club);
|
||||
bool UnBan(ulong ownerUserId, string userName, out ClubInfo club);
|
||||
bool Kick(ulong kickerId, string userName, out ClubInfo club);
|
||||
List<ClubInfo> GetClubLeaderboardPage(int page);
|
||||
}
|
Reference in New Issue
Block a user