2016-08-15 14:57:40 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2016-08-13 18:45:08 +00:00
|
|
|
|
using Discord.WebSocket;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Impl;
|
2016-08-18 21:00:54 +00:00
|
|
|
|
using NLog;
|
2016-08-13 18:45:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-02-14 14:53:51 +00:00
|
|
|
|
using System.Collections.Immutable;
|
2017-02-14 13:49:21 +00:00
|
|
|
|
using System.Diagnostics;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
2017-04-15 00:54:19 +00:00
|
|
|
|
using System.Threading;
|
2017-06-04 09:40:34 +00:00
|
|
|
|
using System.IO;
|
2017-06-29 04:10:56 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2017-07-15 03:04:16 +00:00
|
|
|
|
using System.Collections.Generic;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common;
|
|
|
|
|
using NadekoBot.Common.ShardCom;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services.Database;
|
2017-09-14 17:37:41 +00:00
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
using Newtonsoft.Json;
|
2016-08-13 18:45:08 +00:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot
|
|
|
|
|
{
|
|
|
|
|
public class NadekoBot
|
|
|
|
|
{
|
2016-08-18 21:00:54 +00:00
|
|
|
|
private Logger _log;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public BotCredentials Credentials { get; }
|
|
|
|
|
public DiscordSocketClient Client { get; }
|
|
|
|
|
public CommandService CommandService { get; }
|
|
|
|
|
|
2017-07-25 16:31:30 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public ImmutableArray<GuildConfig> AllGuildConfigs { get; private set; }
|
|
|
|
|
|
2017-05-22 23:59:31 +00:00
|
|
|
|
/* I don't know how to make this not be static
|
|
|
|
|
* and keep the convenience of .WithOkColor
|
|
|
|
|
* and .WithErrorColor extensions methods.
|
|
|
|
|
* I don't want to pass botconfig every time I
|
|
|
|
|
* want to send a confirm or error message, so
|
|
|
|
|
* I'll keep this for now */
|
|
|
|
|
public static Color OkColor { get; private set; }
|
|
|
|
|
public static Color ErrorColor { get; private set; }
|
2017-05-27 23:51:22 +00:00
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public TaskCompletionSource<bool> Ready { get; private set; } = new TaskCompletionSource<bool>();
|
2016-10-26 14:30:49 +00:00
|
|
|
|
|
2017-05-27 23:51:22 +00:00
|
|
|
|
public INServiceProvider Services { get; private set; }
|
2017-05-10 14:08:31 +00:00
|
|
|
|
|
2017-07-20 03:10:39 +00:00
|
|
|
|
private readonly BotConfig _botConfig;
|
2017-10-09 22:04:02 +00:00
|
|
|
|
public IDataCache Cache { get; private set; }
|
2017-07-20 03:10:39 +00:00
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
public int GuildCount =>
|
|
|
|
|
Cache.Redis.GetDatabase()
|
|
|
|
|
.ListRange(Credentials.RedisKey() + "_shardstats")
|
|
|
|
|
.Select(x => JsonConvert.DeserializeObject<ShardComMessage>(x))
|
|
|
|
|
.Sum(x => x.Guilds);
|
|
|
|
|
|
2017-10-09 22:04:02 +00:00
|
|
|
|
public NadekoBot(int shardId, int parentProcessId)
|
2016-10-26 14:30:49 +00:00
|
|
|
|
{
|
2017-06-19 13:42:10 +00:00
|
|
|
|
if (shardId < 0)
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(shardId));
|
|
|
|
|
|
|
|
|
|
LogSetup.SetupLogger();
|
2017-05-22 23:59:31 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-06-04 09:40:34 +00:00
|
|
|
|
TerribleElevatedPermissionCheck();
|
2017-06-19 13:42:10 +00:00
|
|
|
|
|
2017-10-09 22:04:02 +00:00
|
|
|
|
Cache = new RedisCache();
|
2017-07-15 03:04:16 +00:00
|
|
|
|
Credentials = new BotCredentials();
|
2017-07-25 16:31:30 +00:00
|
|
|
|
_db = new DbService(Credentials);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
Client = new DiscordSocketClient(new DiscordSocketConfig
|
2016-08-13 18:45:08 +00:00
|
|
|
|
{
|
2016-09-06 21:34:00 +00:00
|
|
|
|
MessageCacheSize = 10,
|
2016-08-15 14:57:40 +00:00
|
|
|
|
LogLevel = LogSeverity.Warning,
|
2017-02-01 16:50:14 +00:00
|
|
|
|
ConnectionTimeout = int.MaxValue,
|
2017-06-19 13:42:10 +00:00
|
|
|
|
TotalShards = Credentials.TotalShards,
|
|
|
|
|
ShardId = shardId,
|
2017-06-17 12:07:15 +00:00
|
|
|
|
AlwaysDownloadUsers = false,
|
2016-08-13 18:45:08 +00:00
|
|
|
|
});
|
2017-05-27 23:51:22 +00:00
|
|
|
|
CommandService = new CommandService(new CommandServiceConfig()
|
2017-05-22 23:59:31 +00:00
|
|
|
|
{
|
2017-01-28 23:38:09 +00:00
|
|
|
|
CaseSensitiveCommands = false,
|
2017-06-20 15:14:55 +00:00
|
|
|
|
DefaultRunMode = RunMode.Sync,
|
2016-12-31 16:34:21 +00:00
|
|
|
|
});
|
2017-07-15 03:04:16 +00:00
|
|
|
|
|
2017-07-25 16:31:30 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-07-15 03:04:16 +00:00
|
|
|
|
{
|
2017-07-20 03:10:39 +00:00
|
|
|
|
_botConfig = uow.BotConfig.GetOrCreate();
|
|
|
|
|
OkColor = new Color(Convert.ToUInt32(_botConfig.OkColor, 16));
|
|
|
|
|
ErrorColor = new Color(Convert.ToUInt32(_botConfig.ErrorColor, 16));
|
2017-07-15 03:04:16 +00:00
|
|
|
|
}
|
2017-06-01 08:11:05 +00:00
|
|
|
|
|
2017-10-09 22:04:02 +00:00
|
|
|
|
SetupShard(parentProcessId);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
|
2017-05-27 23:51:22 +00:00
|
|
|
|
#if GLOBAL_NADEKO
|
|
|
|
|
Client.Log += Client_Log;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2016-08-13 18:45:08 +00:00
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private void StartSendingData()
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2017-10-10 16:24:36 +00:00
|
|
|
|
var data = new ShardComMessage()
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
|
|
|
|
ConnectionState = Client.ConnectionState,
|
|
|
|
|
Guilds = Client.ConnectionState == ConnectionState.Connected ? Client.Guilds.Count : 0,
|
|
|
|
|
ShardId = Client.ShardId,
|
2017-07-05 13:16:06 +00:00
|
|
|
|
Time = DateTime.UtcNow,
|
2017-10-10 16:24:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var sub = Cache.Redis.GetSubscriber();
|
|
|
|
|
var msg = JsonConvert.SerializeObject(data);
|
|
|
|
|
|
|
|
|
|
await sub.PublishAsync(Credentials.RedisKey() + "_shardcoord_send", msg).ConfigureAwait(false);
|
2017-10-23 17:46:59 +00:00
|
|
|
|
await Task.Delay(7500);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 23:51:22 +00:00
|
|
|
|
private void AddServices()
|
|
|
|
|
{
|
2017-06-20 02:23:11 +00:00
|
|
|
|
var startingGuildIdList = Client.Guilds.Select(x => (long)x.Id).ToList();
|
2017-06-22 21:59:54 +00:00
|
|
|
|
|
|
|
|
|
//this unit of work will be used for initialization of all modules too, to prevent multiple queries from running
|
2017-07-25 16:31:30 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
2017-06-20 02:23:11 +00:00
|
|
|
|
AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
|
2017-07-15 03:04:16 +00:00
|
|
|
|
|
2017-07-25 16:31:30 +00:00
|
|
|
|
IBotConfigProvider botConfigProvider = new BotConfigProvider(_db, _botConfig);
|
|
|
|
|
|
2017-06-22 21:59:54 +00:00
|
|
|
|
//initialize Services
|
2017-10-04 22:51:12 +00:00
|
|
|
|
Services = new NServiceProvider()
|
2017-07-15 03:04:16 +00:00
|
|
|
|
.AddManual<IBotCredentials>(Credentials)
|
2017-07-25 16:31:30 +00:00
|
|
|
|
.AddManual(_db)
|
2017-07-15 03:04:16 +00:00
|
|
|
|
.AddManual(Client)
|
|
|
|
|
.AddManual(CommandService)
|
2017-07-25 16:31:30 +00:00
|
|
|
|
.AddManual(botConfigProvider)
|
2017-07-15 03:04:16 +00:00
|
|
|
|
.AddManual<NadekoBot>(this)
|
|
|
|
|
.AddManual<IUnitOfWork>(uow)
|
2017-10-09 22:04:02 +00:00
|
|
|
|
.AddManual<IDataCache>(Cache);
|
2017-10-09 00:59:00 +00:00
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
Services.LoadFrom(Assembly.GetAssembly(typeof(CommandHandler)));
|
2017-06-22 21:59:54 +00:00
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
var commandHandler = Services.GetService<CommandHandler>();
|
|
|
|
|
commandHandler.AddServices(Services);
|
2017-06-22 21:59:54 +00:00
|
|
|
|
|
2017-10-09 00:52:46 +00:00
|
|
|
|
LoadTypeReaders(typeof(NadekoBot).Assembly);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
2017-10-04 22:51:12 +00:00
|
|
|
|
Services.Unload(typeof(IUnitOfWork)); // unload it after the startup
|
2017-05-22 23:59:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 22:04:02 +00:00
|
|
|
|
private IEnumerable<object> LoadTypeReaders(Assembly assembly)
|
2017-10-09 00:52:46 +00:00
|
|
|
|
{
|
|
|
|
|
Type[] allTypes;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
allTypes = assembly.GetTypes();
|
|
|
|
|
}
|
|
|
|
|
catch (ReflectionTypeLoadException ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.LoaderExceptions[0]);
|
2017-10-09 22:04:02 +00:00
|
|
|
|
return Enumerable.Empty<object>();
|
2017-10-09 00:52:46 +00:00
|
|
|
|
}
|
|
|
|
|
var filteredTypes = allTypes
|
2017-10-13 04:14:54 +00:00
|
|
|
|
.Where(x => x.IsSubclassOf(typeof(TypeReader))
|
2017-10-09 22:04:02 +00:00
|
|
|
|
&& x.BaseType.GetGenericArguments().Length > 0
|
2017-10-09 00:52:46 +00:00
|
|
|
|
&& !x.IsAbstract);
|
|
|
|
|
|
2017-10-09 22:04:02 +00:00
|
|
|
|
var toReturn = new List<object>();
|
2017-10-09 00:52:46 +00:00
|
|
|
|
foreach (var ft in filteredTypes)
|
|
|
|
|
{
|
2017-10-09 22:04:02 +00:00
|
|
|
|
var x = (TypeReader)Activator.CreateInstance(ft, Client, CommandService);
|
|
|
|
|
var baseType = ft.BaseType;
|
|
|
|
|
var typeArgs = baseType.GetGenericArguments();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CommandService.AddTypeReader(typeArgs[0], x);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Error(ex);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2017-10-09 00:52:46 +00:00
|
|
|
|
toReturn.Add(x);
|
2017-10-17 12:37:57 +00:00
|
|
|
|
//_log.Info("Loaded {0} typereader.", x.GetType().Name);
|
2017-10-09 00:52:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 21:59:54 +00:00
|
|
|
|
private async Task LoginAsync(string token)
|
2017-05-22 23:59:31 +00:00
|
|
|
|
{
|
2017-06-22 21:59:54 +00:00
|
|
|
|
var clientReady = new TaskCompletionSource<bool>();
|
|
|
|
|
|
|
|
|
|
Task SetClientReady()
|
|
|
|
|
{
|
2017-06-24 05:23:59 +00:00
|
|
|
|
var _ = Task.Run(async () =>
|
2017-06-23 23:41:24 +00:00
|
|
|
|
{
|
|
|
|
|
clientReady.TrySetResult(true);
|
2017-06-24 05:23:59 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-06-25 00:35:37 +00:00
|
|
|
|
foreach (var chan in (await Client.GetDMChannelsAsync()))
|
|
|
|
|
{
|
|
|
|
|
await chan.CloseAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
2017-06-24 05:23:59 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2017-10-04 22:51:12 +00:00
|
|
|
|
|
2017-06-24 05:23:59 +00:00
|
|
|
|
}
|
2017-06-23 23:41:24 +00:00
|
|
|
|
});
|
2017-06-22 21:59:54 +00:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-15 14:57:40 +00:00
|
|
|
|
//connect
|
2017-07-15 03:23:46 +00:00
|
|
|
|
_log.Info("Shard {0} logging in ...", Client.ShardId);
|
2017-06-25 06:00:52 +00:00
|
|
|
|
await Client.LoginAsync(TokenType.Bot, token).ConfigureAwait(false);
|
|
|
|
|
await Client.StartAsync().ConfigureAwait(false);
|
|
|
|
|
Client.Ready += SetClientReady;
|
|
|
|
|
await clientReady.Task.ConfigureAwait(false);
|
|
|
|
|
Client.Ready -= SetClientReady;
|
2017-07-05 09:43:36 +00:00
|
|
|
|
Client.JoinedGuild += Client_JoinedGuild;
|
|
|
|
|
Client.LeftGuild += Client_LeftGuild;
|
2017-07-15 03:23:46 +00:00
|
|
|
|
_log.Info("Shard {0} logged in.", Client.ShardId);
|
2017-05-27 23:51:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-05 09:43:36 +00:00
|
|
|
|
private Task Client_LeftGuild(SocketGuild arg)
|
|
|
|
|
{
|
|
|
|
|
_log.Info("Left server: {0} [{1}]", arg?.Name, arg?.Id);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task Client_JoinedGuild(SocketGuild arg)
|
|
|
|
|
{
|
|
|
|
|
_log.Info("Joined server: {0} [{1}]", arg?.Name, arg?.Id);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 23:51:22 +00:00
|
|
|
|
public async Task RunAsync(params string[] args)
|
|
|
|
|
{
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
|
|
|
|
|
await LoginAsync(Credentials.Token).ConfigureAwait(false);
|
|
|
|
|
|
2017-07-15 03:23:46 +00:00
|
|
|
|
_log.Info($"Shard {Client.ShardId} loading services...");
|
2017-10-17 12:37:57 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
AddServices();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Error(ex);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2016-08-18 21:00:54 +00:00
|
|
|
|
|
2017-02-14 13:49:21 +00:00
|
|
|
|
sw.Stop();
|
2017-07-15 03:23:46 +00:00
|
|
|
|
_log.Info($"Shard {Client.ShardId} connected in {sw.Elapsed.TotalSeconds:F2}s");
|
2016-08-18 21:00:54 +00:00
|
|
|
|
|
2017-05-27 23:51:22 +00:00
|
|
|
|
var stats = Services.GetService<IStatsService>();
|
|
|
|
|
stats.Initialize();
|
|
|
|
|
var commandHandler = Services.GetService<CommandHandler>();
|
|
|
|
|
var CommandService = Services.GetService<CommandService>();
|
|
|
|
|
|
2016-10-10 08:06:06 +00:00
|
|
|
|
// start handling messages received in commandhandler
|
2017-05-22 23:59:31 +00:00
|
|
|
|
await commandHandler.StartHandling().ConfigureAwait(false);
|
2016-10-10 08:06:06 +00:00
|
|
|
|
|
2017-05-27 23:51:22 +00:00
|
|
|
|
var _ = await CommandService.AddModulesAsync(this.GetType().GetTypeInfo().Assembly);
|
2017-06-02 01:44:59 +00:00
|
|
|
|
|
2017-06-15 17:16:50 +00:00
|
|
|
|
|
2017-06-29 04:10:56 +00:00
|
|
|
|
bool isPublicNadeko = false;
|
|
|
|
|
#if GLOBAL_NADEKO
|
|
|
|
|
isPublicNadeko = true;
|
|
|
|
|
#endif
|
|
|
|
|
//unload modules which are not available on the public bot
|
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
if (isPublicNadeko)
|
2017-06-29 04:10:56 +00:00
|
|
|
|
CommandService
|
|
|
|
|
.Modules
|
|
|
|
|
.ToArray()
|
|
|
|
|
.Where(x => x.Preconditions.Any(y => y.GetType() == typeof(NoPublicBot)))
|
|
|
|
|
.ForEach(x => CommandService.RemoveModuleAsync(x));
|
2017-06-21 21:34:29 +00:00
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
Ready.TrySetResult(true);
|
2017-09-14 17:37:41 +00:00
|
|
|
|
HandleStatusChanges();
|
2017-10-10 16:24:36 +00:00
|
|
|
|
StartSendingData();
|
2017-07-15 03:23:46 +00:00
|
|
|
|
_log.Info($"Shard {Client.ShardId} ready.");
|
2016-11-15 00:31:41 +00:00
|
|
|
|
}
|
2016-08-13 18:45:08 +00:00
|
|
|
|
|
2017-01-15 01:08:14 +00:00
|
|
|
|
private Task Client_Log(LogMessage arg)
|
|
|
|
|
{
|
2017-01-15 02:18:13 +00:00
|
|
|
|
_log.Warn(arg.Source + " | " + arg.Message);
|
2017-01-15 01:08:14 +00:00
|
|
|
|
if (arg.Exception != null)
|
|
|
|
|
_log.Warn(arg.Exception);
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 00:31:41 +00:00
|
|
|
|
public async Task RunAndBlockAsync(params string[] args)
|
|
|
|
|
{
|
|
|
|
|
await RunAsync(args).ConfigureAwait(false);
|
2017-10-10 16:24:36 +00:00
|
|
|
|
await Task.Delay(-1).ConfigureAwait(false);
|
2016-08-13 18:45:08 +00:00
|
|
|
|
}
|
2016-08-15 14:57:40 +00:00
|
|
|
|
|
2017-06-04 09:40:34 +00:00
|
|
|
|
private void TerribleElevatedPermissionCheck()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
File.WriteAllText("test", "test");
|
|
|
|
|
File.Delete("test");
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
_log.Error("You must run the application as an ADMINISTRATOR.");
|
|
|
|
|
Console.ReadKey();
|
|
|
|
|
Environment.Exit(2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-20 02:23:11 +00:00
|
|
|
|
|
2017-10-09 22:04:02 +00:00
|
|
|
|
private void SetupShard(int parentProcessId)
|
2017-06-20 02:23:11 +00:00
|
|
|
|
{
|
2017-07-15 03:23:46 +00:00
|
|
|
|
new Thread(new ThreadStart(() =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var p = Process.GetProcessById(parentProcessId);
|
|
|
|
|
if (p == null)
|
|
|
|
|
return;
|
|
|
|
|
p.WaitForExit();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Environment.Exit(10);
|
|
|
|
|
}
|
|
|
|
|
})).Start();
|
2017-06-20 02:23:11 +00:00
|
|
|
|
}
|
2017-09-14 17:37:41 +00:00
|
|
|
|
|
|
|
|
|
private void HandleStatusChanges()
|
|
|
|
|
{
|
|
|
|
|
var sub = Services.GetService<IDataCache>().Redis.GetSubscriber();
|
2017-09-22 22:42:15 +00:00
|
|
|
|
sub.Subscribe(Client.CurrentUser.Id + "_status.game_set", async (ch, game) =>
|
2017-09-14 17:37:41 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var obj = new { Name = default(string) };
|
|
|
|
|
obj = JsonConvert.DeserializeAnonymousType(game, obj);
|
|
|
|
|
await Client.SetGameAsync(obj.Name).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn(ex);
|
|
|
|
|
}
|
|
|
|
|
}, CommandFlags.FireAndForget);
|
|
|
|
|
|
2017-09-22 22:42:15 +00:00
|
|
|
|
sub.Subscribe(Client.CurrentUser.Id + "_status.stream_set", async (ch, streamData) =>
|
2017-09-14 17:37:41 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var obj = new { Name = "", Url = "" };
|
|
|
|
|
obj = JsonConvert.DeserializeAnonymousType(streamData, obj);
|
|
|
|
|
await Client.SetGameAsync(obj.Name, obj.Url, StreamType.Twitch).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn(ex);
|
|
|
|
|
}
|
|
|
|
|
}, CommandFlags.FireAndForget);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SetGameAsync(string game)
|
|
|
|
|
{
|
|
|
|
|
var obj = new { Name = game };
|
|
|
|
|
var sub = Services.GetService<IDataCache>().Redis.GetSubscriber();
|
2017-09-22 22:42:15 +00:00
|
|
|
|
return sub.PublishAsync(Client.CurrentUser.Id + "_status.game_set", JsonConvert.SerializeObject(obj));
|
2017-09-14 17:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SetStreamAsync(string name, string url)
|
|
|
|
|
{
|
|
|
|
|
var obj = new { Name = name, Url = url };
|
|
|
|
|
var sub = Services.GetService<IDataCache>().Redis.GetSubscriber();
|
2017-09-22 22:42:15 +00:00
|
|
|
|
return sub.PublishAsync(Client.CurrentUser.Id + "_status.game_set", JsonConvert.SerializeObject(obj));
|
2017-09-14 17:37:41 +00:00
|
|
|
|
}
|
2017-10-04 22:51:12 +00:00
|
|
|
|
|
2017-10-15 07:39:46 +00:00
|
|
|
|
//private readonly Dictionary<string, (IEnumerable<ModuleInfo> Modules, IEnumerable<Type> Types)> _loadedPackages = new Dictionary<string, (IEnumerable<ModuleInfo>, IEnumerable<Type>)>();
|
|
|
|
|
//private readonly SemaphoreSlim _packageLocker = new SemaphoreSlim(1, 1);
|
|
|
|
|
//public IEnumerable<string> LoadedPackages => _loadedPackages.Keys;
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
|
///// Unloads a package
|
|
|
|
|
///// </summary>
|
|
|
|
|
///// <param name="name">Package name. Case sensitive.</param>
|
|
|
|
|
///// <returns>Whether the unload is successful.</returns>
|
|
|
|
|
//public async Task<bool> UnloadPackage(string name)
|
|
|
|
|
//{
|
|
|
|
|
// await _packageLocker.WaitAsync().ConfigureAwait(false);
|
|
|
|
|
// try
|
|
|
|
|
// {
|
|
|
|
|
// if (!_loadedPackages.Remove(name, out var data))
|
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
|
|
// var modules = data.Modules;
|
|
|
|
|
// var types = data.Types;
|
|
|
|
|
|
|
|
|
|
// var i = 0;
|
|
|
|
|
// foreach (var m in modules)
|
|
|
|
|
// {
|
|
|
|
|
// await CommandService.RemoveModuleAsync(m).ConfigureAwait(false);
|
|
|
|
|
// i++;
|
|
|
|
|
// }
|
|
|
|
|
// _log.Info("Unloaded {0} modules.", i);
|
|
|
|
|
|
|
|
|
|
// if (types != null && types.Any())
|
|
|
|
|
// {
|
|
|
|
|
// i = 0;
|
|
|
|
|
// foreach (var t in types)
|
|
|
|
|
// {
|
|
|
|
|
// var obj = Services.Unload(t);
|
|
|
|
|
// if (obj is IUnloadableService s)
|
|
|
|
|
// await s.Unload().ConfigureAwait(false);
|
|
|
|
|
// i++;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// _log.Info("Unloaded {0} types.", i);
|
|
|
|
|
// }
|
|
|
|
|
// using (var uow = _db.UnitOfWork)
|
|
|
|
|
// {
|
|
|
|
|
// uow.BotConfig.GetOrCreate().LoadedPackages.Remove(new LoadedPackage
|
|
|
|
|
// {
|
|
|
|
|
// Name = name,
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// return true;
|
|
|
|
|
// }
|
|
|
|
|
// finally
|
|
|
|
|
// {
|
|
|
|
|
// _packageLocker.Release();
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
///// <summary>
|
|
|
|
|
///// Loads a package
|
|
|
|
|
///// </summary>
|
|
|
|
|
///// <param name="name">Name of the package to load. Case sensitive.</param>
|
|
|
|
|
///// <returns>Whether the load is successful.</returns>
|
|
|
|
|
//public async Task<bool> LoadPackage(string name)
|
|
|
|
|
//{
|
|
|
|
|
// await _packageLocker.WaitAsync().ConfigureAwait(false);
|
|
|
|
|
// try
|
|
|
|
|
// {
|
|
|
|
|
// if (_loadedPackages.ContainsKey(name))
|
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
|
|
// var startingGuildIdList = Client.Guilds.Select(x => (long)x.Id).ToList();
|
|
|
|
|
// using (var uow = _db.UnitOfWork)
|
|
|
|
|
// {
|
|
|
|
|
// AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// var domain = new Context();
|
|
|
|
|
// var package = domain.LoadFromAssemblyPath(Path.Combine(AppContext.BaseDirectory,
|
|
|
|
|
// "modules",
|
|
|
|
|
// $"NadekoBot.Modules.{name}",
|
|
|
|
|
// $"NadekoBot.Modules.{name}.dll"));
|
|
|
|
|
// //var package = Assembly.LoadFile(Path.Combine(AppContext.BaseDirectory,
|
|
|
|
|
// // "modules",
|
|
|
|
|
// // $"NadekoBot.Modules.{name}",
|
|
|
|
|
// // $"NadekoBot.Modules.{name}.dll"));
|
|
|
|
|
// var types = Services.LoadFrom(package);
|
|
|
|
|
// var added = await CommandService.AddModulesAsync(package).ConfigureAwait(false);
|
|
|
|
|
// var trs = LoadTypeReaders(package);
|
|
|
|
|
// /* i don't have to unload typereaders
|
|
|
|
|
// * (and there's no api for it)
|
|
|
|
|
// * because they get overwritten anyway, and since
|
|
|
|
|
// * the only time I'd unload typereaders, is when unloading a module
|
|
|
|
|
// * which means they won't have a chance to be used
|
|
|
|
|
// * */
|
|
|
|
|
// _log.Info("Loaded {0} modules and {1} types.", added.Count(), types.Count());
|
|
|
|
|
// _loadedPackages.Add(name, (added, types));
|
|
|
|
|
// using (var uow = _db.UnitOfWork)
|
|
|
|
|
// {
|
|
|
|
|
// uow.BotConfig.GetOrCreate().LoadedPackages.Add(new LoadedPackage
|
|
|
|
|
// {
|
|
|
|
|
// Name = name,
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// return true;
|
|
|
|
|
// }
|
|
|
|
|
// finally
|
|
|
|
|
// {
|
|
|
|
|
// _packageLocker.Release();
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2016-08-13 18:45:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|