From 82a74a3e633428515bddfdfe38e55ac582b58952 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 9 Aug 2017 23:23:43 +0200 Subject: [PATCH] idk lol --- Maki/DiscordChannel.cs | 20 ++++++++--------- Maki/DiscordMessage.cs | 2 +- Maki/Gateway/GatewayShard.cs | 2 +- Maki/Rest/HttpMethod.cs | 2 +- Maki/Rest/WebRequest.cs | 43 ++++++++++++++++++++---------------- 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/Maki/DiscordChannel.cs b/Maki/DiscordChannel.cs index fe8ae57..a748743 100644 --- a/Maki/DiscordChannel.cs +++ b/Maki/DiscordChannel.cs @@ -39,14 +39,7 @@ namespace Maki UserLimit = channel.UserLimit ?? 0; Server = server; } - - public DiscordMessage Send(Stream stream, string filename, string text = "", DiscordEmbed embed = null) - { - byte[] bytes = new byte[stream.Length]; - stream.Read(bytes, 0, bytes.Length); - return Send(text, embed, filename, bytes); - } - + public DiscordMessage Send(string text = "", DiscordEmbed embed = null, string filename = null, byte[] file = null) { Message? msg = null; @@ -58,7 +51,7 @@ namespace Maki Text = text, Embed = embed?.ToStruct(), })); - + if (file != null && !string.IsNullOrEmpty(filename)) wr.AddFile(filename, file); @@ -73,10 +66,17 @@ namespace Maki if (!msg.HasValue) throw new DiscordException("Empty response?"); - + DiscordMessage message = new DiscordMessage(client, msg.Value, this, client.members.Find(x => x.User.Id == msg.Value.User.Id)); client.messages.Add(message); return message; } + + public DiscordMessage Send(Stream stream, string filename, string text = "", DiscordEmbed embed = null) + { + byte[] bytes = new byte[stream.Length]; + stream.Read(bytes, 0, bytes.Length); + return Send(text, embed, filename, bytes); + } } } diff --git a/Maki/DiscordMessage.cs b/Maki/DiscordMessage.cs index efe24d3..586f2aa 100644 --- a/Maki/DiscordMessage.cs +++ b/Maki/DiscordMessage.cs @@ -24,7 +24,7 @@ namespace Maki public bool MentionsMe(bool everyone = false, bool roles = true) => (everyone && MentionsEveryone) - || (Channel.Type != DiscordChannelType.Private && roles && client.members.Where(x => x.User == client.Me && x.Server == Channel.Server).First().Roles.Where(x => MentionsRoles.Contains(x)).Count() > 0) + || (Channel.Type != DiscordChannelType.Private && roles && client.members.Where(x => x.User == client.Me && x.Server == Channel.Server).FirstOrDefault()?.Roles.Where(x => MentionsRoles.Contains(x)).Count() > 0) || MentionsUsers.Select(x => x.User).Contains(client.Me); public bool IsMe => client.Me == User; diff --git a/Maki/Gateway/GatewayShard.cs b/Maki/Gateway/GatewayShard.cs index 9a89d14..5106c99 100644 --- a/Maki/Gateway/GatewayShard.cs +++ b/Maki/Gateway/GatewayShard.cs @@ -152,7 +152,7 @@ namespace Maki.Gateway heartbeatHandler.Stop(); OnSocketClose?.Invoke(this, e.WasClean, e.Code, e.Reason); - if (!e.WasClean) + if (!e.WasClean || e.Code != 1000) Connect(); } diff --git a/Maki/Rest/HttpMethod.cs b/Maki/Rest/HttpMethod.cs index 1529e35..6ba56c0 100644 --- a/Maki/Rest/HttpMethod.cs +++ b/Maki/Rest/HttpMethod.cs @@ -3,7 +3,7 @@ /// /// Discord Rest request methods /// - enum HttpMethod + public enum HttpMethod { /// /// GET, does not send additional data diff --git a/Maki/Rest/WebRequest.cs b/Maki/Rest/WebRequest.cs index d353530..127fb2e 100644 --- a/Maki/Rest/WebRequest.cs +++ b/Maki/Rest/WebRequest.cs @@ -9,17 +9,22 @@ using System.Threading; namespace Maki.Rest { - internal class WebRequest : IDisposable + public class WebRequest : IDisposable { private const string USER_AGENT = @"DiscordBot (https://github.com/flashwave/maki, 1.0.0.0)"; private const string GENERIC_CONTENT_TYPE = @"application/octet-stream"; private const string JSON_CONTENT_TYPE = @"application/json"; private const string FORM_CONTENT_TYPE = @"multipart/form-data"; - internal readonly HttpMethod Method; - internal readonly string Url; + private const long BUFFER_SIZE = 5242880; - internal string ContentType { get; set; } = GENERIC_CONTENT_TYPE; + public readonly HttpMethod Method; + public readonly string Url; + + public string UserAgent { get; set; } = USER_AGENT; + + public string ContentType { get; set; } = GENERIC_CONTENT_TYPE; + public long ContentLength => wResponse.ContentLength < 1 ? BUFFER_SIZE : wResponse.ContentLength; // TODO: make this not static internal static string Authorisation { get; set; } @@ -43,13 +48,13 @@ namespace Maki.Rest private Stream responseStream; private byte[] rawResponse; - internal byte[] RawResponse + public byte[] RawResponse { get { if (rawResponse == null) { - rawResponse = new byte[4096]; + rawResponse = new byte[BUFFER_SIZE]; responseStream.Read(rawResponse, 0, rawResponse.Length); } @@ -59,7 +64,7 @@ namespace Maki.Rest private string responseString = string.Empty; - internal string Response + public string Response { get { @@ -70,10 +75,10 @@ namespace Maki.Rest } } - internal T ResponseJson() => + public T ResponseJson() => JsonConvert.DeserializeObject(Response); - internal short Status => + public short Status => (short)wResponse?.StatusCode; static WebRequest() @@ -81,35 +86,35 @@ namespace Maki.Rest ServicePointManager.Expect100Continue = false; } - internal WebRequest(HttpMethod method, string url) + public WebRequest(HttpMethod method, string url) { Method = method; Url = url; } - internal void AddRaw(byte[] bytes) => + public void AddRaw(byte[] bytes) => rawContent = bytes; - internal void AddRaw(string str) => + public void AddRaw(string str) => AddRaw(Encoding.UTF8.GetBytes(str)); - internal void AddJson(object obj) + public void AddJson(object obj) { ContentType = JSON_CONTENT_TYPE; AddRaw(JsonConvert.SerializeObject(obj)); } - internal void AddParam(string name, string contents) => + public void AddParam(string name, string contents) => parameters.Add(name, contents); - internal void AddFile(string name, byte[] bytes) => + public void AddFile(string name, byte[] bytes) => files.Add(name, bytes); - internal void Perform() + public void Perform() { StringBuilder urlBuilder = new StringBuilder(); - if (!Url.StartsWith("http://") || !Url.StartsWith("https://")) + if (!Url.StartsWith("http://") && !Url.StartsWith("https://")) { urlBuilder.Append(RestEndpoints.BASE_URL); urlBuilder.Append(RestEndpoints.BASE_PATH); @@ -132,13 +137,13 @@ namespace Maki.Rest wRequest = System.Net.WebRequest.Create(url) as HttpWebRequest; wRequest.Method = Method.ToString(); - wRequest.UserAgent = USER_AGENT; + wRequest.UserAgent = UserAgent; wRequest.KeepAlive = true; //wRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; wRequest.ReadWriteTimeout = Timeout.Infinite; wRequest.Timeout = Timeout.Infinite; - if (!string.IsNullOrEmpty(Authorisation)) + if (!string.IsNullOrEmpty(Authorisation) && url.StartsWith(RestEndpoints.BASE_URL + RestEndpoints.BASE_PATH)) wRequest.Headers[HttpRequestHeader.Authorization] = Authorisation; foreach (KeyValuePair header in headers)