Fixed .fwtoall and .fwmsgs, ~ow level bugfix
This commit is contained in:
		@@ -12,6 +12,9 @@ using Discord.WebSocket;
 | 
			
		||||
using NadekoBot.Services;
 | 
			
		||||
using NadekoBot.Services.Database.Models;
 | 
			
		||||
using Microsoft.EntityFrameworkCore;
 | 
			
		||||
using System.Collections.Immutable;
 | 
			
		||||
using NadekoBot.DataStructures;
 | 
			
		||||
using NLog;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Modules.Administration
 | 
			
		||||
{
 | 
			
		||||
@@ -25,8 +28,11 @@ namespace NadekoBot.Modules.Administration
 | 
			
		||||
 | 
			
		||||
            private static readonly object _locker = new object();
 | 
			
		||||
 | 
			
		||||
            private new static readonly Logger _log;
 | 
			
		||||
 | 
			
		||||
            static SelfCommands()
 | 
			
		||||
            {
 | 
			
		||||
                _log = LogManager.GetCurrentClassLogger();
 | 
			
		||||
                using (var uow = DbHandler.UnitOfWork())
 | 
			
		||||
                {
 | 
			
		||||
                    var config = uow.BotConfig.GetOrCreate();
 | 
			
		||||
@@ -36,7 +42,7 @@ namespace NadekoBot.Modules.Administration
 | 
			
		||||
 | 
			
		||||
                var _ = Task.Run(async () =>
 | 
			
		||||
                {
 | 
			
		||||
                    while(!NadekoBot.Ready)
 | 
			
		||||
                    while (!NadekoBot.Ready)
 | 
			
		||||
                        await Task.Delay(1000);
 | 
			
		||||
 | 
			
		||||
                    foreach (var cmd in NadekoBot.BotConfig.StartupCommands)
 | 
			
		||||
@@ -128,7 +134,7 @@ namespace NadekoBot.Modules.Administration
 | 
			
		||||
{Format.Code(GetText("channel"))}: {x.ChannelName}/{x.ChannelId}
 | 
			
		||||
{Format.Code(GetText("command_text"))}: {x.CommandText}";
 | 
			
		||||
                        return str;
 | 
			
		||||
                    })),footer: GetText("page", page + 1))
 | 
			
		||||
                    })), footer: GetText("page", page + 1))
 | 
			
		||||
                         .ConfigureAwait(false);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
@@ -138,7 +144,7 @@ namespace NadekoBot.Modules.Administration
 | 
			
		||||
            public async Task Wait(int miliseconds)
 | 
			
		||||
            {
 | 
			
		||||
                if (miliseconds <= 0)
 | 
			
		||||
                    return;                
 | 
			
		||||
                    return;
 | 
			
		||||
                Context.Message.DeleteAfter(0);
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
@@ -172,7 +178,7 @@ namespace NadekoBot.Modules.Administration
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if(cmd == null)
 | 
			
		||||
                if (cmd == null)
 | 
			
		||||
                    await ReplyErrorLocalized("scrm_fail").ConfigureAwait(false);
 | 
			
		||||
                else
 | 
			
		||||
                    await ReplyConfirmLocalized("scrm").ConfigureAwait(false);
 | 
			
		||||
@@ -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",
 | 
			
		||||
                                    NadekoBot.Localization.DefaultCultureInfo,
 | 
			
		||||
@@ -253,26 +259,39 @@ namespace NadekoBot.Modules.Administration
 | 
			
		||||
 | 
			
		||||
                    if (_forwardDMsToAllOwners)
 | 
			
		||||
                    {
 | 
			
		||||
                        await Task.WhenAll(ownerChannels.Where(ch => ch.Recipient.Id != msg.Author.Id)
 | 
			
		||||
                            .Select(ch => ch.SendConfirmAsync(title, toSend))).ConfigureAwait(false);
 | 
			
		||||
                    }
 | 
			
		||||
                    else
 | 
			
		||||
                    {
 | 
			
		||||
                        var firstOwnerChannel = ownerChannels.First();
 | 
			
		||||
                        if (firstOwnerChannel.Recipient.Id != msg.Author.Id)
 | 
			
		||||
                        var allOwnerChannels = await Task.WhenAll(ownerChannels
 | 
			
		||||
                            .Select(x => x.Value))
 | 
			
		||||
                            .ConfigureAwait(false);
 | 
			
		||||
 | 
			
		||||
                        foreach (var ownerCh in allOwnerChannels.Where(ch => ch.Recipient.Id != msg.Author.Id))
 | 
			
		||||
                        {
 | 
			
		||||
                            try
 | 
			
		||||
                            {
 | 
			
		||||
                                await firstOwnerChannel.SendConfirmAsync(title, toSend).ConfigureAwait(false);
 | 
			
		||||
                                await ownerCh.SendConfirmAsync(title, toSend).ConfigureAwait(false);
 | 
			
		||||
                            }
 | 
			
		||||
                            catch
 | 
			
		||||
                            {
 | 
			
		||||
                                // ignored
 | 
			
		||||
                                _log.Warn("Can't contact owner with id {0}", ownerCh.Recipient.Id);
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    else
 | 
			
		||||
                    {
 | 
			
		||||
                            var firstOwnerChannel = await ownerChannels[0];
 | 
			
		||||
                            if (firstOwnerChannel.Recipient.Id != msg.Author.Id)
 | 
			
		||||
                            {
 | 
			
		||||
                                try
 | 
			
		||||
                                {
 | 
			
		||||
                                    await firstOwnerChannel.SendConfirmAsync(title, toSend).ConfigureAwait(false);
 | 
			
		||||
                                }
 | 
			
		||||
                                catch
 | 
			
		||||
                                {
 | 
			
		||||
                                    // ignored
 | 
			
		||||
                                }
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            [NadekoCommand, Usage, Description, Aliases]
 | 
			
		||||
 
 | 
			
		||||
@@ -42,7 +42,7 @@ namespace NadekoBot.Modules.Searches
 | 
			
		||||
                            .WithUrl($"https://www.overbuff.com/players/pc/{battletag}")
 | 
			
		||||
                            .WithIconUrl("https://cdn.discordapp.com/attachments/155726317222887425/255653487512256512/YZ4w2ey.png"))
 | 
			
		||||
                            .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("compet_rank")).WithValue("0").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}")
 | 
			
		||||
                                .WithIconUrl(compet.OverallStats.rank_image))
 | 
			
		||||
                            .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("compet_wins")).WithValue(compet.OverallStats.wins.ToString()).WithIsInline(true))
 | 
			
		||||
                            .AddField(fb => fb.WithName(GetText("compet_loses")).WithValue(compet.OverallStats.losses.ToString()).WithIsInline(true))
 | 
			
		||||
 
 | 
			
		||||
@@ -17,6 +17,8 @@ using NadekoBot.Modules.Games;
 | 
			
		||||
using System.Collections.Concurrent;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
using NadekoBot.DataStructures;
 | 
			
		||||
using System.Diagnostics;
 | 
			
		||||
using System.Collections.Immutable;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Services
 | 
			
		||||
{
 | 
			
		||||
@@ -34,7 +36,7 @@ namespace NadekoBot.Services
 | 
			
		||||
        private readonly CommandService _commandService;
 | 
			
		||||
        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; };
 | 
			
		||||
 | 
			
		||||
@@ -60,28 +62,15 @@ namespace NadekoBot.Services
 | 
			
		||||
            var _ = Task.Run(async () =>
 | 
			
		||||
            {
 | 
			
		||||
                await Task.Delay(5000).ConfigureAwait(false);
 | 
			
		||||
                ownerChannels = (await Task.WhenAll(_client.GetGuilds().SelectMany(g => g.Users)
 | 
			
		||||
                        .Where(u => NadekoBot.Credentials.OwnerIds.Contains(u.Id))
 | 
			
		||||
                        .Distinct(new GuildUserComparer())
 | 
			
		||||
                        .Select(async u =>
 | 
			
		||||
                        {
 | 
			
		||||
                            try
 | 
			
		||||
                            {
 | 
			
		||||
                                return await u.CreateDMChannelAsync();
 | 
			
		||||
                            }
 | 
			
		||||
                            catch
 | 
			
		||||
                            {
 | 
			
		||||
                                return null;
 | 
			
		||||
                            }
 | 
			
		||||
                        })))
 | 
			
		||||
                    .Where(ch => ch != null)
 | 
			
		||||
                    .OrderBy(x => NadekoBot.Credentials.OwnerIds.IndexOf(x.Id))
 | 
			
		||||
                    .ToList();
 | 
			
		||||
 | 
			
		||||
                _client.GetGuilds().SelectMany(g => g.Users);
 | 
			
		||||
 | 
			
		||||
                LoadOwnerChannels();
 | 
			
		||||
 | 
			
		||||
                if (!ownerChannels.Any())
 | 
			
		||||
                    _log.Warn("No owner channels created! Make sure you've specified correct OwnerId in the credentials.json file.");
 | 
			
		||||
                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;
 | 
			
		||||
@@ -109,6 +98,36 @@ namespace NadekoBot.Services
 | 
			
		||||
            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)
 | 
			
		||||
        {
 | 
			
		||||
            if (guild == null)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user