Killed history

This commit is contained in:
Kwoth
2021-09-06 21:29:22 +02:00
commit 7aca29ae8a
950 changed files with 366651 additions and 0 deletions

View File

@@ -0,0 +1,376 @@
using NadekoBot.Core.Services;
using System;
using NadekoBot.Core.Services.Database.Models;
using Discord;
using System.Linq;
using NadekoBot.Extensions;
using System.Net.Http;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Xp.Services
{
public class ClubService : INService
{
private readonly DbService _db;
private readonly IHttpClientFactory _httpFactory;
public ClubService(DbService db, IHttpClientFactory _httpFactory)
{
_db = db;
this._httpFactory = _httpFactory;
}
public bool CreateClub(IUser user, string clubName, out ClubInfo club)
{
//must be lvl 5 and must not be in a club already
club = null;
using (var uow = _db.GetDbContext())
{
var du = uow.DiscordUsers.GetOrCreate(user);
uow._context.SaveChanges();
var xp = new LevelStats(du.TotalXp);
if (xp.Level >= 5 && du.Club == null)
{
du.IsClubAdmin = true;
du.Club = new ClubInfo()
{
Name = clubName,
Discrim = uow.Clubs.GetNextDiscrim(clubName),
Owner = du,
};
uow.Clubs.Add(du.Club);
uow._context.SaveChanges();
}
else
return false;
uow._context.Set<ClubApplicants>()
.RemoveRange(uow._context.Set<ClubApplicants>()
.AsQueryable()
.Where(x => x.UserId == du.Id));
club = du.Club;
uow.SaveChanges();
}
return true;
}
public ClubInfo TransferClub(IUser from, IUser newOwner)
{
ClubInfo club;
using (var uow = _db.GetDbContext())
{
club = uow.Clubs.GetByOwner(from.Id);
var newOwnerUser = uow.DiscordUsers.GetOrCreate(newOwner);
if (club == null ||
club.Owner.UserId != from.Id ||
!club.Users.Contains(newOwnerUser))
return null;
club.Owner.IsClubAdmin = true; // old owner will stay as admin
newOwnerUser.IsClubAdmin = true;
club.Owner = newOwnerUser;
uow.SaveChanges();
}
return club;
}
public bool ToggleAdmin(IUser owner, IUser toAdmin)
{
bool newState;
using (var uow = _db.GetDbContext())
{
var club = uow.Clubs.GetByOwner(owner.Id);
var adminUser = uow.DiscordUsers.GetOrCreate(toAdmin);
if (club == null || club.Owner.UserId != owner.Id ||
!club.Users.Contains(adminUser))
throw new InvalidOperationException();
if (club.OwnerId == adminUser.Id)
return true;
newState = adminUser.IsClubAdmin = !adminUser.IsClubAdmin;
uow.SaveChanges();
}
return newState;
}
public ClubInfo GetClubByMember(IUser user)
{
using (var uow = _db.GetDbContext())
{
return uow.Clubs.GetByMember(user.Id);
}
}
public async Task<bool> SetClubIcon(ulong ownerUserId, Uri url)
{
if (url != null)
{
using (var http = _httpFactory.CreateClient())
using (var temp = await http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
{
if (!temp.IsImage() || temp.GetImageSize() > 11)
return false;
}
}
using (var uow = _db.GetDbContext())
{
var club = uow.Clubs.GetByOwner(ownerUserId, set => set);
if (club == null)
return false;
club.ImageUrl = url.ToString();
uow.SaveChanges();
}
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 == null)
return false;
else
return true;
}
}
public bool ApplyToClub(IUser user, ClubInfo club)
{
using (var uow = _db.GetDbContext())
{
var du = uow.DiscordUsers.GetOrCreate(user);
uow._context.SaveChanges();
if (du.Club != 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,
// or doesn't min minumum level requirement, can't apply
return false;
}
var app = new ClubApplicants
{
ClubId = club.Id,
UserId = du.Id,
};
uow._context.Set<ClubApplicants>().Add(app);
uow.SaveChanges();
}
return true;
}
public bool AcceptApplication(ulong clubOwnerUserId, string userName, out DiscordUser discordUser)
{
discordUser = null;
using (var uow = _db.GetDbContext())
{
var club = uow.Clubs.GetByOwnerOrAdmin(clubOwnerUserId);
if (club == null)
return false;
var applicant = club.Applicants.FirstOrDefault(x => x.User.ToString().ToUpperInvariant() == userName.ToUpperInvariant());
if (applicant == null)
return false;
applicant.User.Club = club;
applicant.User.IsClubAdmin = false;
club.Applicants.Remove(applicant);
//remove that user's all other applications
uow._context.Set<ClubApplicants>()
.RemoveRange(uow._context.Set<ClubApplicants>()
.AsQueryable()
.Where(x => x.UserId == applicant.User.Id));
discordUser = applicant.User;
uow.SaveChanges();
}
return true;
}
public ClubInfo GetClubWithBansAndApplications(ulong ownerUserId)
{
using (var uow = _db.GetDbContext())
{
return uow.Clubs.GetByOwnerOrAdmin(ownerUserId);
}
}
public bool LeaveClub(IUser user)
{
using (var uow = _db.GetDbContext())
{
var du = uow.DiscordUsers.GetOrCreate(user);
if (du.Club == null || du.Club.OwnerId == du.Id)
return false;
du.Club = null;
du.IsClubAdmin = false;
uow.SaveChanges();
}
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 == null)
return false;
club.MinimumLevelReq = level;
uow.SaveChanges();
}
return true;
}
public bool ChangeClubDescription(ulong userId, string desc)
{
using (var uow = _db.GetDbContext())
{
var club = uow.Clubs.GetByOwner(userId);
if (club == null)
return false;
club.Description = desc?.TrimTo(150, true);
uow.SaveChanges();
}
return true;
}
public bool Disband(ulong userId, out ClubInfo club)
{
using (var uow = _db.GetDbContext())
{
club = uow.Clubs.GetByOwner(userId);
if (club == null)
return false;
uow.Clubs.Remove(club);
uow.SaveChanges();
}
return true;
}
public bool Ban(ulong bannerId, string userName, out ClubInfo club)
{
using (var uow = _db.GetDbContext())
{
club = uow.Clubs.GetByOwnerOrAdmin(bannerId);
if (club == null)
return false;
var usr = club.Users.FirstOrDefault(x => x.ToString().ToUpperInvariant() == userName.ToUpperInvariant())
?? club.Applicants.FirstOrDefault(x => x.User.ToString().ToUpperInvariant() == userName.ToUpperInvariant())?.User;
if (usr == null)
return false;
if (club.OwnerId == usr.Id || (usr.IsClubAdmin && club.Owner.UserId != bannerId)) // can't ban the owner kek, whew
return false;
club.Bans.Add(new ClubBans
{
Club = club,
User = usr,
});
club.Users.Remove(usr);
var app = club.Applicants.FirstOrDefault(x => x.UserId == usr.Id);
if (app != null)
club.Applicants.Remove(app);
uow.SaveChanges();
}
return true;
}
public bool UnBan(ulong ownerUserId, string userName, out ClubInfo club)
{
using (var uow = _db.GetDbContext())
{
club = uow.Clubs.GetByOwnerOrAdmin(ownerUserId);
if (club == null)
return false;
var ban = club.Bans.FirstOrDefault(x => x.User.ToString().ToUpperInvariant() == userName.ToUpperInvariant());
if (ban == null)
return false;
club.Bans.Remove(ban);
uow.SaveChanges();
}
return true;
}
public bool Kick(ulong kickerId, string userName, out ClubInfo club)
{
using (var uow = _db.GetDbContext())
{
club = uow.Clubs.GetByOwnerOrAdmin(kickerId);
if (club == null)
return false;
var usr = club.Users.FirstOrDefault(x => x.ToString().ToUpperInvariant() == userName.ToUpperInvariant());
if (usr == null)
return false;
if (club.OwnerId == usr.Id || (usr.IsClubAdmin && club.Owner.UserId != kickerId))
return false;
club.Users.Remove(usr);
var app = club.Applicants.FirstOrDefault(x => x.UserId == usr.Id);
if (app != null)
club.Applicants.Remove(app);
uow.SaveChanges();
}
return true;
}
public ClubInfo[] GetClubLeaderboardPage(int page)
{
if (page < 0)
throw new ArgumentOutOfRangeException(nameof(page));
using (var uow = _db.GetDbContext())
{
return uow.Clubs.GetClubLeaderboardPage(page);
}
}
}
}

View File

@@ -0,0 +1,22 @@
using Discord;
namespace NadekoBot.Modules.Xp.Services
{
public class UserCacheItem
{
public IGuildUser User { get; set; }
public IGuild Guild { get; set; }
public IMessageChannel Channel { get; set; }
public int XpAmount { get; set; }
public override int GetHashCode()
{
return User.GetHashCode();
}
public override bool Equals(object obj)
{
return obj is UserCacheItem uci && uci.User == User;
}
}
}

View File

@@ -0,0 +1,70 @@
using System;
using System.Data.Common;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Core.Services;
using Serilog;
namespace NadekoBot.Modules.Xp.Services
{
public sealed class XpConfigMigrator : IConfigMigrator
{
private readonly DbService _db;
private readonly XpConfigService _gss;
public XpConfigMigrator(DbService dbService, XpConfigService gss)
{
_db = dbService;
_gss = gss;
}
public void EnsureMigrated()
{
using var uow = _db.GetDbContext();
using var conn = uow._context.Database.GetDbConnection();
Migrate(conn);
}
private void Migrate(DbConnection conn)
{
using (var checkTableCommand = conn.CreateCommand())
{
// make sure table still exists
checkTableCommand.CommandText =
"SELECT name FROM sqlite_master WHERE type='table' AND name='BotConfig';";
var checkReader = checkTableCommand.ExecuteReader();
if (!checkReader.HasRows)
return;
}
using (var checkMigratedCommand = conn.CreateCommand())
{
checkMigratedCommand.CommandText =
"UPDATE BotConfig SET HasMigratedXpSettings = 1 WHERE HasMigratedXpSettings = 0;";
var changedRows = checkMigratedCommand.ExecuteNonQuery();
if (changedRows == 0)
return;
}
Log.Information("Migrating Xp settings...");
using var com = conn.CreateCommand();
com.CommandText = $@"SELECT XpPerMessage, XpMinutesTimeout, VoiceXpPerMinute, MaxXpMinutes
FROM BotConfig";
using var reader = com.ExecuteReader();
if (!reader.Read())
return;
_gss.ModifyConfig(ModifyAction(reader));
}
private static Action<XpConfig> ModifyAction(DbDataReader reader)
=> config =>
{
config.XpPerMessage = (int) (long) reader["XpPerMessage"];
config.MessageXpCooldown = (int) (long) reader["XpMinutesTimeout"];
config.VoiceMaxMinutes = (int) (long) reader["MaxXpMinutes"];
config.VoiceXpPerMinute = (double) reader["VoiceXpPerMinute"];
};
}
}

View File

@@ -0,0 +1,44 @@
using System;
using NadekoBot.Core.Common;
using NadekoBot.Core.Common.Configs;
using NadekoBot.Core.Services;
namespace NadekoBot.Modules.Xp.Services
{
public sealed class XpConfigService : ConfigServiceBase<XpConfig>
{
public override string Name { get; } = "xp";
private const string FilePath = "data/xp.yml";
private static TypedKey<XpConfig> changeKey = new TypedKey<XpConfig>("config.xp.updated");
public XpConfigService(IConfigSeria serializer, IPubSub pubSub)
: base(FilePath, serializer, pubSub, changeKey)
{
AddParsedProp("txt.cooldown", conf => conf.MessageXpCooldown, int.TryParse,
ConfigPrinters.ToString, x => x > 0);
AddParsedProp("txt.per_msg", conf => conf.XpPerMessage, int.TryParse,
ConfigPrinters.ToString, x => x >= 0);
AddParsedProp("txt.per_image", conf => conf.XpFromImage, int.TryParse,
ConfigPrinters.ToString, x => x > 0);
AddParsedProp("voice.per_minute", conf => conf.VoiceXpPerMinute, double.TryParse,
ConfigPrinters.ToString, x => x >= 0);
AddParsedProp("voice.max_minutes", conf => conf.VoiceMaxMinutes, int.TryParse,
ConfigPrinters.ToString, x => x > 0);
Migrate();
}
private void Migrate()
{
if (_data.Version <= 1)
{
ModifyConfig(c =>
{
c.Version = 2;
c.XpFromImage = 0;
});
}
}
}
}

File diff suppressed because it is too large Load Diff