It is working

This commit is contained in:
Serraniel 2017-10-04 15:01:54 +02:00
parent ecec55ec07
commit 5c45aad049
8 changed files with 124 additions and 23 deletions

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Discord.Net" Version="1.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\SweetLib\SweetLib\SweetLib.csproj" />
</ItemGroup>
</Project>

28
DML.Client/DMLClient.cs Normal file
View file

@ -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<bool> 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;
}
}
}

View file

@ -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)

View file

@ -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<List<SocketMessage>> Scan()
internal async Task<List<IMessage>> Scan()
{
Debug($"Starting scan of guild {GuildId} channel {ChannelId}...");
var result = new List<SocketMessage>();
var result = new List<IMessage>();
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<IMessage>();
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}.");

View file

@ -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<Job> JobList { get; set; } = new List<Job>();
public Dictionary<int, Queue<SocketMessage>> RunningJobs = new Dictionary<int, Queue<SocketMessage>>();
public Dictionary<int, Queue<IMessage>> RunningJobs = new Dictionary<int, Queue<IMessage>>();
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<SocketMessage>();
var queue = new Queue<IMessage>();
Logger.Trace("Locking scheduler...");
lock (this)
@ -179,7 +180,7 @@ namespace DML.AppCore.Classes
}
Logger.Trace("Found job.");
Queue<SocketMessage> queue;
Queue<IMessage> 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 =

View file

@ -208,6 +208,10 @@
<Project>{02c1f8ef-32f2-4e77-a36d-79129402af37}</Project>
<Name>SweetLib</Name>
</ProjectReference>
<ProjectReference Include="..\DML.Client\DML.Client.csproj">
<Project>{045eb4a1-34e7-47e0-867e-e10c40505095}</Project>
<Name>DML.Client</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="Resources\Serraniel-Logo4-NO-BG.png" />

View file

@ -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)

View file

@ -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