87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
using Maki.Rest;
|
|
using Maki.Structures.Channels;
|
|
using Maki.Structures.Messages;
|
|
using Maki.Structures.Rest;
|
|
using Newtonsoft.Json;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
|
|
namespace Maki
|
|
{
|
|
public class DiscordChannel
|
|
{
|
|
public readonly ulong Id;
|
|
public readonly DiscordServer Server;
|
|
private readonly Discord client;
|
|
|
|
public string Name { get; internal set; }
|
|
public DiscordChannelType Type { get; internal set; }
|
|
|
|
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)
|
|
{
|
|
client = discord;
|
|
Id = channel.Id;
|
|
Name = channel.Name;
|
|
Type = (DiscordChannelType)channel.Type;
|
|
Position = channel.Position ?? 0;
|
|
Topic = channel.Topic;
|
|
LastMessageId = channel.LastMessageId ?? 0;
|
|
Bitrate = channel.Bitrate ?? 0;
|
|
UserLimit = channel.UserLimit ?? 0;
|
|
Server = server;
|
|
}
|
|
|
|
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
|
|
{
|
|
Text = text,
|
|
Embed = embed?.ToStruct(),
|
|
}));
|
|
|
|
if (file != null && !string.IsNullOrEmpty(filename))
|
|
wr.AddFile(filename, file);
|
|
|
|
wr.Perform();
|
|
|
|
if (wr.Status != 200 || wr.Response.Length < 1)
|
|
// TODO: elaborate
|
|
//throw new DiscordException("Failed to send message");
|
|
return null;
|
|
|
|
msg = wr.ResponseJson<Message>();
|
|
}
|
|
|
|
if (!msg.HasValue)
|
|
throw new DiscordException("Empty response?");
|
|
|
|
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);
|
|
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);
|
|
}
|
|
}
|
|
}
|