diff --git a/DML.Client/DML.Client.csproj b/DML.Client/DML.Client.csproj new file mode 100644 index 0000000..4b6dd43 --- /dev/null +++ b/DML.Client/DML.Client.csproj @@ -0,0 +1,15 @@ + + + + netstandard1.4 + + + + + + + + + + + diff --git a/DML.Client/DMLClient.cs b/DML.Client/DMLClient.cs new file mode 100644 index 0000000..5f049c0 --- /dev/null +++ b/DML.Client/DMLClient.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Discord; +using Discord.WebSocket; + +namespace DML.Client +{ + public static class DMLClient + { + public static DiscordSocketClient Client { get; set; } = new DiscordSocketClient(new DiscordSocketConfig(){DefaultRetryMode = RetryMode.RetryRatelimit|RetryMode.RetryTimeouts}); + + public static async Task Login(string token) + { + await Client.LoginAsync(TokenType.User, token); + await Client.StartAsync(); + await Task.Delay(1000); + + while (Client.LoginState == LoginState.LoggingIn || Client.ConnectionState == ConnectionState.Connecting) + { + // wait + } + + return Client.LoginState == LoginState.LoggedIn && Client.ConnectionState == ConnectionState.Connected; + } + } +} diff --git a/Discord Media Loader.Application/Classes/Core.cs b/Discord Media Loader.Application/Classes/Core.cs index e73cde4..01ad7e7 100644 --- a/Discord Media Loader.Application/Classes/Core.cs +++ b/Discord Media Loader.Application/Classes/Core.cs @@ -10,6 +10,7 @@ using Discord.Net; using Discord.WebSocket; using DML.AppCore.Classes; using DML.Application.Dialogs; +using DML.Client; using LiteDB; using SweetLib.Utils; using SweetLib.Utils.Logger; @@ -20,7 +21,7 @@ namespace DML.Application.Classes { public static class Core { - internal static DiscordSocketClient Client { get; set; } + //internal static DiscordSocketClient Client { get; set; } internal static LiteDatabase Database { get; set; } internal static Settings Settings { get; set; } internal static JobScheduler Scheduler { get; } = new JobScheduler(); @@ -133,8 +134,8 @@ namespace DML.Application.Classes DefaultRetryMode = RetryMode.AlwaysRetry, }; - Client = new DiscordSocketClient(config); - Client.Log += (arg) => + //Client = new DiscordSocketClient(config); + DMLClient.Client.Log += (arg) => { var logMessage = $"DiscordClient: {arg.Message}"; switch (arg.Severity) @@ -163,9 +164,27 @@ namespace DML.Application.Classes Logger.Info("Trying to log into discord..."); var abort = false; - Client.Connected += Client_Connected; + DMLClient.Client.Connected += Client_Connected; - while ((Client.LoginState != LoginState.LoggedIn || Client.ConnectionState!=ConnectionState.Connected) && !abort) + var loggedIn = false; + + while (!loggedIn) + { + if (!string.IsNullOrEmpty(Settings.LoginToken)) + { + Logger.Debug("Trying to login with last known token..."); + loggedIn= await DMLClient.Login(Settings.LoginToken); + } + + if (!loggedIn) + { + Logger.Debug("Showing dialog for username and password..."); + var loginDlg = new LoginDialog(); + loginDlg.ShowDialog(); + } + } + + /*while ((Client.LoginState != LoginState.LoggedIn || Client.ConnectionState!=ConnectionState.Connected) && !abort) { Logger.Debug(Client.ConnectionState.ToString()); Logger.Debug(Client.LoginState.ToString()); @@ -199,13 +218,13 @@ namespace DML.Application.Classes loginDlg.ShowDialog(); Logger.Trace("Dialog closed."); } - } + }*/ Logger.Debug("Start checking for invalid jobs..."); //Client - while (Client.Guilds.Count == 0) + while (DMLClient.Client.Guilds.Count == 0) { // wait until guilds are loaded } @@ -262,7 +281,7 @@ namespace DML.Application.Classes private static SocketGuild FindServerById(ulong id) { Logger.Trace($"Trying to find server by Id: {id}"); - return (from s in Core.Client.Guilds where s.Id == id select s).FirstOrDefault(); + return (from s in DMLClient.Client.Guilds where s.Id == id select s).FirstOrDefault(); } private static SocketTextChannel FindChannelById(SocketGuild server, ulong id) diff --git a/Discord Media Loader.Application/Classes/Job.cs b/Discord Media Loader.Application/Classes/Job.cs index 5a60bae..c900f7e 100644 --- a/Discord Media Loader.Application/Classes/Job.cs +++ b/Discord Media Loader.Application/Classes/Job.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Discord; using Discord.WebSocket; using DML.Application.Classes; +using DML.Client; using SweetLib.Utils; using static SweetLib.Utils.Logger.Logger; @@ -50,7 +51,7 @@ namespace DML.AppCore.Classes private SocketGuild FindServerById(ulong id) { Trace($"Trying to find server by Id: {id}"); - return (from s in Core.Client.Guilds where s.Id == id select s).FirstOrDefault(); + return (from s in DMLClient.Client.Guilds where s.Id == id select s).FirstOrDefault(); } private SocketTextChannel FindChannelById(SocketGuild server, ulong id) @@ -59,10 +60,10 @@ namespace DML.AppCore.Classes return (from c in server.TextChannels where c.Id == id select c).FirstOrDefault(); } - internal async Task> Scan() + internal async Task> Scan() { Debug($"Starting scan of guild {GuildId} channel {ChannelId}..."); - var result = new List(); + var result = new List(); var limit = 100; var lastId = ulong.MaxValue; @@ -72,6 +73,13 @@ namespace DML.AppCore.Classes var guild = FindServerById(GuildId); var channel = FindChannelById(guild, ChannelId); + Debug("Checking channel access"); + if (!channel.Users.Contains(channel.Guild.CurrentUser)) + { + Info("Skipping channel without access"); + return result; + } + if (Math.Abs(StopTimestamp) < 0.4) StopTimestamp = KnownTimestamp; Trace("Initialized scanning parameters."); @@ -79,18 +87,37 @@ namespace DML.AppCore.Classes while (!finished) { Trace("Entering scanning loop..."); - SocketMessage[] messages; + var messages = new List(); Trace($"Downloading next {limit} messages..."); if (isFirst) { - messages = await channel.GetMessagesAsync(limit).ToArray() as SocketMessage[]; + //messages = await channel.GetMessagesAsync(limit).ToArray() as SocketMessage[]; + var realMessages = await channel.GetMessagesAsync(limit).ToArray(); + + foreach (var realMessageArray in realMessages) + { + foreach (var realMessage in realMessageArray) + { + messages.Add(realMessage); + } + } } else { - messages = await channel.GetMessagesAsync(lastId, Direction.Before, limit).ToArray() as SocketMessage[]; + var realMessages = await channel.GetMessagesAsync(lastId, Direction.Before, limit).ToArray(); + + foreach (var realMessageArray in realMessages) + { + foreach (var realMessage in realMessageArray) + { + messages.Add(realMessage); + } + } + + //messages = await channel.GetMessagesAsync(lastId, Direction.Before, limit).ToArray() as SocketMessage[]; } - Trace($"Downloaded {messages.Length} messages."); + Trace($"Downloaded {messages.Count} messages."); isFirst = false; @@ -125,7 +152,7 @@ namespace DML.AppCore.Classes Core.Scheduler.MessagesScanned++; } - finished = finished || messages.Length < limit; + finished = finished || messages.Count < limit; } Trace($"Downloaded all messages for guild {GuildId} channel {ChannelId}."); diff --git a/Discord Media Loader.Application/Classes/JobScheduler.cs b/Discord Media Loader.Application/Classes/JobScheduler.cs index b81ea96..6e6754f 100644 --- a/Discord Media Loader.Application/Classes/JobScheduler.cs +++ b/Discord Media Loader.Application/Classes/JobScheduler.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; +using Discord; using Discord.WebSocket; using DML.Application.Classes; using SweetLib.Utils; @@ -20,7 +21,7 @@ namespace DML.AppCore.Classes private bool Run { get; set; } = false; public List JobList { get; set; } = new List(); - public Dictionary> RunningJobs = new Dictionary>(); + public Dictionary> RunningJobs = new Dictionary>(); internal int RunningThreads { get; set; } = 0; internal ulong MessagesScanned @@ -112,7 +113,7 @@ namespace DML.AppCore.Classes if (!hasJob) { Logger.Debug("Job is not performed yet...Performing job..."); - var queue = new Queue(); + var queue = new Queue(); Logger.Trace("Locking scheduler..."); lock (this) @@ -179,7 +180,7 @@ namespace DML.AppCore.Classes } Logger.Trace("Found job."); - Queue queue; + Queue queue; Logger.Trace("Locking scheduler..."); lock (this) { @@ -229,7 +230,7 @@ namespace DML.AppCore.Classes if (socketTextChannel != null) { serverName = socketTextChannel.Guild.Name.Replace(":", "").Replace("/", "") - .Replace("\\", ""); + .Replace("\\", "").Replace("|", ""); } fileName = diff --git a/Discord Media Loader.Application/DML.Application.csproj b/Discord Media Loader.Application/DML.Application.csproj index fa96c36..32a438f 100644 --- a/Discord Media Loader.Application/DML.Application.csproj +++ b/Discord Media Loader.Application/DML.Application.csproj @@ -208,6 +208,10 @@ {02c1f8ef-32f2-4e77-a36d-79129402af37} SweetLib + + {045eb4a1-34e7-47e0-867e-e10c40505095} + DML.Client + diff --git a/Discord Media Loader.Application/MainForm.cs b/Discord Media Loader.Application/MainForm.cs index 362e675..7d629b9 100644 --- a/Discord Media Loader.Application/MainForm.cs +++ b/Discord Media Loader.Application/MainForm.cs @@ -8,6 +8,7 @@ using Discord.WebSocket; using DML.AppCore; using DML.AppCore.Classes; using DML.Application.Classes; +using DML.Client; using static SweetLib.Utils.Logger.Logger; namespace DML.Application @@ -49,7 +50,7 @@ namespace DML.Application if (cbGuild.Items.Count == 0) { Trace("Adding guilds to component..."); - cbGuild.Items.AddRange(Core.Client.Guilds.OrderBy(g => g.Name).Select(g => g.Name).ToArray()); + cbGuild.Items.AddRange(DMLClient.Client.Guilds.OrderBy(g => g.Name).Select(g => g.Name).ToArray()); cbGuild.SelectedIndex = 0; Trace("Guild component initialized."); } @@ -114,7 +115,7 @@ namespace DML.Application private SocketGuild FindServerByName(string name) { Trace($"Trying to find server by name: {name}"); - return (from s in Core.Client.Guilds where s.Name == name select s).FirstOrDefault(); + return (from s in DMLClient.Client.Guilds where s.Name == name select s).FirstOrDefault(); } private SocketTextChannel FindChannelByName(SocketGuild server, string name) @@ -126,7 +127,7 @@ namespace DML.Application private SocketGuild FindServerById(ulong id) { Trace($"Trying to find server by Id: {id}"); - return (from s in Core.Client.Guilds where s.Id == id select s).FirstOrDefault(); + return (from s in DMLClient.Client.Guilds where s.Id == id select s).FirstOrDefault(); } private SocketTextChannel FindChannelById(SocketGuild server, ulong id) diff --git a/Discord Media Loader.sln b/Discord Media Loader.sln index 7fed432..8124baf 100644 --- a/Discord Media Loader.sln +++ b/Discord Media Loader.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DML.Application", "Discord EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SweetLib", "..\SweetLib\SweetLib\SweetLib.csproj", "{02C1F8EF-32F2-4E77-A36D-79129402AF37}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DML.Client", "DML.Client\DML.Client.csproj", "{045EB4A1-34E7-47E0-867E-E10C40505095}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {02C1F8EF-32F2-4E77-A36D-79129402AF37}.Debug|Any CPU.Build.0 = Debug|Any CPU {02C1F8EF-32F2-4E77-A36D-79129402AF37}.Release|Any CPU.ActiveCfg = Release|Any CPU {02C1F8EF-32F2-4E77-A36D-79129402AF37}.Release|Any CPU.Build.0 = Release|Any CPU + {045EB4A1-34E7-47E0-867E-E10C40505095}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {045EB4A1-34E7-47E0-867E-E10C40505095}.Debug|Any CPU.Build.0 = Debug|Any CPU + {045EB4A1-34E7-47E0-867E-E10C40505095}.Release|Any CPU.ActiveCfg = Release|Any CPU + {045EB4A1-34E7-47E0-867E-E10C40505095}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE