idk lol
This commit is contained in:
parent
fb1873b779
commit
82a74a3e63
|
@ -39,14 +39,7 @@ namespace Maki
|
||||||
UserLimit = channel.UserLimit ?? 0;
|
UserLimit = channel.UserLimit ?? 0;
|
||||||
Server = server;
|
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)
|
public DiscordMessage Send(string text = "", DiscordEmbed embed = null, string filename = null, byte[] file = null)
|
||||||
{
|
{
|
||||||
Message? msg = null;
|
Message? msg = null;
|
||||||
|
@ -58,7 +51,7 @@ namespace Maki
|
||||||
Text = text,
|
Text = text,
|
||||||
Embed = embed?.ToStruct(),
|
Embed = embed?.ToStruct(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if (file != null && !string.IsNullOrEmpty(filename))
|
if (file != null && !string.IsNullOrEmpty(filename))
|
||||||
wr.AddFile(filename, file);
|
wr.AddFile(filename, file);
|
||||||
|
|
||||||
|
@ -73,10 +66,17 @@ namespace Maki
|
||||||
|
|
||||||
if (!msg.HasValue)
|
if (!msg.HasValue)
|
||||||
throw new DiscordException("Empty response?");
|
throw new DiscordException("Empty response?");
|
||||||
|
|
||||||
DiscordMessage message = new DiscordMessage(client, msg.Value, this, client.members.Find(x => x.User.Id == msg.Value.User.Id));
|
DiscordMessage message = new DiscordMessage(client, msg.Value, this, client.members.Find(x => x.User.Id == msg.Value.User.Id));
|
||||||
client.messages.Add(message);
|
client.messages.Add(message);
|
||||||
return 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ namespace Maki
|
||||||
|
|
||||||
public bool MentionsMe(bool everyone = false, bool roles = true) =>
|
public bool MentionsMe(bool everyone = false, bool roles = true) =>
|
||||||
(everyone && MentionsEveryone)
|
(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);
|
|| MentionsUsers.Select(x => x.User).Contains(client.Me);
|
||||||
|
|
||||||
public bool IsMe => client.Me == User;
|
public bool IsMe => client.Me == User;
|
||||||
|
|
|
@ -152,7 +152,7 @@ namespace Maki.Gateway
|
||||||
heartbeatHandler.Stop();
|
heartbeatHandler.Stop();
|
||||||
OnSocketClose?.Invoke(this, e.WasClean, e.Code, e.Reason);
|
OnSocketClose?.Invoke(this, e.WasClean, e.Code, e.Reason);
|
||||||
|
|
||||||
if (!e.WasClean)
|
if (!e.WasClean || e.Code != 1000)
|
||||||
Connect();
|
Connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Discord Rest request methods
|
/// Discord Rest request methods
|
||||||
/// </summary>
|
/// </summary>
|
||||||
enum HttpMethod
|
public enum HttpMethod
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// GET, does not send additional data
|
/// GET, does not send additional data
|
||||||
|
|
|
@ -9,17 +9,22 @@ using System.Threading;
|
||||||
|
|
||||||
namespace Maki.Rest
|
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 USER_AGENT = @"DiscordBot (https://github.com/flashwave/maki, 1.0.0.0)";
|
||||||
private const string GENERIC_CONTENT_TYPE = @"application/octet-stream";
|
private const string GENERIC_CONTENT_TYPE = @"application/octet-stream";
|
||||||
private const string JSON_CONTENT_TYPE = @"application/json";
|
private const string JSON_CONTENT_TYPE = @"application/json";
|
||||||
private const string FORM_CONTENT_TYPE = @"multipart/form-data";
|
private const string FORM_CONTENT_TYPE = @"multipart/form-data";
|
||||||
|
|
||||||
internal readonly HttpMethod Method;
|
private const long BUFFER_SIZE = 5242880;
|
||||||
internal readonly string Url;
|
|
||||||
|
|
||||||
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
|
// TODO: make this not static
|
||||||
internal static string Authorisation { get; set; }
|
internal static string Authorisation { get; set; }
|
||||||
|
@ -43,13 +48,13 @@ namespace Maki.Rest
|
||||||
private Stream responseStream;
|
private Stream responseStream;
|
||||||
|
|
||||||
private byte[] rawResponse;
|
private byte[] rawResponse;
|
||||||
internal byte[] RawResponse
|
public byte[] RawResponse
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (rawResponse == null)
|
if (rawResponse == null)
|
||||||
{
|
{
|
||||||
rawResponse = new byte[4096];
|
rawResponse = new byte[BUFFER_SIZE];
|
||||||
responseStream.Read(rawResponse, 0, rawResponse.Length);
|
responseStream.Read(rawResponse, 0, rawResponse.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,7 +64,7 @@ namespace Maki.Rest
|
||||||
|
|
||||||
private string responseString = string.Empty;
|
private string responseString = string.Empty;
|
||||||
|
|
||||||
internal string Response
|
public string Response
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
|
@ -70,10 +75,10 @@ namespace Maki.Rest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal T ResponseJson<T>() =>
|
public T ResponseJson<T>() =>
|
||||||
JsonConvert.DeserializeObject<T>(Response);
|
JsonConvert.DeserializeObject<T>(Response);
|
||||||
|
|
||||||
internal short Status =>
|
public short Status =>
|
||||||
(short)wResponse?.StatusCode;
|
(short)wResponse?.StatusCode;
|
||||||
|
|
||||||
static WebRequest()
|
static WebRequest()
|
||||||
|
@ -81,35 +86,35 @@ namespace Maki.Rest
|
||||||
ServicePointManager.Expect100Continue = false;
|
ServicePointManager.Expect100Continue = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal WebRequest(HttpMethod method, string url)
|
public WebRequest(HttpMethod method, string url)
|
||||||
{
|
{
|
||||||
Method = method;
|
Method = method;
|
||||||
Url = url;
|
Url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AddRaw(byte[] bytes) =>
|
public void AddRaw(byte[] bytes) =>
|
||||||
rawContent = bytes;
|
rawContent = bytes;
|
||||||
|
|
||||||
internal void AddRaw(string str) =>
|
public void AddRaw(string str) =>
|
||||||
AddRaw(Encoding.UTF8.GetBytes(str));
|
AddRaw(Encoding.UTF8.GetBytes(str));
|
||||||
|
|
||||||
internal void AddJson(object obj)
|
public void AddJson(object obj)
|
||||||
{
|
{
|
||||||
ContentType = JSON_CONTENT_TYPE;
|
ContentType = JSON_CONTENT_TYPE;
|
||||||
AddRaw(JsonConvert.SerializeObject(obj));
|
AddRaw(JsonConvert.SerializeObject(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AddParam(string name, string contents) =>
|
public void AddParam(string name, string contents) =>
|
||||||
parameters.Add(name, contents);
|
parameters.Add(name, contents);
|
||||||
|
|
||||||
internal void AddFile(string name, byte[] bytes) =>
|
public void AddFile(string name, byte[] bytes) =>
|
||||||
files.Add(name, bytes);
|
files.Add(name, bytes);
|
||||||
|
|
||||||
internal void Perform()
|
public void Perform()
|
||||||
{
|
{
|
||||||
StringBuilder urlBuilder = new StringBuilder();
|
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_URL);
|
||||||
urlBuilder.Append(RestEndpoints.BASE_PATH);
|
urlBuilder.Append(RestEndpoints.BASE_PATH);
|
||||||
|
@ -132,13 +137,13 @@ namespace Maki.Rest
|
||||||
|
|
||||||
wRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
|
wRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
|
||||||
wRequest.Method = Method.ToString();
|
wRequest.Method = Method.ToString();
|
||||||
wRequest.UserAgent = USER_AGENT;
|
wRequest.UserAgent = UserAgent;
|
||||||
wRequest.KeepAlive = true;
|
wRequest.KeepAlive = true;
|
||||||
//wRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
|
//wRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
|
||||||
wRequest.ReadWriteTimeout = Timeout.Infinite;
|
wRequest.ReadWriteTimeout = Timeout.Infinite;
|
||||||
wRequest.Timeout = 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;
|
wRequest.Headers[HttpRequestHeader.Authorization] = Authorisation;
|
||||||
|
|
||||||
foreach (KeyValuePair<string, string> header in headers)
|
foreach (KeyValuePair<string, string> header in headers)
|
||||||
|
|
Reference in a new issue