2017-07-17 19:42:36 +00:00
|
|
|
|
using System.Collections.Generic;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
using System.Collections.Immutable;
|
|
|
|
|
using System.Linq;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using Discord;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
using Discord.WebSocket;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common;
|
|
|
|
|
using NadekoBot.Common.ModuleBehaviors;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Impl;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NLog;
|
2017-10-31 08:52:46 +00:00
|
|
|
|
using StackExchange.Redis;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Administration.Services
|
2017-05-27 08:19:27 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public class SelfService : ILateExecutor, INService
|
2017-05-27 08:19:27 +00:00
|
|
|
|
{
|
2017-07-20 03:10:39 +00:00
|
|
|
|
public bool ForwardDMs => _bc.BotConfig.ForwardMessages;
|
|
|
|
|
public bool ForwardDMsToAllOwners => _bc.BotConfig.ForwardToAllOwners;
|
2017-10-31 08:52:46 +00:00
|
|
|
|
|
|
|
|
|
private readonly ConnectionMultiplexer _redis;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
private readonly NadekoBot _bot;
|
|
|
|
|
private readonly CommandHandler _cmdHandler;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly DbService _db;
|
|
|
|
|
private readonly Logger _log;
|
|
|
|
|
private readonly ILocalization _localization;
|
|
|
|
|
private readonly NadekoStrings _strings;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly IBotCredentials _creds;
|
|
|
|
|
private ImmutableArray<AsyncLazy<IDMChannel>> ownerChannels = new ImmutableArray<AsyncLazy<IDMChannel>>();
|
2017-07-20 03:10:39 +00:00
|
|
|
|
private readonly IBotConfigProvider _bc;
|
2017-10-31 08:52:46 +00:00
|
|
|
|
private readonly IImagesService _imgs;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
public SelfService(DiscordSocketClient client, NadekoBot bot, CommandHandler cmdHandler, DbService db,
|
2017-10-31 08:52:46 +00:00
|
|
|
|
IBotConfigProvider bc, ILocalization localization, NadekoStrings strings, IBotCredentials creds,
|
|
|
|
|
IDataCache cache, IImagesService imgs)
|
2017-05-27 08:19:27 +00:00
|
|
|
|
{
|
2017-10-31 08:52:46 +00:00
|
|
|
|
_redis = cache.Redis;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
_bot = bot;
|
|
|
|
|
_cmdHandler = cmdHandler;
|
|
|
|
|
_db = db;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
_localization = localization;
|
|
|
|
|
_strings = strings;
|
|
|
|
|
_client = client;
|
|
|
|
|
_creds = creds;
|
2017-07-20 03:10:39 +00:00
|
|
|
|
_bc = bc;
|
2017-10-31 08:52:46 +00:00
|
|
|
|
_imgs = imgs;
|
|
|
|
|
|
|
|
|
|
var sub = _redis.GetSubscriber();
|
|
|
|
|
sub.Subscribe(_creds.RedisKey() + "_reload_images",
|
|
|
|
|
delegate { _imgs.Reload(); }, CommandFlags.FireAndForget);
|
2017-05-27 08:19:27 +00:00
|
|
|
|
|
2017-10-13 00:21:39 +00:00
|
|
|
|
Task.Run(async () =>
|
2017-05-27 08:19:27 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
await bot.Ready.Task.ConfigureAwait(false);
|
2017-05-27 08:19:27 +00:00
|
|
|
|
|
2017-07-20 03:10:39 +00:00
|
|
|
|
foreach (var cmd in bc.BotConfig.StartupCommands)
|
2017-05-27 08:19:27 +00:00
|
|
|
|
{
|
2017-10-09 00:57:03 +00:00
|
|
|
|
var prefix = _cmdHandler.GetPrefix(cmd.GuildId);
|
|
|
|
|
//if someone already has .die as their startup command, ignore it
|
|
|
|
|
if (cmd.CommandText.StartsWith(prefix + "die"))
|
|
|
|
|
continue;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
await cmdHandler.ExecuteExternal(cmd.GuildId, cmd.ChannelId, cmd.CommandText);
|
|
|
|
|
await Task.Delay(400).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
2017-10-13 00:21:39 +00:00
|
|
|
|
Task.Run(async () =>
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
await bot.Ready.Task.ConfigureAwait(false);
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
|
|
|
|
await Task.Delay(5000);
|
|
|
|
|
|
2017-06-24 05:23:59 +00:00
|
|
|
|
if(client.ShardId == 0)
|
|
|
|
|
LoadOwnerChannels();
|
2017-05-29 04:13:22 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-24 05:23:59 +00:00
|
|
|
|
private void LoadOwnerChannels()
|
|
|
|
|
{
|
|
|
|
|
var hs = new HashSet<ulong>(_creds.OwnerIds);
|
|
|
|
|
var channels = new Dictionary<ulong, AsyncLazy<IDMChannel>>();
|
|
|
|
|
|
|
|
|
|
if (hs.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var g in _client.Guilds)
|
|
|
|
|
{
|
|
|
|
|
if (hs.Count == 0)
|
|
|
|
|
break;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
2017-06-24 05:23:59 +00:00
|
|
|
|
foreach (var u in g.Users)
|
|
|
|
|
{
|
|
|
|
|
if (hs.Remove(u.Id))
|
|
|
|
|
{
|
2017-07-05 15:38:38 +00:00
|
|
|
|
channels.Add(u.Id, new AsyncLazy<IDMChannel>(async () => await u.GetOrCreateDMChannelAsync()));
|
2017-06-24 05:23:59 +00:00
|
|
|
|
if (hs.Count == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
2017-06-24 05:23:59 +00:00
|
|
|
|
ownerChannels = channels.OrderBy(x => _creds.OwnerIds.IndexOf(x.Key))
|
|
|
|
|
.Select(x => x.Value)
|
|
|
|
|
.ToImmutableArray();
|
2017-06-19 13:42:10 +00:00
|
|
|
|
|
2017-06-24 05:23:59 +00:00
|
|
|
|
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.Length} out of {_creds.OwnerIds.Length} owner message channels.");
|
|
|
|
|
}
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
|
|
|
|
// forwards dms
|
2017-06-19 13:42:10 +00:00
|
|
|
|
public async Task LateExecute(DiscordSocketClient client, IGuild guild, IUserMessage msg)
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
|
|
|
|
if (msg.Channel is IDMChannel && ForwardDMs && ownerChannels.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
var title = _strings.GetText("dm_from",
|
|
|
|
|
_localization.DefaultCultureInfo,
|
|
|
|
|
"Administration".ToLowerInvariant()) +
|
|
|
|
|
$" [{msg.Author}]({msg.Author.Id})";
|
|
|
|
|
|
|
|
|
|
var attachamentsTxt = _strings.GetText("attachments",
|
|
|
|
|
_localization.DefaultCultureInfo,
|
|
|
|
|
"Administration".ToLowerInvariant());
|
|
|
|
|
|
|
|
|
|
var toSend = msg.Content;
|
|
|
|
|
|
|
|
|
|
if (msg.Attachments.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
toSend += $"\n\n{Format.Code(attachamentsTxt)}:\n" +
|
|
|
|
|
string.Join("\n", msg.Attachments.Select(a => a.ProxyUrl));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ForwardDMsToAllOwners)
|
|
|
|
|
{
|
|
|
|
|
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 ownerCh.SendConfirmAsync(title, toSend).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
_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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-27 08:19:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|