Working on .novel and .waifutransfer

This commit is contained in:
Master Kwoth 2017-11-01 14:01:42 +01:00
parent 9f7604f456
commit e581419945
5 changed files with 121 additions and 7 deletions

View File

@ -1,14 +1,40 @@
using NadekoBot.Core.Services;
using Discord;
using NadekoBot.Core.Services;
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Gambling.Services
{
public class WaifuService : INService
{
private readonly DbService _db;
public ConcurrentDictionary<ulong, DateTime> DivorceCooldowns { get; } = new ConcurrentDictionary<ulong, DateTime>();
public ConcurrentDictionary<ulong, DateTime> AffinityCooldowns { get; } = new ConcurrentDictionary<ulong, DateTime>();
public WaifuService(DbService db)
{
_db = db;
}
public async Task<bool> WaifuTransfer(IUser owner, ulong waifuId, IUser newOwner)
{
using (var uow = _db.UnitOfWork)
{
var waifu = uow.Waifus.ByWaifuUserId(waifuId);
var ownerUser = uow.DiscordUsers.GetOrCreate(owner);
// owner has to be the owner of the waifu
if (waifu.ClaimerId != ownerUser.Id)
return false;
//new claimerId is the id of the new owner
var newOwnerUser = uow.DiscordUsers.GetOrCreate(newOwner);
waifu.ClaimerId = newOwnerUser.Id;
await uow.CompleteAsync().ConfigureAwait(false);
}
}
}
}

View File

@ -55,6 +55,11 @@ namespace NadekoBot.Modules.Gambling
InsufficientAmount
}
private static readonly TimeSpan _affinityLimit = TimeSpan.FromMinutes(30);
private readonly IBotConfigProvider _bc;
private readonly CurrencyService _cs;
private readonly DbService _db;
public WaifuClaimCommands(IBotConfigProvider bc, CurrencyService cs, DbService db)
{
_bc = bc;
@ -183,6 +188,20 @@ namespace NadekoBot.Modules.Gambling
await Context.Channel.SendConfirmAsync(Context.User.Mention + msg).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task WaifuTransfer(IGuildUser waifu, IGuildUser newOwner)
{
if(!await _service.WaifuTransfer(waifu.Id, newOwner.Id)
.ConfigureAwait(false))
{
await ReplyErrorLocalized("waifu_transfer_fail").ConfigureAwait(false);
return;
}
await ReplyConfirmLocalized("waifu_transfer_success").ConfigureAwait(false);
}
public enum DivorceResult
{
Success,
@ -274,11 +293,6 @@ namespace NadekoBot.Modules.Gambling
}
}
private static readonly TimeSpan _affinityLimit = TimeSpan.FromMinutes(30);
private readonly IBotConfigProvider _bc;
private readonly CurrencyService _cs;
private readonly DbService _db;
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task WaifuClaimerAffinity([Remainder]IGuildUser u = null)
@ -299,6 +313,7 @@ namespace NadekoBot.Modules.Gambling
var now = DateTime.UtcNow;
if (w?.Affinity?.UserId == u?.Id)
{
//todo don't let people change affinity on different shards
}
else if (_service.AffinityCooldowns.AddOrUpdate(Context.User.Id,
now,

View File

@ -16,6 +16,32 @@ namespace NadekoBot.Modules.Searches
[Group]
public class AnimeSearchCommands : NadekoSubmodule<AnimeSearchService>
{
[NadekoCommand, Usage, Description, Aliases]
public async Task Novel([Remainder] string query)
{
if (string.IsNullOrWhiteSpace(query))
return;
var novelData = await _service.GetNovelData(query).ConfigureAwait(false);
if (novelData == null)
{
await ReplyErrorLocalized("failed_finding_novel").ConfigureAwait(false);
return;
}
var embed = new EmbedBuilder().WithColor(NadekoBot.OkColor)
.WithDescription(novelData.Description.Replace("<br>", Environment.NewLine))
.WithTitle(novelData.Title)
.WithUrl(novelData.Link)
.WithImageUrl(novelData.ImageUrl)
.AddField(efb => efb.WithName(GetText("authors")).WithValue(String.Join("\n", novelData.Authors)).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("status")).WithValue(novelData.Status).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("genres")).WithValue(string.Join(",\n", novelData.Genres.Any() ? novelData.Genres : new[] { "none" })).WithIsInline(true))
.WithFooter(efb => efb.WithText(GetText("score") + " " + novelData.Score));
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[Priority(0)]
public async Task Mal([Remainder] string name)

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Searches.Common
{
public class NovelData
{
public string Description { get; set; }
public string Title { get; set; }
public string Link { get; set; }
public string ImageUrl { get; set; }
public string[] Authors { get; set; }
public string Status { get; set; }
public string[] Genres { get; set; }
public string Score { get; set; }
}
}

View File

@ -46,6 +46,33 @@ namespace NadekoBot.Modules.Searches.Services
}
}
public async Task<NovelData> GetNovelData(string query)
{
if (string.IsNullOrWhiteSpace(query))
throw new ArgumentNullException(nameof(query));
query = query.Replace(" ", "-");
try
{
var link = "http://www.novelupdates.com/series/" + Uri.EscapeDataString(query.Replace("/", " "));
link = link.ToLowerInvariant();
var (ok, data) = await _cache.TryGetAnimeDataAsync(link).ConfigureAwait(false);
if (!ok)
{
data = await _http.GetStringAsync(link).ConfigureAwait(false);
await _cache.SetAnimeDataAsync(link, data).ConfigureAwait(false);
}
return JsonConvert.DeserializeObject<MangaResult>(data);
}
catch
{
return null;
}
}
public async Task<MangaResult> GetMangaData(string query)
{
if (string.IsNullOrWhiteSpace(query))