Fixed .fwtoall and .fwmsgs, ~ow level bugfix

This commit is contained in:
Master Kwoth 2017-05-19 10:02:29 +02:00
parent 95899f0542
commit c14c680606
3 changed files with 75 additions and 37 deletions

View File

@ -12,6 +12,9 @@ using Discord.WebSocket;
using NadekoBot.Services; using NadekoBot.Services;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Collections.Immutable;
using NadekoBot.DataStructures;
using NLog;
namespace NadekoBot.Modules.Administration namespace NadekoBot.Modules.Administration
{ {
@ -25,8 +28,11 @@ namespace NadekoBot.Modules.Administration
private static readonly object _locker = new object(); private static readonly object _locker = new object();
private new static readonly Logger _log;
static SelfCommands() static SelfCommands()
{ {
_log = LogManager.GetCurrentClassLogger();
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var config = uow.BotConfig.GetOrCreate(); var config = uow.BotConfig.GetOrCreate();
@ -230,9 +236,9 @@ namespace NadekoBot.Modules.Administration
} }
public static async Task HandleDmForwarding(IUserMessage msg, List<IDMChannel> ownerChannels) public static async Task HandleDmForwarding(IUserMessage msg, ImmutableArray<AsyncLazy<IDMChannel>> ownerChannels)
{ {
if (_forwardDMs && ownerChannels.Any()) if (_forwardDMs && ownerChannels.Length > 0)
{ {
var title = GetTextStatic("dm_from", var title = GetTextStatic("dm_from",
NadekoBot.Localization.DefaultCultureInfo, NadekoBot.Localization.DefaultCultureInfo,
@ -253,12 +259,25 @@ namespace NadekoBot.Modules.Administration
if (_forwardDMsToAllOwners) if (_forwardDMsToAllOwners)
{ {
await Task.WhenAll(ownerChannels.Where(ch => ch.Recipient.Id != msg.Author.Id) var allOwnerChannels = await Task.WhenAll(ownerChannels
.Select(ch => ch.SendConfirmAsync(title, toSend))).ConfigureAwait(false); .Select(x => x.Value))
.ConfigureAwait(false);
foreach (var ownerCh in allOwnerChannels.Where(ch => ch.Recipient.Id != msg.Author.Id))
{
try
{
await ownerCh.SendConfirmAsync(title, toSend).ConfigureAwait(false);
}
catch
{
_log.Warn("Can't contact owner with id {0}", ownerCh.Recipient.Id);
}
}
} }
else else
{ {
var firstOwnerChannel = ownerChannels.First(); var firstOwnerChannel = await ownerChannels[0];
if (firstOwnerChannel.Recipient.Id != msg.Author.Id) if (firstOwnerChannel.Recipient.Id != msg.Author.Id)
{ {
try try

View File

@ -42,7 +42,7 @@ namespace NadekoBot.Modules.Searches
.WithUrl($"https://www.overbuff.com/players/pc/{battletag}") .WithUrl($"https://www.overbuff.com/players/pc/{battletag}")
.WithIconUrl("https://cdn.discordapp.com/attachments/155726317222887425/255653487512256512/YZ4w2ey.png")) .WithIconUrl("https://cdn.discordapp.com/attachments/155726317222887425/255653487512256512/YZ4w2ey.png"))
.WithThumbnailUrl(qp.OverallStats.avatar) .WithThumbnailUrl(qp.OverallStats.avatar)
.AddField(fb => fb.WithName(GetText("level")).WithValue(qp.OverallStats.level.ToString()).WithIsInline(true)) .AddField(fb => fb.WithName(GetText("level")).WithValue((qp.OverallStats.level + (qp.OverallStats.prestige * 100)).ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("quick_wins")).WithValue(qp.OverallStats.wins.ToString()).WithIsInline(true)) .AddField(fb => fb.WithName(GetText("quick_wins")).WithValue(qp.OverallStats.wins.ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("compet_rank")).WithValue("0").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("compet_rank")).WithValue("0").WithIsInline(true))
.AddField(fb => fb.WithName(GetText("quick_playtime")).WithValue($"{qp.GameStats.timePlayed}hrs").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("quick_playtime")).WithValue($"{qp.GameStats.timePlayed}hrs").WithIsInline(true))
@ -58,7 +58,7 @@ namespace NadekoBot.Modules.Searches
.WithUrl($"https://www.overbuff.com/players/pc/{battletag}") .WithUrl($"https://www.overbuff.com/players/pc/{battletag}")
.WithIconUrl(compet.OverallStats.rank_image)) .WithIconUrl(compet.OverallStats.rank_image))
.WithThumbnailUrl(compet.OverallStats.avatar) .WithThumbnailUrl(compet.OverallStats.avatar)
.AddField(fb => fb.WithName(GetText("level")).WithValue((compet.OverallStats.level + (compet.OverallStats.prestige * 100)).ToString()).WithIsInline(true)) .AddField(fb => fb.WithName(GetText("level")).WithValue((qp.OverallStats.level + (qp.OverallStats.prestige * 100)).ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("quick_wins")).WithValue(qp.OverallStats.wins.ToString()).WithIsInline(true)) .AddField(fb => fb.WithName(GetText("quick_wins")).WithValue(qp.OverallStats.wins.ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("compet_wins")).WithValue(compet.OverallStats.wins.ToString()).WithIsInline(true)) .AddField(fb => fb.WithName(GetText("compet_wins")).WithValue(compet.OverallStats.wins.ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("compet_loses")).WithValue(compet.OverallStats.losses.ToString()).WithIsInline(true)) .AddField(fb => fb.WithName(GetText("compet_loses")).WithValue(compet.OverallStats.losses.ToString()).WithIsInline(true))

View File

@ -17,6 +17,8 @@ using NadekoBot.Modules.Games;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Threading; using System.Threading;
using NadekoBot.DataStructures; using NadekoBot.DataStructures;
using System.Diagnostics;
using System.Collections.Immutable;
namespace NadekoBot.Services namespace NadekoBot.Services
{ {
@ -34,7 +36,7 @@ namespace NadekoBot.Services
private readonly CommandService _commandService; private readonly CommandService _commandService;
private readonly Logger _log; private readonly Logger _log;
private List<IDMChannel> ownerChannels { get; set; } = new List<IDMChannel>(); private ImmutableArray<AsyncLazy<IDMChannel>> ownerChannels { get; set; } = new ImmutableArray<AsyncLazy<IDMChannel>>();
public event Func<IUserMessage, CommandInfo, Task> CommandExecuted = delegate { return Task.CompletedTask; }; public event Func<IUserMessage, CommandInfo, Task> CommandExecuted = delegate { return Task.CompletedTask; };
@ -60,28 +62,15 @@ namespace NadekoBot.Services
var _ = Task.Run(async () => var _ = Task.Run(async () =>
{ {
await Task.Delay(5000).ConfigureAwait(false); await Task.Delay(5000).ConfigureAwait(false);
ownerChannels = (await Task.WhenAll(_client.GetGuilds().SelectMany(g => g.Users)
.Where(u => NadekoBot.Credentials.OwnerIds.Contains(u.Id)) _client.GetGuilds().SelectMany(g => g.Users);
.Distinct(new GuildUserComparer())
.Select(async u => LoadOwnerChannels();
{
try
{
return await u.CreateDMChannelAsync();
}
catch
{
return null;
}
})))
.Where(ch => ch != null)
.OrderBy(x => NadekoBot.Credentials.OwnerIds.IndexOf(x.Id))
.ToList();
if (!ownerChannels.Any()) if (!ownerChannels.Any())
_log.Warn("No owner channels created! Make sure you've specified correct OwnerId in the credentials.json file."); _log.Warn("No owner channels created! Make sure you've specified correct OwnerId in the credentials.json file.");
else else
_log.Info($"Created {ownerChannels.Count} out of {NadekoBot.Credentials.OwnerIds.Length} owner message channels."); _log.Info($"Created {ownerChannels.Length} out of {NadekoBot.Credentials.OwnerIds.Length} owner message channels.");
}); });
_client.MessageReceived += MessageReceivedHandler; _client.MessageReceived += MessageReceivedHandler;
@ -109,6 +98,36 @@ namespace NadekoBot.Services
return Task.CompletedTask; return Task.CompletedTask;
} }
private void LoadOwnerChannels()
{
var hs = new HashSet<ulong>(NadekoBot.Credentials.OwnerIds);
var channels = new Dictionary<ulong, AsyncLazy<IDMChannel>>();
foreach (var s in _client.Shards)
{
if(hs.Count == 0)
break;
foreach (var g in s.Guilds)
{
if(hs.Count == 0)
break;
foreach (var u in g.Users)
{
if(hs.Remove(u.Id))
{
channels.Add(u.Id, new AsyncLazy<IDMChannel>(async () => await u.CreateDMChannelAsync()));
if(hs.Count == 0)
break;
}
}
}
}
ownerChannels = channels.OrderBy(x => NadekoBot.Credentials.OwnerIds.IndexOf(x.Key))
.Select(x => x.Value)
.ToImmutableArray();
}
private async Task<bool> TryRunCleverbot(IUserMessage usrMsg, SocketGuild guild) private async Task<bool> TryRunCleverbot(IUserMessage usrMsg, SocketGuild guild)
{ {
if (guild == null) if (guild == null)