Archived
1
0
Fork 0
This commit is contained in:
flash 2017-08-09 23:23:43 +02:00
parent fb1873b779
commit 82a74a3e63
5 changed files with 37 additions and 32 deletions

View file

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

View file

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

View file

@ -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();
}

View file

@ -3,7 +3,7 @@
/// <summary>
/// Discord Rest request methods
/// </summary>
enum HttpMethod
public enum HttpMethod
{
/// <summary>
/// GET, does not send additional data

View file

@ -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<T>() =>
public T ResponseJson<T>() =>
JsonConvert.DeserializeObject<T>(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<string, string> header in headers)