2017-05-14 12:02:51 +00:00
|
|
|
|
using Maki.Rest;
|
|
|
|
|
using Maki.Structures.Channels;
|
|
|
|
|
using Maki.Structures.Messages;
|
|
|
|
|
using Maki.Structures.Rest;
|
2017-05-31 21:02:38 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2017-10-13 20:05:50 +00:00
|
|
|
|
using System.Diagnostics;
|
2017-05-31 21:02:38 +00:00
|
|
|
|
using System.IO;
|
2017-05-14 12:02:51 +00:00
|
|
|
|
|
|
|
|
|
namespace Maki
|
|
|
|
|
{
|
|
|
|
|
public class DiscordChannel
|
|
|
|
|
{
|
|
|
|
|
public readonly ulong Id;
|
|
|
|
|
public readonly DiscordServer Server;
|
|
|
|
|
private readonly Discord client;
|
|
|
|
|
|
|
|
|
|
public string Name { get; internal set; }
|
2017-05-16 22:45:28 +00:00
|
|
|
|
public DiscordChannelType Type { get; internal set; }
|
2017-05-14 12:02:51 +00:00
|
|
|
|
|
2017-05-17 22:33:09 +00:00
|
|
|
|
public int Position { get; internal set; }
|
|
|
|
|
|
|
|
|
|
public string Topic { get; internal set; }
|
|
|
|
|
public ulong LastMessageId { get; internal set; }
|
|
|
|
|
|
|
|
|
|
public int Bitrate { get; internal set; }
|
|
|
|
|
public int UserLimit { get; internal set; }
|
|
|
|
|
|
|
|
|
|
public override string ToString() => $@"<#{Id}>";
|
|
|
|
|
|
|
|
|
|
internal DiscordChannel(Discord discord, Channel channel, DiscordServer server = null)
|
2017-05-14 12:02:51 +00:00
|
|
|
|
{
|
|
|
|
|
client = discord;
|
|
|
|
|
Id = channel.Id;
|
|
|
|
|
Name = channel.Name;
|
2017-05-16 22:45:28 +00:00
|
|
|
|
Type = (DiscordChannelType)channel.Type;
|
2017-05-17 22:33:09 +00:00
|
|
|
|
Position = channel.Position ?? 0;
|
|
|
|
|
Topic = channel.Topic;
|
|
|
|
|
LastMessageId = channel.LastMessageId ?? 0;
|
|
|
|
|
Bitrate = channel.Bitrate ?? 0;
|
|
|
|
|
UserLimit = channel.UserLimit ?? 0;
|
2017-05-14 12:02:51 +00:00
|
|
|
|
Server = server;
|
|
|
|
|
}
|
2017-08-09 21:23:43 +00:00
|
|
|
|
|
2017-05-31 21:02:38 +00:00
|
|
|
|
public DiscordMessage Send(string text = "", DiscordEmbed embed = null, string filename = null, byte[] file = null)
|
|
|
|
|
{
|
|
|
|
|
Message? msg = null;
|
|
|
|
|
|
|
|
|
|
using (WebRequest wr = new WebRequest(HttpMethod.POST, RestEndpoints.ChannelMessages(Id)))
|
|
|
|
|
{
|
|
|
|
|
wr.AddParam("payload_json", JsonConvert.SerializeObject(new MessageCreate
|
2017-05-20 01:29:03 +00:00
|
|
|
|
{
|
|
|
|
|
Text = text,
|
2017-05-21 01:08:34 +00:00
|
|
|
|
Embed = embed?.ToStruct(),
|
2017-05-31 21:02:38 +00:00
|
|
|
|
}));
|
2017-08-09 21:23:43 +00:00
|
|
|
|
|
2017-05-31 21:02:38 +00:00
|
|
|
|
if (file != null && !string.IsNullOrEmpty(filename))
|
|
|
|
|
wr.AddFile(filename, file);
|
|
|
|
|
|
|
|
|
|
wr.Perform();
|
|
|
|
|
|
|
|
|
|
if (wr.Status != 200 || wr.Response.Length < 1)
|
|
|
|
|
// TODO: elaborate
|
2017-10-13 20:05:50 +00:00
|
|
|
|
//throw new DiscordException("Failed to send message");
|
|
|
|
|
return null;
|
2017-05-31 21:02:38 +00:00
|
|
|
|
|
|
|
|
|
msg = wr.ResponseJson<Message>();
|
|
|
|
|
}
|
2017-05-20 01:29:03 +00:00
|
|
|
|
|
2017-05-31 21:02:38 +00:00
|
|
|
|
if (!msg.HasValue)
|
|
|
|
|
throw new DiscordException("Empty response?");
|
2017-08-09 21:23:43 +00:00
|
|
|
|
|
2017-10-13 20:05:50 +00:00
|
|
|
|
DiscordMember member = client.MemberManager.Id(Server, msg.Value.User.Id);
|
|
|
|
|
Debug.Assert(member != null, "member is null");
|
|
|
|
|
|
|
|
|
|
DiscordMessage message = new DiscordMessage(client, msg.Value, this, member);
|
|
|
|
|
client.MessageManager.Add(message);
|
2017-05-20 01:29:03 +00:00
|
|
|
|
return message;
|
|
|
|
|
}
|
2017-08-09 21:23:43 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2017-05-14 12:02:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|