Archived
1
0
Fork 0
This commit is contained in:
flash 2017-05-24 22:30:46 +02:00
parent c81ea9441c
commit 625ca8e30c
4 changed files with 18 additions and 32 deletions

View file

@ -8,10 +8,8 @@ using Maki.Structures.Messages;
using Maki.Structures.Presences;
using Maki.Structures.Roles;
using Maki.Structures.Users;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Maki
@ -21,12 +19,6 @@ namespace Maki
/// </summary>
public class Discord : IDisposable
{
// temporary, whether gateway data should be written to a file
public bool LogGateway = false;
// temporary, whether rest data should be written to the console
public bool LogRest = false;
/// <summary>
/// Discord Gateway/API version we're targeting
/// </summary>
@ -209,14 +201,18 @@ namespace Maki
public DiscordServer[] Servers => servers.ToArray();
public DiscordUser Me => users.FirstOrDefault();
private DiscordParams Params;
/// <summary>
/// Constructor
/// </summary>
public Discord()
public Discord(DiscordParams parameters = null)
{
Params = parameters ?? new DiscordParams();
RestClient = new RestClient(this);
ShardClient = new GatewayShardClient(this);
#region Assigning event handlers
ShardClient.OnChannelCreate += ShardManager_OnChannelCreate;
ShardClient.OnChannelUpdate += ShardManager_OnChannelUpdate;
ShardClient.OnChannelDelete += ShardManager_OnChannelDelete;
@ -252,6 +248,7 @@ namespace Maki
ShardClient.OnSocketClose += ShardManager_OnSocketClose;
ShardClient.OnSocketError += ShardManager_OnSocketError;
ShardClient.OnSocketMessage += ShardManager_OnSocketMessage;
#endregion
}
private void ClearContainers()
@ -376,6 +373,7 @@ namespace Maki
Gateway = string.Empty;
}
#region Event Handlers
private void ShardManager_OnChannelCreate(GatewayShard shard, Channel channel)
{
DiscordServer server = servers.Find(x => x.Id == channel.GuildId);
@ -819,38 +817,22 @@ namespace Maki
private void ShardManager_OnSocketOpen(GatewayShard shard)
{
//MultiLineWrite($"Connection opened on shard {shard.Id}");
}
private void ShardManager_OnSocketClose(GatewayShard shard, bool wasClean, ushort code, string reason)
{
//MultiLineWrite($"Connection closed on shard {shard.Id} ({wasClean}/{code}/{reason})");
}
private void ShardManager_OnSocketError(GatewayShard shard, Exception ex)
{
//MultiLineWrite($"Socket error on shard {shard.Id}", ex.Message);
}
private void ShardManager_OnSocketMessage(GatewayShard shard, string text)
{
if (LogGateway)
{
if (!Directory.Exists("Json"))
Directory.CreateDirectory("Json");
File.WriteAllText(
$"Json/{DateTime.Now:yyyy-MM-dd HH-mm-ss.fffffff}.json",
JsonConvert.SerializeObject(
JsonConvert.DeserializeObject(text),
Formatting.Indented
)
);
}
}
#endregion
#region IDisposable
private bool isDisposed = false;
private void Dispose(bool disposing)
@ -874,7 +856,6 @@ namespace Maki
Dispose(true);
GC.SuppressFinalize(true);
}
#endregion
}
}

7
Maki/DiscordParams.cs Normal file
View file

@ -0,0 +1,7 @@
namespace Maki
{
public class DiscordParams
{
public int BackLogSize = 5;
}
}

View file

@ -62,6 +62,7 @@
<Compile Include="DiscordGame.cs" />
<Compile Include="DiscordMember.cs" />
<Compile Include="DiscordMessage.cs" />
<Compile Include="DiscordParams.cs" />
<Compile Include="DiscordRole.cs" />
<Compile Include="DiscordServer.cs" />
<Compile Include="DiscordUserStatus.cs" />

View file

@ -110,10 +110,7 @@ namespace Maki.Rest
response.ErrorCode = (RestErrorCode)error.Code;
response.ErrorMessage = error.Message;
}
if (client.LogRest)
Console.WriteLine(response.RawResponse);
if (response.Status == 200)
response.Response = JsonConvert.DeserializeObject<T>(response.RawResponse);