Performance improvements. Timely command almost done

This commit is contained in:
Master Kwoth
2017-10-26 11:31:44 +02:00
parent ffcaa594c9
commit 8220487672
18 changed files with 2084 additions and 23 deletions

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Threading.Tasks;
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Administration.Services;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Modules.Administration
{
@ -28,7 +29,7 @@ namespace NadekoBot.Modules.Administration
bool enabled;
using (var uow = _db.UnitOfWork)
{
var config = uow.BotConfig.GetOrCreate();
var config = uow.BotConfig.GetOrCreate(set => set);
enabled = config.RotatingStatuses = !config.RotatingStatuses;
uow.Complete();
@ -45,7 +46,7 @@ namespace NadekoBot.Modules.Administration
{
using (var uow = _db.UnitOfWork)
{
var config = uow.BotConfig.GetOrCreate();
var config = uow.BotConfig.GetOrCreate(set => set.Include(x => x.RotatingStatusMessages));
var toAdd = new PlayingStatus { Status = status };
config.RotatingStatusMessages.Add(toAdd);
await uow.CompleteAsync();
@ -79,7 +80,7 @@ namespace NadekoBot.Modules.Administration
string msg;
using (var uow = _db.UnitOfWork)
{
var config = uow.BotConfig.GetOrCreate();
var config = uow.BotConfig.GetOrCreate(set => set.Include(x => x.RotatingStatusMessages));
if (index >= config.RotatingStatusMessages.Count)
return;

View File

@ -190,7 +190,7 @@ namespace NadekoBot.Modules.Administration
{
using (var uow = _db.UnitOfWork)
{
var config = uow.BotConfig.GetOrCreate();
var config = uow.BotConfig.GetOrCreate(set => set);
config.ForwardMessages = !config.ForwardMessages;
uow.Complete();
}
@ -208,7 +208,7 @@ namespace NadekoBot.Modules.Administration
{
using (var uow = _db.UnitOfWork)
{
var config = uow.BotConfig.GetOrCreate();
var config = uow.BotConfig.GetOrCreate(set => set);
lock (_locker)
config.ForwardToAllOwners = !config.ForwardToAllOwners;
uow.Complete();

View File

@ -8,6 +8,7 @@ using NadekoBot.Core.Services.Database.Models;
using System.Collections.Generic;
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
using System;
namespace NadekoBot.Modules.Gambling
{
@ -16,16 +17,19 @@ namespace NadekoBot.Modules.Gambling
private readonly IBotConfigProvider _bc;
private readonly DbService _db;
private readonly CurrencyService _currency;
private readonly IDataCache _cache;
private string CurrencyName => _bc.BotConfig.CurrencyName;
private string CurrencyPluralName => _bc.BotConfig.CurrencyPluralName;
private string CurrencySign => _bc.BotConfig.CurrencySign;
public Gambling(IBotConfigProvider bc, DbService db, CurrencyService currency)
public Gambling(IBotConfigProvider bc, DbService db, CurrencyService currency,
IDataCache cache)
{
_bc = bc;
_db = db;
_currency = currency;
_cache = cache;
}
public long GetCurrency(ulong id)
@ -36,6 +40,45 @@ namespace NadekoBot.Modules.Gambling
}
}
[NadekoCommand, Usage, Description, Aliases]
public async Task Timely()
{
var val = _bc.BotConfig.TimelyCurrency;
var period = _bc.BotConfig.TimelyCurrencyPeriod;
if (val <= 0)
{
await ReplyErrorLocalized("timely_none").ConfigureAwait(false);
return;
}
TimeSpan? rem;
if ((rem = _cache.AddTimelyClaim(Context.User.Id, period)) != null)
{
await ReplyErrorLocalized("timely_already_claimed", rem?.ToString(@"HH\:mm\:ss")).ConfigureAwait(false);
return;
}
await ReplyConfirmLocalized("timely", val + _bc.BotConfig.CurrencySign, period).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
public async Task TimelySet(int num, int period = 24)
{
if (num < 0 || period < 1)
return;
using (var uow = _db.UnitOfWork)
{
uow.BotConfig.GetOrCreate(set => set)
.TimelyCurrency = num;
uow.Complete();
}
if(num == 0)
await ReplyConfirmLocalized("timely_set_none").ConfigureAwait(false);
else
await ReplyConfirmLocalized("timely_set", num, period).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task Raffle([Remainder] IRole role = null)
@ -46,7 +89,7 @@ namespace NadekoBot.Modules.Gambling
var membersArray = members as IUser[] ?? members.ToArray();
if (membersArray.Length == 0)
{
return;
}
var usr = membersArray[new NadekoRandom().Next(0, membersArray.Length)];
await Context.Channel.SendConfirmAsync("🎟 "+ GetText("raffled_user"), $"**{usr.Username}#{usr.Discriminator}**", footer: $"ID: {usr.Id}").ConfigureAwait(false);

View File

@ -8,6 +8,7 @@ using NadekoBot.Common.Attributes;
using NadekoBot.Common.TypeReaders;
using NadekoBot.Modules.Permissions.Services;
using NadekoBot.Core.Services.Database.Models;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Modules.Permissions
{
@ -55,7 +56,7 @@ namespace NadekoBot.Modules.Permissions
{
using (var uow = _db.UnitOfWork)
{
var bc = uow.BotConfig.GetOrCreate();
var bc = uow.BotConfig.GetOrCreate(set => set.Include(x => x.BlockedModules));
bc.BlockedModules.Add(new BlockedCmdOrMdl
{
Name = moduleName,
@ -69,7 +70,7 @@ namespace NadekoBot.Modules.Permissions
{
using (var uow = _db.UnitOfWork)
{
var bc = uow.BotConfig.GetOrCreate();
var bc = uow.BotConfig.GetOrCreate(set => set.Include(x => x.BlockedModules));
bc.BlockedModules.RemoveWhere(x => x.Name == moduleName);
uow.Complete();
}
@ -87,7 +88,7 @@ namespace NadekoBot.Modules.Permissions
{
using (var uow = _db.UnitOfWork)
{
var bc = uow.BotConfig.GetOrCreate();
var bc = uow.BotConfig.GetOrCreate(set => set.Include(x => x.BlockedCommands));
bc.BlockedCommands.Add(new BlockedCmdOrMdl
{
Name = commandName,
@ -101,7 +102,7 @@ namespace NadekoBot.Modules.Permissions
{
using (var uow = _db.UnitOfWork)
{
var bc = uow.BotConfig.GetOrCreate();
var bc = uow.BotConfig.GetOrCreate(set => set.Include(x => x.BlockedCommands));
bc.BlockedCommands.RemoveWhere(x => x.Name == commandName);
uow.Complete();
}

View File

@ -71,7 +71,7 @@ namespace NadekoBot.Modules.Permissions.Services
{
using (var uow = _db.UnitOfWork)
{
var bc = uow.BotConfig.GetOrCreate();
var bc = uow.BotConfig.GetOrCreate(set => set);
var log = LogManager.GetCurrentClassLogger();
if (bc.PermissionVersion <= 1)
{

View File

@ -145,7 +145,7 @@ namespace NadekoBot.Modules.Utility
using (var uow = _db.UnitOfWork)
{
uow.BotConfig.GetOrCreate().RemindMessageFormat = arg.Trim();
uow.BotConfig.GetOrCreate(set => set).RemindMessageFormat = arg.Trim();
await uow.CompleteAsync().ConfigureAwait(false);
}