2017-05-20 01:29:03 +00:00
|
|
|
|
using Maki.Rest;
|
|
|
|
|
using Maki.Structures.Messages;
|
|
|
|
|
using Maki.Structures.Rest;
|
2017-05-14 12:02:51 +00:00
|
|
|
|
using System;
|
2017-10-13 20:05:50 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2017-05-16 22:45:28 +00:00
|
|
|
|
using System.Linq;
|
2017-05-14 12:02:51 +00:00
|
|
|
|
|
|
|
|
|
namespace Maki
|
|
|
|
|
{
|
|
|
|
|
public class DiscordMessage
|
|
|
|
|
{
|
|
|
|
|
public readonly ulong Id;
|
|
|
|
|
public readonly DateTime Sent;
|
2017-05-21 01:30:45 +00:00
|
|
|
|
public readonly DiscordMember Member;
|
|
|
|
|
public readonly DiscordUser User;
|
2017-05-14 12:02:51 +00:00
|
|
|
|
public readonly DiscordChannel Channel;
|
|
|
|
|
private readonly Discord client;
|
|
|
|
|
|
|
|
|
|
public string Text { get; internal set; }
|
|
|
|
|
public DateTime Edited { get; internal set; }
|
2017-10-13 20:05:50 +00:00
|
|
|
|
public IEnumerable<DiscordMember> MentionsUsers { get; internal set; }
|
|
|
|
|
public IEnumerable<DiscordRole> MentionsRoles { get; internal set; }
|
2017-05-16 22:45:28 +00:00
|
|
|
|
public bool MentionsEveryone { get; internal set; }
|
2017-05-17 22:33:09 +00:00
|
|
|
|
public bool IsPinned { get; internal set; }
|
2017-05-16 22:45:28 +00:00
|
|
|
|
|
|
|
|
|
public bool MentionsMe(bool everyone = false, bool roles = true) =>
|
|
|
|
|
(everyone && MentionsEveryone)
|
2017-10-13 20:05:50 +00:00
|
|
|
|
|| (Channel.Type != DiscordChannelType.Private && roles && client.MemberManager.Id(Channel.Server, client.Me)?.Roles.Where(x => MentionsRoles.Contains(x)).Count() > 0)
|
2017-05-16 22:45:28 +00:00
|
|
|
|
|| MentionsUsers.Select(x => x.User).Contains(client.Me);
|
|
|
|
|
|
2017-05-21 01:30:45 +00:00
|
|
|
|
public bool IsMe => client.Me == User;
|
2017-05-14 12:02:51 +00:00
|
|
|
|
|
2017-10-13 20:05:50 +00:00
|
|
|
|
public DiscordServer Server => Channel.Server;
|
|
|
|
|
|
2017-05-21 01:30:45 +00:00
|
|
|
|
internal DiscordMessage(Discord discord, Message msg, DiscordChannel channel, DiscordMember member = null)
|
2017-05-14 12:02:51 +00:00
|
|
|
|
{
|
|
|
|
|
client = discord;
|
|
|
|
|
Id = msg.Id;
|
|
|
|
|
Sent = msg.Sent;
|
|
|
|
|
Text = msg.Content;
|
2017-05-21 01:30:45 +00:00
|
|
|
|
|
|
|
|
|
if (member != null)
|
|
|
|
|
{
|
|
|
|
|
Member = member;
|
|
|
|
|
User = member.User;
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
member = null;
|
2017-10-13 20:05:50 +00:00
|
|
|
|
User = client.UserManager.Id(msg.User.Id);
|
|
|
|
|
Debug.Assert(User != null, "user is null");
|
2017-05-21 01:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-14 12:02:51 +00:00
|
|
|
|
Channel = channel;
|
2017-10-13 20:05:50 +00:00
|
|
|
|
MentionsUsers = client.MemberManager.Server(channel.Server).Where(x => msg.Mentions.Select(y => y.Id).Contains(x.User.Id));
|
|
|
|
|
MentionsRoles = client.RoleManager .Server(channel.Server).Where(x => msg.MentionsRoles.Contains(x.Id));
|
2017-05-16 22:45:28 +00:00
|
|
|
|
MentionsEveryone = msg.MentioningEveryone;
|
2017-05-17 22:33:09 +00:00
|
|
|
|
IsPinned = msg.IsPinned == true;
|
2017-05-14 12:02:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-20 01:29:03 +00:00
|
|
|
|
public DiscordMessage Edit(string text = null, DiscordEmbed embed = null)
|
2017-05-14 12:02:51 +00:00
|
|
|
|
{
|
2017-05-31 21:02:38 +00:00
|
|
|
|
using (WebRequest wr = new WebRequest(HttpMethod.PATCH, RestEndpoints.ChannelMessage(Channel.Id, Id)))
|
|
|
|
|
{
|
|
|
|
|
wr.AddJson(new MessageEdit
|
|
|
|
|
{
|
|
|
|
|
Text = text,
|
|
|
|
|
Embed = embed?.ToStruct(),
|
|
|
|
|
});
|
|
|
|
|
wr.Perform();
|
|
|
|
|
|
|
|
|
|
if (wr.Status != 200)
|
|
|
|
|
return this;
|
2017-05-20 01:29:03 +00:00
|
|
|
|
|
2017-05-31 21:02:38 +00:00
|
|
|
|
Message msg = wr.ResponseJson<Message>();
|
|
|
|
|
|
|
|
|
|
Text = msg.Content;
|
|
|
|
|
Edited = msg.Edited ?? DateTime.UtcNow;
|
|
|
|
|
}
|
2017-05-20 01:29:03 +00:00
|
|
|
|
|
2017-05-14 12:02:51 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
2017-05-20 01:29:03 +00:00
|
|
|
|
|
|
|
|
|
public void Delete()
|
|
|
|
|
{
|
2017-05-31 21:02:38 +00:00
|
|
|
|
using (WebRequest wr = new WebRequest(HttpMethod.DELETE, RestEndpoints.ChannelMessage(Channel.Id, Id)))
|
|
|
|
|
wr.Perform();
|
2017-05-20 01:29:03 +00:00
|
|
|
|
}
|
2017-05-14 12:02:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|